-
Notifications
You must be signed in to change notification settings - Fork 0
/
bj1260.py
43 lines (37 loc) · 860 Bytes
/
bj1260.py
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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
import sys
# dfs 함수
def dfs(idx):
global visted
visted[idx] = True
print(idx, end= " ")
for next in range(1, n +1):
if not visted[next] and graph[idx][next]:
dfs(next)
# bfs 함수
def bfs():
global q, visted
while q:
cur = q.pop(0)
print(cur, end= " ")
for next in range(1, n + 1):
if not visted[next] and graph[cur][next]:
visted[next] = True
q.append(next)
# 0 입력 및 초기화
input = sys.stdin.readline
n,m,v = map(int, input().split())
graph = [[False] * (n + 1) for _ in range(n+1)]
visted = [False] * (n + 1)
# 1. graph 정보 입력
for _ in range(m):
a,b = map(int, input().split())
graph[a][b] = True
graph[b][a] = True
# 2. dfs
dfs(v)
print()
# 3. bfs
visted = [False] * (n + 1)
q = [v]
visted[v] = True
bfs()