-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolution.java
130 lines (93 loc) · 3.71 KB
/
Solution.java
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
package cutthetree;
import java.io.*;
import java.util.*;
import java.util.stream.*;
import static java.util.stream.Collectors.toList;
class Result {
private static List<Node> nodes;
private static Stack<Node> stack = new Stack<>();
static class Node {
public int index;
public int weight;
public int childrenWeight = 0;
public List<Node> children = new ArrayList<>();
public Node(int index, int weight) {
this.index = index;
this.weight = weight;
}
}
static Set<Integer> visited = new HashSet<>();
public static int cutTheTree(List<Integer> data, List<List<Integer>> edges) {
Node node = makeATree(data, edges);
int totalWeight = data.stream().mapToInt(Integer::intValue).sum();
visited.add(node.index);
stack.push(node);
return calculateChildrenWeightsStack(totalWeight);
}
private static int calculateChildrenWeightsStack(int total) {
int min = total;
while (!stack.isEmpty()) {
Node peek = stack.peek();
boolean allVisited = true;
for (Node child: peek.children) {
child.children.remove(peek); //remove back link
if (!visited.contains(child.index)) {
visited.add(child.index);
stack.push(child);
allVisited = false;
}
}
if (allVisited) {
stack.pop();
for (Node child: peek.children) {
peek.childrenWeight += child.weight + child.childrenWeight;
}
int bottomWeight = peek.weight + peek.childrenWeight;
int topWeight = total - bottomWeight;
int diff = Math.abs(bottomWeight - topWeight);
min = Math.min(diff, min);
}
}
return min;
}
private static Node makeATree(List<Integer> data, List<List<Integer>> edges) {
nodes = IntStream.range(0, data.size())
.mapToObj(index -> new Node(index+1, data.get(index)))
.collect(Collectors.toList());
edges.forEach(edge -> {
Integer index0 = edge.get(0) - 1;
Integer index1 = edge.get(1) - 1;
nodes.get(index0).children.add(nodes.get(index1));
nodes.get(index1).children.add(nodes.get(index0));
});
return nodes.get(0);
}
}
public class Solution {
public static void main(String[] args) throws IOException {
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
// BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(System.getenv("OUTPUT_PATH")));
BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(System.out));
int n = Integer.parseInt(bufferedReader.readLine().trim());
List<Integer> data = Stream.of(bufferedReader.readLine().replaceAll("\\s+$", "").split(" "))
.map(Integer::parseInt)
.collect(toList());
List<List<Integer>> edges = new ArrayList<>();
IntStream.range(0, n - 1).forEach(i -> {
try {
edges.add(
Stream.of(bufferedReader.readLine().replaceAll("\\s+$", "").split(" "))
.map(Integer::parseInt)
.collect(toList())
);
} catch (IOException ex) {
throw new RuntimeException(ex);
}
});
int result = Result.cutTheTree(data, edges);
bufferedWriter.write(String.valueOf(result));
bufferedWriter.newLine();
bufferedReader.close();
bufferedWriter.close();
}
}