-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathseorim.py
50 lines (37 loc) · 1.04 KB
/
seorim.py
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
43
44
45
46
47
48
49
50
import sys
input = sys.stdin.readline
def bfs(graph, visited, x):
visited[x] = True
queue = []
queue.append(x)
while queue:
curr = queue.pop(0)
for next in graph[curr]:
if not visited[next]:
visited[next] = True
queue.append(next)
graph[next].remove(curr)
else:
return 0
return 1
num = 1
while True:
n, m = map(int, input().split())
if n == 0 and m == 0: break
graph = [[] for _ in range(n+1)]
for _ in range(m):
u, v = map(int, input().split())
graph[u].append(v)
graph[v].append(u)
visited = [False] * (n+1)
cnt = 0
for i in range(1, n+1):
if not visited[i]:
cnt += bfs(graph, visited, i)
if cnt == 0:
print(f"Case {num}: No trees.")
elif cnt == 1:
print(f"Case {num}: There is one tree.")
else:
print(f"Case {num}: A forest of {cnt} trees.")
num += 1