Skip to content

Commit ee6fa84

Browse files
authored
[20250622] BOJ / G2 / 칵테일 / 권혁준
1 parent f5c6f27 commit ee6fa84

File tree

1 file changed

+139
-0
lines changed

1 file changed

+139
-0
lines changed
Lines changed: 139 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,139 @@
1+
```java
2+
import java.math.BigInteger;
3+
import java.util.*;
4+
import java.io.*;
5+
6+
class IOController {
7+
BufferedReader br;
8+
BufferedWriter bw;
9+
StringTokenizer st;
10+
11+
public IOController() {
12+
br = new BufferedReader(new InputStreamReader(System.in));
13+
bw = new BufferedWriter(new OutputStreamWriter(System.out));
14+
st = new StringTokenizer("");
15+
}
16+
17+
String nextLine() throws Exception {
18+
String line = br.readLine();
19+
st = new StringTokenizer(line);
20+
return line;
21+
}
22+
23+
String nextToken() throws Exception {
24+
while (!st.hasMoreTokens()) nextLine();
25+
return st.nextToken();
26+
}
27+
28+
int nextInt() throws Exception {
29+
return Integer.parseInt(nextToken());
30+
}
31+
32+
long nextLong() throws Exception {
33+
return Long.parseLong(nextToken());
34+
}
35+
36+
double nextDouble() throws Exception {
37+
return Double.parseDouble(nextToken());
38+
}
39+
40+
void close() throws Exception {
41+
bw.flush();
42+
bw.close();
43+
}
44+
45+
void write(String content) throws Exception {
46+
bw.write(content);
47+
}
48+
49+
}
50+
51+
public class Main {
52+
53+
static IOController io;
54+
55+
//
56+
57+
static int N;
58+
static long[] A;
59+
static List<int[]>[] V;
60+
61+
public static void main(String[] args) throws Exception {
62+
63+
io = new IOController();
64+
65+
init();
66+
solve();
67+
68+
io.close();
69+
70+
}
71+
72+
public static void init() throws Exception {
73+
74+
N = io.nextInt();
75+
V = new List[N];
76+
for(int i=0;i<N;i++) V[i] = new ArrayList<>();
77+
for(int i=1;i<N;i++) {
78+
int a = io.nextInt(), b = io.nextInt(), c = io.nextInt(), d = io.nextInt();
79+
long g = gcd(c,d);
80+
c /= g;
81+
d /= g;
82+
V[a].add(new int[]{b, c, d});
83+
V[b].add(new int[]{a, d, c});
84+
}
85+
A = new long[N];
86+
Arrays.fill(A, 1);
87+
88+
}
89+
90+
static void solve() throws Exception {
91+
92+
dfs(0,-1);
93+
for(int i=0;i<N;i++) io.write(A[i] + " ");
94+
95+
}
96+
97+
static void dfs(int n, int p) {
98+
for(int[] e:V[n]) if(e[0] != p) {
99+
int i = e[0], a = e[1], b = e[2];
100+
dfs(i,n);
101+
A[n] *= A[i] * a;
102+
}
103+
change(n,p);
104+
long g = subgcd(n, p);
105+
div(n,p,g);
106+
}
107+
108+
static long gcd(long a, long b) {
109+
if(a < b) {
110+
long temp = a;
111+
a = b;
112+
b = temp;
113+
}
114+
return a%b == 0 ? b : gcd(b, a%b);
115+
}
116+
117+
static long subgcd(int n, int p) {
118+
long g = A[n];
119+
for(int[] e:V[n]) if(e[0] != p) {
120+
g = gcd(subgcd(e[0], n), g);
121+
}
122+
return g;
123+
}
124+
125+
static void div(int n, int p, long g) {
126+
A[n] /= g;
127+
for(int[] e:V[n]) if(e[0] != p) div(e[0],n,g);
128+
}
129+
130+
static void change(int n, int p) {
131+
for(int[] e:V[n]) if(e[0] != p) {
132+
int i = e[0], a = e[1], b = e[2];
133+
A[i] = A[n]*b/a;
134+
change(e[0],n);
135+
}
136+
}
137+
138+
}
139+
```

0 commit comments

Comments
 (0)