Notice
Recent Posts
Recent Comments
Link
나의 개발일지
[백준] 11724 연결 요소의 개수 [Python, 파이썬] 본문
- 문제 : https://www.acmicpc.net/problem/11724
- DFS
- 파이썬으로 풀이 할 경우 재귀 깊이 한계가 정해져 있어 새롭게 지정 필요
- sys.setrecursionlimit(10**9)
from collections import defaultdict
import sys
sys.setrecursionlimit(10**9) # 재귀 깊이 한계 지정
input = sys.stdin.readline
n, m = map(int, input().split()) # 정점, 간선
graph = defaultdict(list)
for i in range(m):
u, v = map(int, input().split())
graph[u].append(v)
graph[v].append(u)
visit = [0] * (n+1)
answer = 0
def dfs(node):
for i in graph[node]:
if visit[i] == 0:
visit[i] = 1
dfs(i)
for i in range(1, n+1):
if visit[i] == 0:
visit[i] = 1
dfs(i)
answer += 1
print(answer)
'백준' 카테고리의 다른 글
[백준] 1417 국회의원 선거 [Python, 파이썬] (0) | 2023.09.18 |
---|---|
[백준] 1068 트리 [Python, 파이썬] (0) | 2023.09.15 |
[백준] 26169 세 번 이내에 사과를 먹자 [Python, 파이썬] (0) | 2023.09.15 |
[백준] 1388 바닥 장식 [Python, 파이썬] (0) | 2023.09.14 |
[백준] 16173 점프왕 쩰리(Small) [Python, 파이썬] (0) | 2023.09.14 |
Comments