forked from encrypted-def/BOJ
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path1033.cpp
54 lines (54 loc) · 1.22 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
#include <stdio.h>
#include <algorithm>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> PAIR; // {2, 3} : 2/3을 의미
#define X first
#define Y second
int N;
PAIR material[11];
PAIR ratio[11][11]; // ratio[a][b] : a가 1일 때 b가 얼마인지.
ll gcd(ll a, ll b) {
if (a == 0ll)
return b;
return gcd(b%a, a);
}
ll lcm(ll a, ll b) {
return a / gcd(a, b) * b;
}
PAIR mul(PAIR a, PAIR b) {
ll c = a.X * b.X;
ll d = a.Y * b.Y;
ll g = gcd(c, d);
return { c / g, d / g };
}
int main(void) {
scanf("%d", &N);
if (N == 1) {
printf("1");
return 0;
}
for (int i = 0; i < N - 1; i++) {
int a, b, p, q;
scanf("%d %d %d %d", &a, &b, &p, &q);
ratio[a][b] = { q,p };
ratio[b][a] = { p,q };
}
material[0] = { 1,1 };
for (int repeat = 0; repeat < N; repeat++) {
for (int i = 0; i < N; i++) {
if (material[i].X == 0)
continue;
for (int j = 0; j < N; j++) { // i로부터 j를 갱신할 예정
if (ratio[i][j].X == 0 || material[j].X != 0)
continue;
material[j] = mul(material[i], ratio[i][j]);
}
}
}
ll l = lcm(material[0].Y, material[1].Y);
for (int i = 2; i < N; i++)
l = lcm(l, material[i].Y);
for (int i = 0; i < N; i++)
printf("%lld ", material[i].X * l / material[i].Y);
}