[BOJ] 2606 - 바이러스

suhyun·2022년 4월 14일
0

백준/프로그래머스

목록 보기
15/81

문제 링크

2606-바이러스

문제 설명

입력

  • 전체 컴퓨터의 갯수
  • 네트워크상의 연결된 컴퓨터 쌍의 수
  • 연결된 컴퓨터 쌍

출력

  • 1번 컴퓨터가 바이러스 걸렸을 때 바이러스가 전염될 컴퓨터 갯수

문제 풀이

전에 푼거랑 너무 같은 문제네,,?
전체중에 1번 컴퓨터랑 연결되어 있는 컴퓨터 갯수 세는 문제 = 연결된 링크 갯수 세는 문제

DFS (깊이 우선 탐색) 이용

import sys
input = sys.stdin.readline

def dfs(start, cnt):
    visited[start] = True

    for s in graph[start]:
        if not visited[s]:
            cnt = dfs(s, cnt+1)
    return cnt

computer = int(input())
link = int(input())
graph = [[]for _ in range(computer+1)]
visited = [False for _ in range(computer+1)]
for c in range(link):
    a, b = map(int, input().split())
    graph[a].append(b)
    graph[b].append(a)
cnt = dfs(1, 0)
print(cnt)
profile
꾸준히 하려고 노력하는 편 💻

0개의 댓글