11724

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

  1. ๋ฌด๋ฐฉํ–ฅ ๊ทธ๋ž˜ํ”„๋Š” ์–‘์ชฝ์— ๊ฐ„์„ ์ •๋ณด๋ฅผ ์ค˜์•ผํ•จ

  2. ๊ฐ„์„ ์ด ์—†๋Š” ํ•˜๋‚˜์˜ ๋…ธ๋“œ๋Š” ํ•˜๋‚˜์˜ ์—ฐ๊ฒฐ์š”์†Œ

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)

Last updated