Practice makes perfect!

[백준] 2606 바이러스 - python 본문

Coding test/BFS&DFS

[백준] 2606 바이러스 - python

na0dev 2022. 1. 7. 21:03

문제 링크 : https://www.acmicpc.net/problem/2606

접근 방법 : 1번 컴퓨터에 연결된 모든 컴퓨터를 찾으면 되는 문제이므로 dfs로 접근.

                 ( 현재 나의 위치에서 연결된 브랜치를 모두 방문하고자 할 때 )

 

n = int(input())
con = int(input())

graph = [[] for _ in range(n+1)]
visited = [0]*(n+1)

for _ in range(con):
    a, b = map(int, input().split())
    graph[a].append(b)
    graph[b].append(a)

cnt = -1

def dfs(a):
    visited[a] = 1
    global cnt
    cnt += 1
    
    for b in graph[a]:
        if visited[b] == 0:
            dfs(b)
    
dfs(1)
print(cnt)

'Coding test > BFS&DFS' 카테고리의 다른 글

[백준] 2178 미로 탐색 - python  (0) 2021.06.26
[백준] 1303 전쟁 - python  (0) 2021.06.26
BFS & DFS 문제 구분  (0) 2021.06.26
Comments