๋ฐ์ํ
๐ป ๋ฐฑ์ค 2606๋ฒ [๋ฐ์ด๋ฌ์ค]
ํ์ด)
def dfs(graph, v, visited) :
# ๋
ธ๋ ๋ฐฉ๋ฌธ ์ฒ๋ฆฌ
visited[v] = True
# ์ธ์ ๋
ธ๋ ์ํ
for i in graph[v] :
if visited[i] == False :
dfs(graph, i, visited)
n_com,n_pair = int(input()),int(input())
input_lst = [list(map(int,input().split())) for _ in range(n_pair)]
graph = [[] for _ in range(n_com+1)]
for e1,e2 in input_lst :
graph[e1].append(e2)
graph[e2].append(e1)
visited = [False] * len(graph)
dfs(graph,1,visited)
print(visited.count(True)-1)
๋ฉ๋ชจ๋ฆฌ | ์๊ฐ | ์ธ์ด | ์ฝ๋๊ธธ์ด |
28776KB | 76ms | Python 3 | 512B |
728x90
๋ฐ์ํ
'๋ฐฑ์ค & ํ๋ก๊ทธ๋๋จธ์ค > DFS & BFS' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[ํ๋ก๊ทธ๋๋จธ์ค] DFS/BFS level2 ํ๊ฒ๋๋ฒ (0) | 2022.03.24 |
---|---|
[๋ฐฑ์ค 1012] [dfs/bfs] ์ ๊ธฐ๋ ๋ฐฐ์ถ (0) | 2021.04.29 |
[๋ฐฑ์ค 1012] [dfs/bfs] ์ ๊ธฐ๋ ๋ฐฐ์ถ (0) | 2021.04.24 |
[๋ฐฑ์ค 2667] [dfs/bfs] ๋จ์ง๋ฒํธ๋ถ์ด๊ธฐ (0) | 2021.04.23 |
[๋ฐฑ์ค 2178] [dfs/bfs] ๋ฏธ๋กํ์ (0) | 2021.04.23 |
[๋ฐฑ์ค 1260] [dfs/bfs] BFS์ DFS โ (0) | 2021.04.23 |
[๋ฐฑ์ค 2331] [dfs] ๋ฐ๋ณต์์ด (0) | 2021.04.22 |