Skip to content

Commit 808e3d9

Browse files
authored
Merge pull request #684 from AlgorithmWithGod/0224LJH
[20250818] BOJ / G2 / 칵테일 / 이종환
2 parents 8375c50 + 02a4698 commit 808e3d9

File tree

1 file changed

+187
-0
lines changed

1 file changed

+187
-0
lines changed
Lines changed: 187 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,187 @@
1+
```java
2+
import java.io.BufferedReader;
3+
import java.io.IOException;
4+
import java.io.InputStreamReader;
5+
import java.util.ArrayList;
6+
import java.util.HashSet;
7+
import java.util.LinkedList;
8+
import java.util.List;
9+
import java.util.Queue;
10+
import java.util.StringTokenizer;
11+
12+
13+
public class Main {
14+
static int nodeCnt;
15+
static Node[] nodes;
16+
static Queue<Edge> q = new LinkedList<>();
17+
static HashSet<Node> visited = new HashSet<>();
18+
19+
static class Node{
20+
int idx;
21+
int num;
22+
23+
List<Node> list = new ArrayList<>();
24+
25+
public Node(int idx){
26+
this.idx = idx;
27+
}
28+
29+
public void propagate(int weight){
30+
visited.add(this);
31+
num *= weight;
32+
33+
for (Node node : list){
34+
if (visited.contains(node)) continue;
35+
node.propagate(weight);
36+
}
37+
}
38+
}
39+
40+
static class Edge{
41+
int from;
42+
int to;
43+
int fromWeight;
44+
int toWeight;
45+
46+
public Edge(int from, int to, int fromWeight, int toWeight){
47+
this.from = from;
48+
this.to = to;
49+
this.fromWeight = fromWeight;
50+
this.toWeight = toWeight;
51+
}
52+
}
53+
54+
55+
56+
57+
public static void main(String[] args) throws IOException {
58+
init();
59+
process();
60+
print();
61+
}
62+
63+
private static void init() throws IOException {
64+
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
65+
nodeCnt = Integer.parseInt(br.readLine());
66+
nodes = new Node[nodeCnt];
67+
68+
for (int i = 0 ; i < nodeCnt -1; i++){
69+
StringTokenizer st = new StringTokenizer(br.readLine());
70+
71+
int from = Integer.parseInt(st.nextToken());
72+
int to = Integer.parseInt(st.nextToken());
73+
int fromWeight = Integer.parseInt(st.nextToken());
74+
int toWeight = Integer.parseInt(st.nextToken());
75+
76+
q.add(new Edge(from,to,fromWeight,toWeight));
77+
78+
}
79+
80+
81+
}
82+
83+
private static void process() throws IOException {
84+
firstSetting();
85+
makeNums();
86+
87+
int gcd = euclid();
88+
89+
for (int i = 0; i < nodeCnt; i++){
90+
nodes[i].num /= gcd;
91+
}
92+
93+
94+
95+
for (int i = 0 ; i< nodeCnt ; i++){
96+
System.out.print(nodes[i].num + " ");
97+
}
98+
99+
100+
}
101+
102+
private static int euclid() {
103+
int gcd = nodes[0].num;
104+
int large,small;
105+
106+
for (int i = 1; i < nodeCnt; i++){
107+
108+
109+
int target = nodes[i].num;
110+
111+
if (gcd < target) {
112+
large = target;
113+
small = gcd;
114+
} else if (gcd > target) {
115+
large = gcd;
116+
small = target;
117+
} else continue;
118+
119+
while(true){
120+
int remainder = large%small;
121+
if (remainder == 0){
122+
gcd = small;
123+
break;
124+
}
125+
126+
large = small;
127+
small = remainder;
128+
}
129+
}
130+
131+
return gcd;
132+
}
133+
134+
private static void makeNums() {
135+
while(!q.isEmpty()){
136+
Edge e = q.poll();
137+
138+
if (nodes[e.from] == null && nodes[e.to] == null) {
139+
q.offer(e);
140+
continue;
141+
}
142+
143+
visited = new HashSet<>();
144+
145+
if (nodes[e.from] == null) {
146+
147+
nodes[e.from] = new Node(e.from);
148+
nodes[e.from].num = e.fromWeight * nodes[e.to].num;
149+
nodes[e.from].list.add(nodes[e.to]);
150+
nodes[e.to].list.add(nodes[e.from]);
151+
152+
visited.add(nodes[e.from]);
153+
nodes[e.to].propagate(e.toWeight);
154+
155+
} else{
156+
nodes[e.to] = new Node(e.to);
157+
nodes[e.to].num = e.toWeight * nodes[e.from].num;
158+
nodes[e.to].list.add(nodes[e.from]);
159+
nodes[e.from].list.add(nodes[e.to]);
160+
161+
visited.add(nodes[e.to]);
162+
163+
nodes[e.from].propagate(e.fromWeight);
164+
}
165+
}
166+
}
167+
168+
private static void firstSetting() {
169+
Edge first = q.poll();
170+
171+
nodes[first.from] = new Node(first.from);
172+
nodes[first.from].num = first.fromWeight;
173+
174+
nodes[first.to] = new Node(first.to);
175+
nodes[first.to].num = first.toWeight;
176+
177+
nodes[first.from].list.add(nodes[first.to]);
178+
nodes[first.to].list.add(nodes[first.from]);
179+
180+
}
181+
182+
183+
private static void print() {
184+
185+
}
186+
}
187+
```

0 commit comments

Comments
 (0)