> For the complete documentation index, see [llms.txt](https://pyohamen.gitbook.io/til/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://pyohamen.gitbook.io/til/algorithm/boj/11724.md).

# 11724

> <https://www.acmicpc.net/problem/11724>

1. 무방향 그래프는 양쪽에 간선정보를 줘야함
2. 간선이 없는 하나의 노드는 하나의 연결요소

```python
n, m = map(int,input().split())
graph = [[] for _ in range(n + 1)]

# 무방향성은 간선이 주어질 시 양쪽에 graph 정보를 줘야함
for _ in range(m):
    a, b = map(int,input().split())
    graph[a].append(b)
    graph[b].append(a)

# 간선이 없으면 연결요소가 없으니 0 출력
if not m:
    print(0)
    exit()

visited = []
cnt = 0


def dfs(node):
    visited.append(node)

    for i in graph[node]:
        if i not in visited:
            dfs(i)


# 방문되지 않은 모든 노드에 대해서 dfs 수행
for i in range(1, n + 1):
    if i not in visited:
        dfs(i)
        cnt += 1

print(cnt)
```
