https://www.codetree.ai/missions/2/problems/graph-traversal/introduction
코드트리 | 코딩테스트 준비를 위한 알고리즘 정석
국가대표가 만든 코딩 공부의 가이드북 코딩 왕초보부터 꿈의 직장 코테 합격까지, 국가대표가 엄선한 커리큘럼으로 준비해보세요.
www.codetree.ai
import sys
input = sys.stdin.readline
n, m = map(int, input().split(" "))
graph = [[] for _ in range(n+1)]
visited = [0 for _ in range(n+1)]
cnt = 0
for _ in range(m):
a, b = map(int, input().split(" "))
graph[a].append(b)
graph[b].append(a)
def dfs(v):
visited[v] = 1
global cnt
cnt += 1
for n in graph[v]:
if not visited[n]:
dfs(n)
dfs(1)
print(cnt-1)
728x90
반응형
'🔻PS > Codetree' 카테고리의 다른 글
[Codetree] 뿌요뿌요 Python (0) | 2024.07.19 |
---|---|
[Codetree] 안전 지대 Python (0) | 2024.07.19 |
[Codetree] 마을 구분하기 Python (0) | 2024.07.19 |
[Codetree] 두 방향 탈출 가능 여부 판별하기 Python (0) | 2024.07.19 |
[Codetree] 진법 변환 7 Python (0) | 2024.07.17 |