-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreachableroads.py
More file actions
37 lines (32 loc) · 924 Bytes
/
reachableroads.py
File metadata and controls
37 lines (32 loc) · 924 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
#!/usr/bin/env python3
def dfs(node, neighborsOf, visited):
if visited[node]:
return
visited[node] = True
for neighbor in neighborsOf[node]:
dfs(neighbor, neighborsOf, visited)
def bfs(node, neighborsOf, visited):
queue = []
queue.append(node)
visited[node] = True
while queue:
node = queue.pop(0)
for neighbor in neighborsOf[node]:
if not visited[neighbor]:
queue.append(neighbor)
visited[neighbor] = True
for _ in range(int(input())):
m = int(input())
grid = [[] for j in range(m)]
count = -1
for i in range(int(input())):
x, y = map(int, input().split())
grid[x].append(y)
grid[y].append(x)
visited = [False for j in range(m)]
for i in range(m):
if visited[i]:
continue
count += 1
dfs(i, grid, visited)
print(count)