DFS/BFS
๋ ๋ฒ์งธ ๋ฌธ์ !
def dfs(graph, start_node, visit):
visit.append(start_node)
# print(start_node, end=' ')
for node in graph[start_node]:
if node not in visit:
dfs(graph, node, visit)
graph = dict()
visit = list()
N = int(input())
nums = int(input())
for _ in range(nums):
x, y = map(int, input().split())
if x in graph:
graph[x].append(y)
else:
graph[x] = [y]
if y in graph:
graph[y].append(x)
else:
graph[y] = [x]
dfs(graph, 1, visit)
print(len(visit)-1)
ํ ๋ฒ์ ํ์๋ค~! ๊ฐ๋จํด ๋ณด์ฌ์ ์ด์ ์ ์์ฑํ dfs
์ฌ๊ท ์ฝ๋๋ฅผ ๊ทธ๋๋ก ๊ฐ์ ธ๋ค ์ฌ์ฉํ๋ค.