-
Notifications
You must be signed in to change notification settings - Fork 0
/
15681.py
47 lines (33 loc) · 798 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
35
36
37
38
39
40
41
42
43
44
45
46
47
# 트리와 쿼리
import sys
from collections import defaultdict
sys.setrecursionlimit(10**8)
input = sys.stdin.readline
N, R, Q = map(int, input().split())
trunks = defaultdict(list)
dir_trunks = defaultdict(list)
for _ in range(N - 1):
U, V = map(int, input().split())
trunks[U].append(V)
trunks[V].append(U)
queue = [R]
visit = [0 for _ in range(N + 1)]
while queue:
d = queue.pop()
if visit[d] == 1:
continue
visit[d] = 1
for i in trunks[d]:
if visit[i] == 0:
queue.append(i)
dir_trunks[d].append(i)
sub = [0 for _ in range(N + 1)]
def find_sub(n):
for i in dir_trunks[n]:
sub[n] += find_sub(i)
sub[n] += 1
return sub[n]
find_sub(R)
for _ in range(Q):
U = int(input())
print(sub[U])