-
Notifications
You must be signed in to change notification settings - Fork 3
/
count_nodes.py
48 lines (36 loc) · 1.07 KB
/
count_nodes.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
from collections import defaultdict, deque
class Graph:
def __init__(self):
self.graph = defaultdict(list)
def add_edge(self, u, v):
self.graph[u].append(v)
self.graph[v].append(u)
def bfs(self, s, x):
visited = [False] * (max(self.graph) + 1)
level = [0] * (max(self.graph) + 1)
queue = deque()
for i in range((max(self.graph) + 1)):
visited[i] = False
level[i] = 0
visited[s] = True
queue.append(s)
level[s] = 0
while len(queue) > 0:
s = queue.popleft()
for i in self.graph[s]:
if not visited[i]:
level[i] = level[s] + 1
visited[i] = True
queue.append(i)
count = 0
for i in range((max(self.graph) + 1)):
if level[i] == x - 1:
count += 1
return count
N = int(input())
G = Graph()
for i in range(N - 1):
a, b = list(map(int, input().split()))
G.add_edge(a, b)
x = int(input())
print(G.bfs(1, x))