๐Ÿ—๏ธ 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)