-
Notifications
You must be signed in to change notification settings - Fork 0
/
1033.cpp
60 lines (47 loc) · 1.52 KB
/
1033.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
#include <cstdio>
#include <vector>
#include <set>
using namespace std;
int gcd(int a, int b) {
if(a < b) return gcd(b, a);
return (a % b) ? gcd(b, a%b) : b;
}
int main() {
int N, a, b, p, q, g, i, j, temp1, temp2;
unsigned int amounts[10] = {1,1,1,1,1,1,1,1,1,1}; // 9^10 ~= 3.4x10^9. out of signed int range
vector<int> connections[10];
set<int> tempset;
set<int>::iterator setiter;
set<int>::iterator setiter2;
scanf("%d",&N);
for(i=0;i<N;++i)
connections[i].push_back(i);
for(i=0;i<N-1;++i) {
scanf("%d %d %d %d",&a,&b,&p,&q);
temp1 = amounts[b] * p;
temp2 = amounts[a] * q;
for(j=0;j<connections[a].size();++j)
amounts[connections[a][j]] *= temp1;
for(j=0;j<connections[b].size();++j)
amounts[connections[b][j]] *= temp2;
tempset.clear();
for(j=0;j<connections[a].size();++j)
tempset.insert(connections[a][j]);
for(j=0;j<connections[b].size();++j)
tempset.insert(connections[b][j]);
for(setiter=tempset.begin();setiter!=tempset.end();++setiter) {
connections[*setiter].clear();
for(setiter2=tempset.begin();setiter2!=tempset.end();++setiter2) {
connections[*setiter].push_back(*setiter2);
}
}
}
g = gcd(amounts[0], amounts[1]);
for(i=2;i<N;++i)
g = gcd(g, amounts[i]);
for(i=0;i<N;++i)
amounts[i] /= g;
for(i=0;i<N;++i)
printf("%d ",amounts[i]);
return 0;
}