🗝️ Algorithm/🟩 백준

🟩 [백준] [Python] 5567번_결혼식

Dbswnstjd 2022. 11. 28. 10:19

문제

https://www.acmicpc.net/problem/5567

 

5567번: 결혼식

예제 1의 경우 2와 3은 상근이의 친구이다. 또, 3과 4는 친구이기 때문에, 4는 상근이의 친구의 친구이다. 5와 6은 친구도 아니고, 친구의 친구도 아니다. 따라서 2, 3, 4 3명의 친구를 결혼식에 초대

www.acmicpc.net

풀이

# 백준 5567번 문제 - 결혼식
from sys import stdin

n = int(stdin.readline().strip())
graph = [[] for _ in range(n + 1)]
visited = [0] * (n + 1)
for _ in range(int(stdin.readline().strip())):
	x, y = map(int, stdin.readline().split())
	graph[y].append(x)
	graph[x].append(y)

cnt = 0
visited[1] = 1
for i in graph[1]:
	if not visited[i]:
		visited[i] = 1
		cnt += 1
	for j in graph[i]:
		if not visited[j]:
			visited[j] = 1
			cnt += 1

print(cnt)