-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathSolution.java
More file actions
220 lines (170 loc) · 5.76 KB
/
Solution.java
File metadata and controls
220 lines (170 loc) · 5.76 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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
package hackrank.algorithm.search.cuttree;
import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Deque;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Scanner;
import java.util.Set;
/**
* Cut the Tree Challenge
*
* @see https://www.hackerrank.com/challenges/cut-the-tree
*/
public class Solution {
public final static int ROOT_NODE_ID = 1;
public static void main(String[] args) {
Graph graph = readInput();
graph.orient(ROOT_NODE_ID);
int minDiff = Integer.MAX_VALUE;
for (Integer[] edge : graph.edges) {
int from = edge[0];
int to = edge[1];
int diff = graph.cutDiff(from, to);
if (diff < minDiff) {
minDiff = diff;
}
// System.out.println( "Remove (" + from + "," + to + ") => diff = " + diff );
}
System.out.println(minDiff);
}
private static Graph readInput() {
Scanner scanner = new Scanner(System.in);
Graph graph = new Graph(scanner.nextInt());
for (int nodeId = 1; nodeId <= graph.size; nodeId++) {
graph.add(new Node(nodeId, scanner.nextInt()));
}
for (int edge = 0; edge < graph.size - 1; edge++) {
graph.addEdge(scanner.nextInt(), scanner.nextInt());
}
scanner.close();
return graph;
}
static class Graph {
public int total;
public int size;
public Map<Integer, Node> lookup;
public List<Integer[]> edges;
public Graph(int size) {
this.size = size;
this.lookup = new HashMap<>(size, 1.0f);
this.edges = new ArrayList<>(size - 1);
}
public void add(Node node) {
lookup.put(node.id, node);
total += node.value;
}
public void addEdge(int fromId, int toId) {
Node nodeFrom = lookup.get(fromId);
Node nodeTo = lookup.get(toId);
nodeFrom.connect(nodeTo);
edges.add(new Integer[] { fromId, toId });
}
public int cutDiff(int firstId, int secondId) {
Node first = lookup.get(firstId);
Node second = lookup.get(secondId);
Node lower = first.level > second.level ? first : second;
int aboveCutTotal = this.total - lower.total;
return Math.abs(aboveCutTotal - lower.total);
}
public void orient(int rootId) {
Node root = lookup.get(rootId);
root.parent = null;
root.level = 0;
Set<Integer> visited = new HashSet<>(size);
Deque<Node> nodes = new ArrayDeque<>();
nodes.push(root);
while (!nodes.isEmpty()) {
Node current = nodes.pop();
visited.add(current.id);
for (Node child : current.nodes) {
if (!visited.contains(child.id)) {
child.parent = current;
child.level = current.level + 1;
nodes.push(child);
}
}
}
calcTotals(root);
}
private void calcTotals(Node root) {
Set<Integer> visited = new HashSet<>();
Node node = root;
while (node != null) {
while (node.hasChildren()) {
Node nonVisitedChild = findNonVisitedChild(node, visited);
if (nonVisitedChild == null) {
break; // arrived at leaf node or all node children have been visited
} else {
node = nonVisitedChild; // continue traversing further down the graph
}
}
node.total = node.calcTotal();
visited.add(node.id);
node = node.parent;
}
}
private Node findNonVisitedChild(Node node, Set<Integer> visited) {
for (Node child : node.getChildren()) {
if (!visited.contains(child.id)) {
return child;
}
}
return null;
}
}
static class Node {
public int id;
public int value;
public int total;
public int level;
public Node parent;
public List<Node> nodes;
public Node(int id, int value) {
this.id = id;
this.value = value;
this.nodes = new ArrayList<>();
}
public void connect(Node node) {
if (!isConnected(node)) {
this.nodes.add(node);
}
if (!node.isConnected(this)) {
node.nodes.add(this);
}
}
public boolean isConnected(Node node) {
for (Node existing : nodes) {
if (existing.id == node.id) {
return true;
}
}
return false;
}
public List<Node> getChildren() {
if (parent == null) {
return nodes;
}
List<Node> children = new ArrayList<>();
for (Node child : nodes) {
if (child.id != parent.id) {
children.add(child);
}
}
return children;
}
public boolean hasChildren() {
return !nodes.isEmpty();
}
public int calcTotal() {
int childrenTotal = getChildren().stream().mapToInt(n -> n.total).sum();
return value + childrenTotal;
}
@Override
public String toString() {
return "Node (" + id + "): value = " + value + ", total = " + total;
}
}
}