forked from rusrushal13/hackerrank-1
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patheventreestudy.py
More file actions
47 lines (42 loc) · 1.29 KB
/
eventreestudy.py
File metadata and controls
47 lines (42 loc) · 1.29 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
# Enter your code here. Read input from STDIN. Print output to STDOUT
def main():
nodes_edges = raw_input().split()
nodes = int(nodes_edges[0])
edges = int(nodes_edges[1])
tree = {}
for i in xrange(1,nodes+1):
tree[i] = [[],0]
for i in xrange(2,nodes+1):
edge = raw_input().split()
for j in xrange(2):
edge[j] = int(edge[j])
tree[edge[1]][0].append(edge[0])
p = 1
d = []
f = 0
for i in xrange(1,nodes+1):
if i not in d:
if tree[i][1] == 0:
tree[i][1] = p
d.append(i)
e = tree[i][0]
for j in e:
if j not in d:
tree[j][1] = tree[i][1] + 1
tree[j][0].append(i)
for i in xrange(1,nodes+1):
if tree[i][1] > f:
f = tree[i][1]
count = 0
for i in xrange(f-1,0,-1):
for j in xrange(1,nodes+1):
if tree[j][1] == i:
if len(tree[j][0])%2 == 0:
count = count+1
for k in tree[j][0]:
if k<j:
tree[j][0].remove(k)
tree[k][0].remove(j)
print count
if __name__ == "__main__":
main()