티스토리 뷰
https://www.acmicpc.net/problem/11724
import sys
sys.setrecursionlimit(10000)
N, M = map(int, sys.stdin.readline().split())
matrix = [[0] * N for _ in range(N)]
check = [0] * N
cnt = 0
def dfs(n):
check[n] = 1
for i in range(N):
if check[i] == 0 and matrix[n][i] == 1:
dfs(i)
for i in range(M):
u, v = map(int, sys.stdin.readline().split())
matrix[u-1][v-1] = 1
matrix[v-1][u-1] = 1
for i in range(N):
if check[i] == 0:
dfs(i)
cnt += 1
print(cnt)
'Algorithm > Baekjoon' 카테고리의 다른 글
[백준] 6064 : 카잉 달력 - Python (0) | 2020.04.25 |
---|---|
[백준] 6603 : 로또 - Python (0) | 2020.04.24 |
[백준] 1912 : 연속합 - Python (0) | 2020.04.23 |
[백준] 1932 : 정수 삼각형 - Python (0) | 2020.04.21 |
[백준] 3085 : 사탕 게임 - Python (0) | 2020.04.21 |