Skip to content

Commit f4739aa

Browse files
committed
[23.01.07/Python] DFS
1 parent 0ac6205 commit f4739aa

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed

Python_BOJ_2023/24479.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# DFS
2+
import sys
3+
4+
input = lambda: sys.stdin.readline().rstrip()
5+
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+
14+
cnt = 1
15+
visited = [0] * (n + 1)
16+
visited[r] = 1
17+
18+
def dfs(now):
19+
global cnt
20+
graph[now].sort()
21+
for next in graph[now]:
22+
if not visited[next]:
23+
cnt += 1
24+
visited[next] = cnt
25+
dfs(next)
26+
27+
dfs(r)
28+
print(visited)

0 commit comments

Comments
 (0)