-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path75.subtree.py
More file actions
27 lines (27 loc) · 750 Bytes
/
75.subtree.py
File metadata and controls
27 lines (27 loc) · 750 Bytes
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
import sys
sys.stdin = open('input.txt','r')
T = int(input())
for tc in range(1,T+1):
E, N = map(int,input().split())
leftlist = [0]*(E+2)
rightlist= [0]*(E+2)
inputlist=list(map(int,input().split()))
for idx,val in enumerate(inputlist):
if idx%2 == 0 and 0<=idx<len(inputlist):
if leftlist[val]!=0:
rightlist[val]=inputlist[idx+1]
else:
leftlist[val]=inputlist[idx+1]
cnt=1
st=[]
st.append(N)
while st:
x=st.pop()
if x:
if leftlist[x]:
st.append(leftlist[x])
cnt+=1
if rightlist[x]:
st.append(rightlist[x])
cnt+=1
print('#{}'.format(tc),cnt)