-
[백준] 2606번 바이러스(python)Python/BAEKJOON 2022. 1. 23. 17:00728x90
문제)
알고리즘)
- graph에 각 컴퓨터의 연결(인접 리스트 방식) 구서
- DFS 탐색을 하며 1번 컴퓨터와 연결된 컴퓨터 카운트
코드)
#바이러스 import sys input = sys.stdin.readline cnt = int(input()) graph = [[] for _ in range(cnt+1)] visited = [] #방문한 컴퓨터 넘버 test = int(input()) for _ in range(test): index, link = map(int, input().split()) graph[index].append(link) graph[link].append(index) #dfs def dfs(start, graph, visited): for i in graph[start]: if i not in visited: visited.append(i) dfs(i, graph, visited) dfs(1, graph, visited) print(len(visited)-1) #1번컴퓨터를 제외하기 위해 -1
728x90'Python > BAEKJOON' 카테고리의 다른 글
[백준] 1431번 시리얼번호(python) (0) 2022.02.15 [백준] 1260번 DFS와 BFS(python) (0) 2022.01.29 [백준] 8911번 거북이(python) (0) 2022.01.21 [백준] 3048번 개미(python) (0) 2022.01.19 [백준] 2841번 외계인의 기타연주(python) (0) 2022.01.08