-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCocktail
More file actions
161 lines (146 loc) · 4.13 KB
/
Cocktail
File metadata and controls
161 lines (146 loc) · 4.13 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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.StringTokenizer;
/**
* author : MoonDooo
* 칵테일 백준 #1033
*/
public class Main {
public static void main(String[] args) {
new Cocktail();
}
}
class Cocktail{
private Node[] nodes;
private int nodeNum;
private final Buf buf = new Buf();
private boolean isVisited[];
public Cocktail(){
nodeNum = buf.nextInt();
nodes = new Node[nodeNum];
isVisited = new boolean[nodeNum];
for(int i = 0; i<nodeNum; i++){
nodes[i] = new Node();
}
for(int i = 0; i<nodeNum-1; i++){
int nodeAIdx = buf.nextInt();
int nodeBIdx = buf.nextInt();
int front = buf.nextInt();
int back = buf.nextInt();
nodes[nodeAIdx].addLink(nodeBIdx, front, back);
nodes[nodeBIdx].addLink(nodeAIdx, back, front);
}
nodes[0].setValue(getNumerator(0, 1));
Arrays.fill(isVisited, false);
calculating(0, nodes[0].getValue());
/**
* 공약수 구하기
*/
long pre = nodes[0].getValue();
long r = 1;
for(int i = 1; i<nodeNum; i++){
r = uclid(pre, nodes[i].getValue());
pre = r;
}
for(int i = 0; i <nodeNum; i++){
System.out.print(nodes[i].getValue()/r + " ");
}
}
private long uclid(long pre, long value) {
long x, y;
if(pre < value){
x = value;
y = pre;
}else{
x = pre;
y = value;
}
long r = 1;
long s;
while(r!=0){
r = x%y;
x = y;
if(r != 0){
y = r;
}
}
return y;
}
private void calculating(int nodeIdx, long value) {
isVisited[nodeIdx] = true;
nodes[nodeIdx].setValue(value);
for(int i = 0; i<nodes[nodeIdx].getLinkedNodeIdx().size(); i++){
if (!isVisited[nodes[nodeIdx].getLinkedNodeIdx().get(i)]){
long nextValue = value;
nextValue *= nodes[nodeIdx].getBack().get(i);
nextValue /= nodes[nodeIdx].getFront().get(i);
calculating(nodes[nodeIdx].getLinkedNodeIdx().get(i),nextValue);
}
}
}
public long getNumerator(int nodeIdx, long value){
isVisited[nodeIdx] = true;
for(int i = 0; i<nodes[nodeIdx].getLinkedNodeIdx().size(); i++){
int nextNodeIdx = nodes[nodeIdx].getLinkedNodeIdx().get(i);
if(!isVisited[nextNodeIdx]){
value = getNumerator(nextNodeIdx, value*nodes[nodeIdx].getFront().get(i));
}
}
return value;
}
}
class Node {
private final List<Integer> linkedNodeIdx;
private final List<Integer> front;
private final List<Integer> back;
private long value;
public void setValue(long value){
this.value = value;
}
public long getValue(){
return value;
}
public Node() {
linkedNodeIdx = new ArrayList<>();
front = new ArrayList<>();
back = new ArrayList<>();
}
public List<Integer> getLinkedNodeIdx() {
return linkedNodeIdx;
}
public List<Integer> getFront() {
return front;
}
public List<Integer> getBack() {
return back;
}
public void addLink(Integer nodeIdx, Integer front, Integer back) {
this.linkedNodeIdx.add(nodeIdx);
this.front.add(front);
this.back.add(back);
}
}
class Buf{
BufferedReader br;
StringTokenizer st;
Buf(){
br= new BufferedReader(new InputStreamReader(System.in));
}
String next(){
while(st==null||!st.hasMoreElements()){
try{
st= new StringTokenizer(br.readLine());
}catch (IOException e) {
throw new RuntimeException(e);
}
}
return st.nextToken();
}
int nextInt(){
return Integer.parseInt(next());
}
}