Skip to content

Commit dcb97aa

Browse files
Merge pull request #729 from gmlrude/main
[박희경] 116차 라이브 코테 제출
2 parents 95652a6 + 4cdf80e commit dcb97aa

2 files changed

Lines changed: 58 additions & 0 deletions

File tree

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
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)
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
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)

0 commit comments

Comments
 (0)