1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
|
from collections import deque
import sys
n = int(input())
m = int(input())
com = []
graph = [[0]]
visited = [False] * (n+1)
for i in range(m):
co = list(map(int,sys.stdin.readline().split()))
com.append(co)
for j in range(m):
com[j].sort
for k in range(n):
graph.append([])
for l in range(m):
if k+1 in com[l]:
if k+1==com[l][0]:
graph[k+1].append(com[l][1])
else:
graph[k + 1].append(com[l][0])
def bfs(Graph, s, Visited):
queue = deque([s])
Visited[s] = True
count = 0
while queue:
v = queue.popleft()
count+=1
for i in Graph[v]:
if Visited[i] is False:
queue.append(i)
Visited[i] = True
return count
result=bfs(graph, 1, visited)
print(result-1)
|
cs |
'백준' 카테고리의 다른 글
백준 1012번: 유기농 배추 (0) | 2021.05.20 |
---|---|
백준 11724번: 연결 요소의 개수 (0) | 2021.05.20 |
백준 10773번: 제로 (0) | 2021.05.18 |
백준 2108번: 통계학 (0) | 2021.05.18 |
백준 2667번: 단지번호붙이기 (0) | 2021.05.16 |