[ALGORITHM] BOJ 2606. 바이러스
ALGORITHM Übung - 백준
- 알고리즘 문제 풀이를 통한 코딩 테스트 연습
문제
코드
import sys
# dfs method
def dfs(n):
for i in graph[n]:
if not visited[i]:
# mark visited
visited[i] = True
dfs(i)
if __name__ == "__main__":
n = int(sys.stdin.readline())
c = int(sys.stdin.readline())
# make graph by adjacency list
graph = [[] for _ in range(n+1)]
for _ in range(c):
a,b = map(int, sys.stdin.readline().split())
graph[a].append(b)
graph[b].append(a)
# make list to check whether visited
visited = [False for _ in range(n+1)]
dfs(1)
# delete computer 1
print(visited.count(True)-1)