https://www.acmicpc.net/problem/11725
내 풀이
from collections import deque
import sys
input = sys.stdin.readline
N = int(input())
graph=[[] for _ in range(N+1)]
answer=[]
for i in range(N-1):
a,b = map(int,input().split())
graph[a].append(b)
graph[b].append(a)
def solution():
q = deque([(1)])
visited = [0] * (N+1)
visited[1] = 1
while q:
a = q.popleft()
for node in graph[a]:
if visited[node] == 0:
visited[node] =1
answer.append((node,a))
q.append(node)
solution()
answer.sort(key=lambda x: x[0])
for i in answer:
print(i[1])
다른사람의 풀이
import sys
sys.setrecursionlimit(10**9)
N = int(sys.stdin.readline())
x = [[] for i in range(N+1)]
y = [0 for i in range(N+1)]
for i in range(N-1):
a,b = map(int,sys.stdin.readline().split())
x[a].append(b)
x[b].append(a)
def DFS(start,x,y):
for i in x[start]:
if y[i] == 0:
y[i] = start
DFS(i,x,y)
DFS(1,x,y)
for i in range(2,N+1):
print(y[i])
[출처] 코린탈출기(8) - 백준 11725번 (파이썬)|작성자 onboard
'취준 > 백준' 카테고리의 다른 글
1389 - 파이썬 (0) | 2022.08.23 |
---|---|
13460 - 파이썬 (0) | 2022.08.22 |
2468 - 파이썬 (0) | 2022.08.16 |
14502 - 파이썬 (0) | 2022.08.03 |
16326 - 파이썬 (0) | 2022.08.02 |