Skip to content

Commit 0ac6205

Browse files
committed
[23.01.06/Python] BFS
1 parent 63b3581 commit 0ac6205

File tree

1 file changed

+34
-0
lines changed

1 file changed

+34
-0
lines changed

Python_BOJ_2023/24444.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
# BFS
2+
from collections import deque
3+
import sys
4+
5+
input = lambda: sys.stdin.readline().rstrip()
6+
n, m, r = map(int, input().split())
7+
graph = [[] for _ in range(n + 1)]
8+
9+
for _ in range(m):
10+
u, v = map(int, input().split())
11+
graph[u].append(v)
12+
graph[v].append(u)
13+
visited = [0] * (n + 1)
14+
cnt = 1
15+
16+
def bfs():
17+
global cnt
18+
queue = deque()
19+
queue.append((r))
20+
visited[r] = 1
21+
22+
while queue:
23+
now_node = queue.popleft()
24+
graph[now_node].sort()
25+
for next_node in graph[now_node]:
26+
if not visited[next_node]:
27+
cnt += 1
28+
visited[next_node] = cnt
29+
queue.append((next_node))
30+
31+
bfs()
32+
33+
for val in visited[1:]:
34+
print(val)

0 commit comments

Comments
 (0)