-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEvenTree.java
More file actions
80 lines (54 loc) · 1.27 KB
/
Copy pathEvenTree.java
File metadata and controls
80 lines (54 loc) · 1.27 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
import java.util.Scanner;
public class EvenTree {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int nodes = in.nextInt();
int vertices = in.nextInt();
int weightChange;
int[] graph = new int[nodes + 1];
int[] weight = new int[nodes + 1];
for (int i = 0; i < nodes + 1; i++) {
graph[i] = 0;
weight[i] = 1;
}
for (int i = 0; i < vertices; i++) {
int node = in.nextInt();
int parent = in.nextInt();
weightChange = weight[node];
graph[node] = parent;
while (graph[node] != 0) {
weight[graph[node]] += weightChange;
node = graph[node];
}
}
int cutOff = 2;
int cuts = 0;
int parent;
int maxWeight = 0;
for (int i = 0; i < weight[i]; i++) {
maxWeight = (weight[i] > maxWeight ? weight[i] : maxWeight);
}
int oldCuts = -1;
while (cuts != oldCuts) {
oldCuts = cuts;
while (cutOff < maxWeight) {
for (int i = 0; i < vertices; i++) {
if (weight[i] == cutOff) {
cuts += 1;
weightChange = weight[i];
parent = i;
while (graph[parent] != 0) {
weight[parent] -= weightChange;
parent = graph[parent];
}
graph[i] = 0;
}
}
cutOff += 2;
}
cutOff = 2;
}
System.out.println(cuts);
in.close();
}
}