DevKim

[Python] 백준 #2606 바이러스 본문

알고리즘 PS

[Python] 백준 #2606 바이러스

on_doing 2020. 12. 19. 20:47
728x90

www.acmicpc.net/problem/2606

 

2606번: 바이러스

첫째 줄에는 컴퓨터의 수가 주어진다. 컴퓨터의 수는 100 이하이고 각 컴퓨터에는 1번 부터 차례대로 번호가 매겨진다. 둘째 줄에는 네트워크 상에서 직접 연결되어 있는 컴퓨터 쌍의 수가 주어

www.acmicpc.net

정답률이 44%인걸보니 쉬운 문제이구나 생각했다.

토마토 문제를 풀고 푸니 너무 쉬웠다.! 10분만에 풀었다

n=int(input())
m=int(input())
graph={i:[] for i in range(1,n+1)}

for i in range(m):
    x,y=map(int,input().split())
    graph[x].append(y)
    graph[y].append(x)
    
for key in graph:
    graph[key].sort()
    
visited=[0 for i in range(n)]

from collections import deque

cnt=0
que=deque()

que.append(1)
visited[0]=1

while que:
    k=que.popleft()
    
    for i in graph[k]:
        if(visited[i-1]==0): #방문 안했으면
            que.append(i)
            cnt+=1
            visited[i-1]=1
            
print(cnt)
728x90
Comments