-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcut_the_tree.java
110 lines (98 loc) · 3.18 KB
/
cut_the_tree.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
import java.io.*;
import java.util.*;
public class Solution{
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int N = Integer.parseInt(br.readLine());
//Get vertex values and tree sum
Vertex[] verts = new Vertex[N];
N = 0;
int sum = 0;
for(String s : br.readLine().split(" ")){
short v = Short.parseShort(s);
sum += v;
verts[N++] = new Vertex(v);
}
//Get edges
while (--N > 0){
String[] temp = br.readLine().split(" ");
int i1 = Integer.parseInt(temp[0]) - 1;
int i2 = Integer.parseInt(temp[1]) - 1;
verts[i1].addEdge(verts[i2], sum);
verts[i2].addEdge(verts[i1], sum);
}
//Initialize
Vertex.init(verts[0]);
//Find smallest diff
int min = Integer.MAX_VALUE;
for(Vertex vertex : verts){
int curMin = vertex.minDiff(sum);
//Update min
min = (curMin < min) ? curMin : min;
}
//Output
System.out.print(min);
}
private static class Vertex{
private int minDiff;
private short value;
private boolean isInit;
private List<Edge> edges;
public Vertex(short value){
this.value = value;
this.isInit = false;
this.edges = new ArrayList<Edge>();
}
public void addEdge(Vertex vertex, int value){
edges.add(new Edge(vertex, value));
}
public int minDiff(int sum){
int min = sum;
for(Edge edge : edges){
//Get difference of both possible trees
int curMin = sum - 2*edge.value;
//Make absolute value
curMin = (curMin < 0) ? -curMin : curMin;
//Update min
min = (curMin < min) ? curMin : min;
}
return min;
}
public static void init(Vertex v){
if (!v.isInit){
v.init();
}
}
private int init(){
this.isInit = true;
Edge caller = null;
int sum = this.value;
//For each edge
for(Edge edge : edges){
//If it's the caller, save for later
if (edge.vertex.isInit){
caller = edge;
//Otherwise, get the sum of
//all vertices for that edge
} else {
sum += edge.value = edge.vertex.init();
}
}
//If it's the caller
//subtract this vertex's
//sum from the tree's sum
if (caller != null){
caller.value -= sum;
}
return sum;
}
private class Edge{
public int value;
public Vertex vertex;
public Edge(Vertex vertex, int value){
this.value = value;
this.vertex = vertex;
}
}
}
}