Skip to content

Commit 95652a6

Browse files
Merge pull request #730 from baekhangyeol/main
[백한결] 116차 라이브 코테 제출
2 parents 3d1f12b + b1efbc6 commit 95652a6

2 files changed

Lines changed: 89 additions & 0 deletions

File tree

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
import sys
2+
sys.setrecursionlimit(10**6)
3+
4+
def main():
5+
input = sys.stdin.readline
6+
T = int(input())
7+
8+
for _ in range(T):
9+
N = int(input())
10+
parent = [0] * (N+1)
11+
depth = [0] * (N+1)
12+
tree = [[] for _ in range(N+1)]
13+
isChild = [False] * (N+1)
14+
for _ in range(N-1):
15+
a, b = map(int, input().split())
16+
parent[b] = a
17+
tree[a].append(b)
18+
isChild[b] = True
19+
20+
root = 0
21+
for i in range(1, N+1):
22+
if not isChild[i]:
23+
root = i
24+
break
25+
26+
def dfs(node, d):
27+
depth[node] = d
28+
for child in tree[node]:
29+
dfs(child, d + 1)
30+
31+
dfs(root, 0)
32+
33+
a, b = map(int, input().split())
34+
35+
while depth[a] > depth[b]:
36+
a = parent[a]
37+
while depth[a] < depth[b]:
38+
b = parent[b]
39+
40+
while a != b:
41+
a = parent[a]
42+
b = parent[b]
43+
44+
print(a)
45+
46+
if __name__ == '__main__':
47+
main()
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import sys
2+
from collections import defaultdict, deque
3+
4+
5+
def main():
6+
input = sys.stdin.readline
7+
n, T = map(int, input().split())
8+
9+
stones = set()
10+
yMap = defaultdict(list)
11+
12+
for _ in range(n):
13+
x, y = map(int, input().split())
14+
stones.add((x, y))
15+
yMap[y].append(x)
16+
17+
for y in yMap:
18+
yMap[y].sort()
19+
20+
visited = set()
21+
queue = deque()
22+
queue.append(((0, 0), 0)) # 위치 (x, y), 이동 횟수
23+
24+
while queue:
25+
(x, y), count = queue.popleft()
26+
27+
if y == T:
28+
print(count)
29+
return
30+
31+
for ny in range(y-2, y+3):
32+
if ny < 0 or ny > T:
33+
continue
34+
for nx in range(x-2, x+3):
35+
if (nx, ny) in stones and (nx, ny) not in visited:
36+
visited.add((nx, ny))
37+
queue.append(((nx, ny), count + 1))
38+
39+
print(-1)
40+
41+
if __name__ == '__main__':
42+
main()

0 commit comments

Comments
 (0)