-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path10113 - Exchange Rates.cpp
73 lines (67 loc) · 1.4 KB
/
10113 - Exchange Rates.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
#include<bits/stdc++.h>
#define MAX 65
using namespace std;
typedef long long ull;
vector<vector<ull> >v(MAX, vector<ull>(MAX,0));
ull cont;
ull gcd(ull a, ull b){
if(b==0)return a;
return gcd(b,a%b);
}
pair<ull,ull> bfs(ull x, ull y){
pair<ull,ull>ans;
vector<ull>visn(MAX,-1),visd(MAX,-1);
queue<ull>Q;
Q.push(x);
visn[x]=1;
visd[x]=1;
while(!Q.empty()){
ull temp = Q.front();
Q.pop();
if(temp==y){
ull g = gcd(visn[temp],visd[temp]);
visn[temp]/=g;visd[temp]/=g;
ans.first = visn[temp];
ans.second = visd[temp];
v[x][y] = ans.second;
v[y][x] = ans.first;
return ans;
}
for(ull i = 1;i<cont;++i){
if(v[temp][i]!=0&&visn[i]==-1){
visn[i] = visn[temp]*v[i][temp];
visd[i] = visd[temp]*v[temp][i];
ull g = gcd(visn[i],visd[i]);
visn[i]/=g;visd[i]/=g;
Q.push(i);
}
}
}
ans.first = -1;
ans.second = -1;
return ans;
}
int main(){
std::ios::sync_with_stdio(false);
char c;
map<string,ull>m;
ull a,b;
cont=1;
string sa,sb;
while(cin>>c){
if(c=='.')return 0;
else if(c=='!'){
cin>>a>>sa>>c>>b>>sb;
ull g = gcd(a,b);
if(!m[sa])m[sa]=cont++;
if(!m[sb])m[sb]=cont++;
v[m[sa]][m[sb]] = b/g;
v[m[sb]][m[sa]] = a/g;
}else{
cin>>sa>>c>>sb;
pair<ull,ull>p = bfs(m[sa], m[sb]);
if(p.first ==-1)cout<<"? "<<sa<<" = ? "<<sb<<endl;
else cout<<p.first<<" "<<sa<<" = "<<p.second<<" "<<sb<<endl;
}
}
}