https://www.acmicpc.net/problem/2606
2606번: 바이러스
첫째 줄에는 컴퓨터의 수가 주어진다. 컴퓨터의 수는 100 이하이고 각 컴퓨터에는 1번 부터 차례대로 번호가 매겨진다. 둘째 줄에는 네트워크 상에서 직접 연결되어 있는 컴퓨터 쌍의 수가 주어
www.acmicpc.net
dfs나 bfs를 적용하면 되는 문제다.
그래프의 연결된 것이 단방향이 아니라 양방향이라는 점을 인지해야 한다.
N = int(input())
M = int(input())
graph = [[] for _ in range(N + 1)]
for i in range(M):
x, y = map(int, input().split())
graph[x].append(y)
graph[y].append(x)
visited = [0] * (N + 1)
def dfs(a):
if visited[a] == 1:
return
visited[a] = 1
for i in graph[a]:
if visited[a] == 1:
dfs(i)
dfs(1)
#print(visited)
count = 0
for i in visited:
if i == 1:
count += 1
print(count - 1)
#양방향인지 단방향인지 확인하자'알고리즘 > python' 카테고리의 다른 글
| 7576번 토마토(python3) (0) | 2022.01.30 |
|---|---|
| 1012번 유기농 배추(python3) (0) | 2022.01.30 |
| 2178번 미로 탐색(python3) (0) | 2022.01.29 |
| 2667번 단지번호붙이기(python3) (0) | 2022.01.29 |
| 18405번 경쟁적 전염(python3) (0) | 2022.01.27 |