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
| from collections import deque
import sys
input = sys.stdin.readline
N = int(input())
graph = [[] for _ in range(N+1)]
visited = [False] * (N+1)
parents = [1] * (N+1)
for _ in range(N-1):
u, v = map(int, input().split())
graph[u].append(v)
graph[v].append(u)
queue = deque([1])
visited[1] = True
while queue:
node = queue.popleft()
for next in graph[node]:
if not visited[next]:
queue.append(next)
visited[next] = True
else:
parents[node] = next
for parent in parents[2:]:
print(parent)
|