알고리즘/DFS | BFS
파이썬 | 백준 | 2606 | 바이러스
cha-n
2020. 9. 2. 17:59
solution
1. bfs : 84ms
2. dfs 이용
# 2606, 바이러스
import sys
input = sys.stdin.readline
def dfs(x):
visited[x] = 1
global virus
for i in network[x]:
if not visited[i]:
virus += 1
dfs(i)
return virus
n = int(input())
m = int(input())
network = [[] for _ in range(n+1)]
for _ in range(m):
a, b = map(int, input().split())
network[a].append(b)
network[b].append(a)
visited = [0]*(n+1)
virus = 0
print(dfs(1))
29380KB
68ms
문제 출처 https://www.acmicpc.net/problem/2606