import sys
sys.setrecursionlimit(10**6)
input = sys.stdin.readline
n,m = map(int, input().split())
graph = [[] for i in range(n+1)]
state = [False for _ in range(n+1)]
for i in range(m):
a,b = map(int, input().split())
graph[a].append(b)
graph[b].append(a)
def dfs(node):
state[node]=True
for nxt in graph[node]:
if state[nxt]==False:
state[nxt]=True
dfs(nxt)
count = 0
for i in range(1, n+1):
if state[i] == False: # Start
count += 1 # count 증가
dfs(i) # 재귀 형태로 연결된 모든 노드 방문, 즉 state가 변하지 않는 값의 경우 다음 Loop에서 검출되는 Process
print(count)
댓글남기기