Repository navigation
Expand file tree
/
Copy pathEvenTree.java
More file actions
79 lines (66 loc) · 1.51 KB
/
Copy pathEvenTree.java
File metadata and controls
79 lines (66 loc) · 1.51 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
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Scanner;
public class EvenTree {
static int edges = 0;
public static void main(String[] args) throws IOException {
TNode root =null;
Scanner in = new Scanner(System.in);
int [] input = new int[2];
String line = in.nextLine();
String[] result = line.split("\\s+");
for (int i=0; i<2; i++) {
input[i] = Integer.parseInt(result[i]);
}
int M = input[1];
edges = 0;
HashMap<Integer, TNode> V = new HashMap<Integer, TNode>();
TNode v, w;
while((M--)>0){
line = in.nextLine();
result = line.split("\\s+");
for (int i=0; i<2; i++) {
input[i] = Integer.parseInt(result[i]);
}
if(! V.containsKey(input[0])){
V.put(input[0], new TNode(input[0]));
}
w = V.get(input[0]);
if(! V.containsKey(input[1])){
V.put(input[1], new TNode(input[1]));
}
v = V.get(input[1]);
w.parent = v;
v.children.add(w);
if((root == null) || root.parent!= null){
root = v;
}
}
dfs(root);
System.out.print(edges);
}
public static int dfs(TNode v){
if(v == null){
return 0;
}
int rank = 1;
for(TNode w: v.children){
rank += dfs(w);
}
if( rank % 2 == 0 && v.parent!=null){
edges ++;
}
return rank;
}
public static class TNode{
int key;
public TNode parent;
public ArrayList<TNode> children = new ArrayList<TNode> ();
public TNode(int k){
this.key = k;
}
}
}