Skip to content

Commit 45216b5

Browse files
committed
[Silver II] Title: 트리의 부모 찾기, Time: 204 ms, Memory: 125432 KB -BaekjoonHub
1 parent 6c77710 commit 45216b5

File tree

2 files changed

+23
-18
lines changed

2 files changed

+23
-18
lines changed

백준/Silver/11725. 트리의 부모 찾기/README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,15 @@
44

55
### 성능 요약
66

7-
메모리: 125496 KB, 시간: 236 ms
7+
메모리: 125432 KB, 시간: 204 ms
88

99
### 분류
1010

1111
그래프 이론, 그래프 탐색, 트리, 너비 우선 탐색, 깊이 우선 탐색
1212

1313
### 제출 일자
1414

15-
2025년 4월 3일 10:20:13
15+
2025년 6월 18일 13:05:30
1616

1717
### 문제 설명
1818

백준/Silver/11725. 트리의 부모 찾기/트리의 부모 찾기.py

Lines changed: 21 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,23 +2,28 @@
22
import sys
33
input = sys.stdin.readline
44

5-
N = int(input())
5+
n = int(input())
6+
graph = [[] for _ in range(n+1)]
7+
parent = [0] * (n+1)
68

7-
adj = [[] for _ in range(N+1)]
8-
for _ in range(N-1):
9-
A, B = map(int, input().split())
10-
adj[A].append(B)
11-
adj[B].append(A)
9+
for _ in range(n-1):
10+
a, b = map(int, input().split())
11+
graph[a].append(b)
12+
graph[b].append(a)
1213

13-
parent = [0] * (N+1)
14+
def bfs():
15+
q = deque([1])
16+
parent[1] = 1
17+
18+
while q:
19+
now = q.popleft()
20+
for next in graph[now]:
21+
if parent[next] == 0:
22+
parent[next] = now
23+
q.append(next)
1424

15-
q = deque([1])
16-
while q:
17-
node = q.popleft()
18-
for i in adj[node]:
19-
if parent[i] == 0:
20-
parent[i] = node
21-
q.append(i)
25+
bfs()
2226

23-
for i in range(2, N+1):
24-
print(parent[i])
27+
for i in range(2, n+1):
28+
print(parent[i])
29+

0 commit comments

Comments
 (0)