-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
5e3faf1
commit 3824c30
Showing
1 changed file
with
50 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
from collections import deque | ||
import sys | ||
def input(): return sys.stdin.readline().strip() | ||
|
||
|
||
test_case = 1 | ||
while True: | ||
N, M = map(int, input().split()) | ||
if N == 0: | ||
break | ||
|
||
edges = [[] for _ in range(N + 1)] | ||
for _ in range(M): | ||
a, b = map(int, input().split()) | ||
edges[a].append(b) | ||
edges[b].append(a) | ||
|
||
answer = 0 | ||
visited = [False] * (N + 1) | ||
for i in range(1, N + 1): | ||
if visited[i]: | ||
continue | ||
visited[i] = True | ||
|
||
is_tree = True | ||
queue = deque([(i, 0)]) | ||
while queue: | ||
here, pre = queue.popleft() | ||
for there in edges[here]: | ||
if there == pre: | ||
continue | ||
|
||
if visited[there]: | ||
is_tree = False | ||
continue | ||
visited[there] = True | ||
|
||
queue.append((there, here)) | ||
|
||
if is_tree: | ||
answer = answer + 1 | ||
|
||
print(f"Case {test_case}: ", end='') | ||
if answer == 0: | ||
print("No trees.") | ||
elif answer == 1: | ||
print("There is one tree.") | ||
else: | ||
print(f"A forest of {answer} trees.") | ||
test_case = test_case + 1 |