-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path15681.py
34 lines (26 loc) · 821 Bytes
/
15681.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
import sys
from collections import deque
sys.setrecursionlimit(100_003)
input = sys.stdin.readline
HEAD = 0
def solution():
make_tree(r, HEAD)
while len(q_query):
query = q_query.popleft()
print(dp[query])
def make_tree(current: int, parent: int) -> int:
for idx, adjacent in enumerate(adjacents[current]):
if adjacent != parent:
child = adjacent
dp[current] += make_tree(child, current)
return dp[current]
if __name__ == '__main__':
n, r, q = map(int, input().split())
adjacents = [[] for node in range(n+1)]
for edge in range(n-1):
u, v = map(int, input().split())
adjacents[u].append(v)
adjacents[v].append(u)
q_query = deque([int(input()) for query in range(q)])
dp = [1] * (n + 1)
solution()