-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJ1033.java
More file actions
93 lines (76 loc) · 2.48 KB
/
Copy pathJ1033.java
File metadata and controls
93 lines (76 loc) · 2.48 KB
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
package TIL;
import java.io.*;
import java.util.*;
public class J1033 {
static LinkedList<Integer>[] A;
static boolean[] visited;
static int[] values;
static int node;
public static void main(String[] args) throws IOException {
BufferedReader buffer = new BufferedReader(new InputStreamReader(System.in));
node = Integer.parseInt(buffer.readLine());
A = new LinkedList[node+1];
for(int i = 0; i<= node; i++){
A[i] = new LinkedList<>();
}
values = new int[node+1];
visited = new boolean[node+1];
for(int i =0; i<= node; i++){
values[i] = 1;
}
for(int i = 1; i<node; i++){
String[] input = buffer.readLine().split(" ");
int a = Integer.parseInt(input[0]);
int b = Integer.parseInt(input[1]);
int p = Integer.parseInt(input[2]);
int q = Integer.parseInt(input[3]);
int min = Math.min(p,q);
int max = Math.max(p,q);
int gcd = gcd(max, min);
p = p/gcd;
q = q/gcd;
if(values[b] != 1 || values[a] != 1){
int valuesA;
int valuesB;
valuesA = values[b]*p;
valuesB = values[a]*q;
max = Math.max(valuesA, valuesB);
min = Math.min(valuesA, valuesB);
gcd = gcd(max, min);
valuesA = valuesA/gcd;
valuesB = valuesB/gcd;
values[a] = values[a]*valuesA;
values[b] = values[b]*valuesB;
DFS(a,valuesA);
visited = new boolean[node+1];
DFS(b,valuesB);
visited = new boolean[node+1];
}else {
values[a] = values[a]*p;
values[b] = values[b]*q;
DFS(a,p);
visited = new boolean[node+1];
DFS(b,q);
visited = new boolean[node+1];
}
A[a].add(b);
A[b].add(a);
}
for(int i = 0; i<node; i++){
System.out.print(values[i]+" ");
}
}
public static void DFS(int node, int value){
visited[node] = true;
for(int i : A[node]){
if(!visited[i]){
values[i] = values[i]*value;
DFS(i, value);
}
}
}
public static int gcd(int max, int min){
if(min == 0) return max;
return gcd(min, max%min);
}
}