Skip to content

Commit ffdac8d

Browse files
authored
#21 : 2606_바이러스_DFS
1 parent 0d64782 commit ffdac8d

File tree

1 file changed

+38
-0
lines changed

1 file changed

+38
-0
lines changed
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
#2023-05-11-Week5-과제
2+
#2606_바이러스
3+
4+
'''
5+
입력
6+
7+
7 총 컴퓨터 수
8+
6 컴퓨터 간 연결된 선 개수
9+
1 2 1번컴퓨터와 2번컴퓨터 연결
10+
2 3
11+
...
12+
4 7
13+
'''
14+
15+
#dfs
16+
import sys
17+
18+
vertex = int(sys.stdin.readline()) # 컴퓨터 개수
19+
edge = int(sys.stdin.readline()) # 연결선 개수
20+
21+
graph = [[] for _ in range(vertex + 1)] # 그래프
22+
visited = [0] * (vertex + 1) # 1번 인덱스부터 사용
23+
24+
for i in range(edge): # 그래프 생성
25+
a, b = map(int, sys.stdin.readline().split())
26+
graph[a].append(b) # a 에 b 연결
27+
graph[b].append(a) # b 에 a 연결
28+
29+
30+
def dfs(node):
31+
visited[node] = 1
32+
for i in graph[node]:
33+
if visited[i] == 0:
34+
dfs(i)
35+
36+
37+
dfs(1) # 1번 컴퓨터부터 시작
38+
print(sum(visited) - 1)

0 commit comments

Comments
 (0)