-
Notifications
You must be signed in to change notification settings - Fork 0
/
EvenTree.java
80 lines (55 loc) · 1.98 KB
/
EvenTree.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
package a;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class EvenTree {
//Tree Structure
private static class Node<T> {
private final List<Node<T>> childs = new ArrayList<>();
private void addChildNode(Node<T> node) {
this.childs.add(node);
}
}
private static int[] decompose(Node<Integer> node) {
int count = 0, edgesToRemove = 0;
for(Node<Integer> childNode : node.childs) {
// For each child node
// calls current method recursively
final int[] tmp = decompose(childNode);
edgesToRemove += tmp[1];
increments edgesToRemove by 1
if(tmp[0] % 2 == 0) {
edgesToRemove++;
} else {
count += tmp[0];
}
}
count++;
return new int[]{count, edgesToRemove};
}
public static void main(String[] args) {
final Scanner in = new Scanner(System.in);
System.out.println(“Enter the Number of nodes followed By The the number of edges and press Enter ”);
final int N = in.nextInt();
final int M = in.nextInt();
final Node<Integer>[] treeNodes = (Node<Integer>[]) new Node[N];
System.out.println(“lets make the edges Now ”);
for(int i = 0; i < M; i++) {
System.out.println(“enter the 2 vertices which will make the edge”+(i+1)+” and press enter ”);
final int node1 = in.nextInt() - 1;
// Gets second node of vertex
final int node2 = in.nextInt() - 1;
treeNodes[node1] = (treeNodes[node1] == null ? new Node<Integer>() : treeNodes[node1]);
treeNodes[node2] = (treeNodes[node2] == null ? new Node<Integer>() : treeNodes[node2]);
// add bigger node to smaller nodes
if(node1 < node2) {
treeNodes[node1].addChildNode(treeNodes[node2]);
} else {
treeNodes[node2].addChildNode(treeNodes[node1]);
}
}
final int[] metadata = decompose(treeNodes[0]);
System.out.println(“number of edges to remove to make it a even tree is ”+metadata[1]);
in.close();
}
}