-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path2606.py
More file actions
42 lines (29 loc) · 732 Bytes
/
2606.py
File metadata and controls
42 lines (29 loc) · 732 Bytes
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
42
import copy
n = int(input())
m = int(input())
if n<1:
print('0')
elif m<1:
print('1')
else:
map = []
row = [0]*n
for i in range(n):
new_row = copy.deepcopy(row)
new_row[i] = 1
map.append(new_row)
for i in range(m):
d = input().strip().split()
map[int(d[0])-1][int(d[1])-1] = 1
map[int(d[1])-1][int(d[0])-1] = 1
queue = [0]
visited = []
while len(queue) != 0:
now = queue.pop(0)
if now in visited:
continue
visited.append(now)
for i in range(n):
if i != now and map[now][i] == 1 and not(i in visited):
queue.append(i)
print(len(visited)-1)