File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ import sys
2+ from collections import *
3+
4+ input = sys .stdin .readline
5+
6+ t = int (input ())
7+
8+ for _ in range (t ):
9+ n = int (input ())
10+ tree = defaultdict (list )
11+ for _ in range (n - 1 ):
12+ a , b = map (int , input ().split ())
13+ tree [b ] = a
14+ x , y = map (int , input ().split ())
15+
16+ x_roots = set ()
17+ while x in tree :
18+ x_roots .add (x )
19+ x = tree [x ]
20+ x_roots .add (x )
21+
22+ while y not in x_roots :
23+ y = tree [y ]
24+
25+ print (y )
Original file line number Diff line number Diff line change 1+ import sys
2+ from collections import *
3+
4+ input = sys .stdin .readline
5+
6+ n , t = map (int , input ().split ())
7+ holds = set ()
8+ y_dict = defaultdict (list )
9+
10+ for _ in range (n ):
11+ x , y = map (int , input ().split ())
12+ holds .add ((x , y ))
13+ y_dict [y ].append (x )
14+
15+ visited = set ()
16+
17+ q = deque ([(0 , 0 , 0 )])
18+ while q :
19+ x , y , cnt = q .popleft ()
20+ if y == t :
21+ print (cnt )
22+ break
23+
24+ for ny in range (y - 2 , y + 3 ):
25+ if ny not in y_dict :
26+ continue
27+ for nx in y_dict [ny ]:
28+ if abs (nx - x ) <= 2 and (nx , ny ) in holds and (nx , ny ) not in visited :
29+ visited .add ((nx , ny ))
30+ q .append ((nx , ny , cnt + 1 ))
31+
32+ else :
33+ print (- 1 )
You can’t perform that action at this time.
0 commit comments