-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEvenTree.py
49 lines (36 loc) · 1.11 KB
/
EvenTree.py
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
# Program use for how many even tree will be there in given tree.
class node:
def __init__(self, data):
self._data = data
self._children = []
def getdata(self):
return self._data
def getchildren(self):
return self._children
def add(self, node):
self._children.append(node)
class tree:
def __init__(self):
self._head = node('header')
def linktohead(self, node):
self._head.add(node)
def countchild(self, node):
a = 0
a += len(node.getchildren())
for child in node.getchildren():
a += self.countchild(child)
return a
n, m = map(int, input().split(" "))
a1 = node(1)
for _ in range(m):
a, b = input().split(" ")
exec("{0} = node({1})".format("a" + a, a))
exec("{0}.add({1})".format("a" + b, "a" + a))
tree = tree()
tree.linktohead(a1)
# testcase
dict1 = {}
for z in range(2, n + 1):
if (eval("tree.countchild(a{})".format(z)) + 1) % 2 == 0:
dict1["a" + str(z)] = eval("tree.countchild(a{})".format(z)) + 1
print(len(dict1))