-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEvenTree.java
More file actions
57 lines (49 loc) · 1.45 KB
/
Copy pathEvenTree.java
File metadata and controls
57 lines (49 loc) · 1.45 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
import java.util.*;
public class Solution {
public static int removed = 0;
public static HashSet<String> visitedNode = new HashSet<String>();
public static int removePath(String root,
Hashtable<String, ArrayList<String>> edgeHash) {
ArrayList<String> childs = edgeHash.get(root);
visitedNode.add(root);
int nodeCount = 1;
for (String child : childs) {
if (visitedNode.contains(child)){
continue;
}
int childCount = removePath(child, edgeHash);
if (childCount % 2 == 0) {
removed++;
} else {
nodeCount += childCount;
}
}
return nodeCount;
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
String line = scanner.nextLine();
String[] cols = line.split(" ");
int vNum = Integer.parseInt(cols[0]);
int mNum = Integer.parseInt(cols[1]);
Hashtable<String, ArrayList<String>> edgeHash = new Hashtable<String, ArrayList<String>>();
for (int i = 0; i < mNum; i++) {
String[] vertexs = scanner.nextLine().split(" ");
ArrayList<String> v1 = edgeHash.get(vertexs[0]);
ArrayList<String> v2 = edgeHash.get(vertexs[1]);
if (v1 == null) {
v1 = new ArrayList<String>();
edgeHash.put(vertexs[0], v1);
}
if (v2 == null) {
v2 = new ArrayList<String>();
edgeHash.put(vertexs[1], v2);
}
v1.add(vertexs[1]);
v2.add(vertexs[0]);
}
String treeRootKey = edgeHash.keys().nextElement();
removePath(treeRootKey, edgeHash);
System.out.println(removed);
}
}