import sys input = sys.stdin.readline def check(num): visited[num] = True stack = [num] while stack: node = stack.pop(0) for next_node in tree[node]: if tree[node][next_node] == 1: if not visited[next_node]: visited[next_node] = True stack.append(next_node) tree[node][next_node] = 0 tree[next_node][node] = 0 else: return False return True tc = 1 while True: N,M = map(int,input().split()) if N+M == 0: break parent_cnt = [0]*(N+1) tree = [{} for _ in range(N+1)] for _ in range(M): x,y = map(int,input().split()) tree[x][y] = 1 tree[y][x] = 1 cnt = 0 visited = [False]*(N+1) for num in range(1,N+1): if not visited[num]: if check(num): cnt += 1 if cnt == 0: print(f'Case {tc}: No trees.') elif cnt == 1: print(f'Case {tc}: There is one tree.') else: print(f'Case {tc}: A forest of {cnt} trees.') tc += 1
'알고리즘 > 백준_복기_미완료' 카테고리의 다른 글
[BOJ/백준] 16954 움직이는 미로 탈출 (0) | 2021.05.06 |
---|---|
[BOJ/백준] 16637 괄호 추가하기 (0) | 2021.05.06 |
[BOJ/백준] 16400 소수 화폐 (0) | 2021.05.05 |
[BOJ/백준] 15961 회전 초밥 (0) | 2021.05.05 |
[BOJ/백준] 14601 샤워실 바닥 깔기(Large) (0) | 2021.05.05 |