๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
4๏ธโƒฃ Python/Problem Solving

[Python Algorithm] ์—ฐ๊ฒฐ ์š”์†Œ์˜ ๊ฐœ์ˆ˜ BOJ #11724

by seolhee2750 2022. 1. 29.
๋ฌธ์ œ

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

 

11724๋ฒˆ: ์—ฐ๊ฒฐ ์š”์†Œ์˜ ๊ฐœ์ˆ˜

์ฒซ์งธ ์ค„์— ์ •์ ์˜ ๊ฐœ์ˆ˜ N๊ณผ ๊ฐ„์„ ์˜ ๊ฐœ์ˆ˜ M์ด ์ฃผ์–ด์ง„๋‹ค. (1 ≤ N ≤ 1,000, 0 ≤ M ≤ N×(N-1)/2) ๋‘˜์งธ ์ค„๋ถ€ํ„ฐ M๊ฐœ์˜ ์ค„์— ๊ฐ„์„ ์˜ ์–‘ ๋์  u์™€ v๊ฐ€ ์ฃผ์–ด์ง„๋‹ค. (1 ≤ u, v ≤ N, u ≠ v) ๊ฐ™์€ ๊ฐ„์„ ์€ ํ•œ ๋ฒˆ๋งŒ ์ฃผ

www.acmicpc.net

 

๋‚ด ๋ฌธ์ œ ํ’€์ด
# N์ด 10000๊นŒ์ง€ ์ž…๋ ฅ๋  ์ˆ˜ ์žˆ์œผ๋ฏ€๋กœ ์•„๋ž˜์™€ ๊ฐ™์ด ๋ฐ”๊ฟ”์ฃผ์–ด์•ผ ํ•จ
import sys
sys.setrecursionlimit(10000)
input = sys.stdin.readline

n, m = map(int, input().split())
connected = [[] for _ in range(n)]
visited = [0] * n
count = 0

for i in range(m):
    a, b = map(int, input().split())
    connected[a-1].append(b-1)
    connected[b-1].append(a-1)

def dfs(x):
    if visited[x] > 0: return
    visited[x] = count

    for i in connected[x]: dfs(i)

for i in range(n):
    if visited[i] == 0 and len(connected[i]) > 0:
        count += 1
        dfs(i)

# ์•„๋ฌด ๊ฒƒ๋„ ์ด์–ด์ง€์ง€ ์•Š์€ ํ˜ผ์ž ๋‚จ์€ ์  ๋˜ํ•œ ํ•˜๋‚˜์˜ ์—ฐ๊ฒฐ ์š”์†Œ๊ฐ€ ๋จ
for i in visited:
    if i == 0: count += 1

print(count)

๐Ÿ‘‰ dfs ๋ฌธ์ œ๋กœ, ๋ชจ๋“  ์ ์„ ๋Œ์•„๊ฐ€๋ฉฐ ๋ช‡ ๊ฐœ์˜ ๊ทธ๋ฃน์œผ๋กœ ๋‚˜๋ˆŒ ์ˆ˜ ์žˆ๋Š”์ง€ ํŒ๋‹จํ•ด ์ฃผ์–ด์•ผ ํ•จ

์•„๋ฌด๊ฒƒ๋„ ์—ฐ๊ฒฐ๋˜์ง€ ์•Š์€ ์ ๋„ ํ•˜๋‚˜์˜ ์—ฐ๊ฒฐ ์š”์†Œ๋กœ ์„ธ์–ด์ฃผ์–ด์•ผ ํ•˜๋Š” ๊ฒƒ์ด ํฌ์ธํŠธ !

 

[ ๋ฐ˜๋ก€ ์ถ”๊ฐ€ ]

6 3

1 3

2 3

3 4

 

[ ๋ณ€์ˆ˜ ์„ค๋ช… ]

  • connected๋Š” ๊ฐ ์ ๋“ค์ด ์–ด๋–ค ์ ๋“ค๊ณผ ์—ฐ๊ฒฐ๋˜์–ด์žˆ๋Š”์ง€ ์ €์žฅํ•ด ์ค„ ๋ฆฌ์ŠคํŠธ์ด๋‹ค.
    ๋”ฐ๋ผ์„œ connected์˜ ์ธ๋ฑ์Šค๋“ค์€ ๋ฆฌ์ŠคํŠธ์˜ ๊ฐ ์ธ๋ฑ์Šค๋Š” ์ ์„ ์˜๋ฏธํ•œ๋‹ค๊ณ  ๋ณด๋ฉด ๋œ๋‹ค.
    ์œ„์˜ ๋ฐ˜๋ก€๋ฅผ ์˜ˆ๋กœ ๋“ค๋ฉด, [[2], [2], [0, 1, 3], [2], [], []] ์ด๋ ‡๊ฒŒ ์ €์žฅ๋œ๋‹ค.
    (๋ฆฌ์ŠคํŠธ๋กœ ์ ๋“ค์„ ๊ณ„์‚ฐํ•  ์˜ˆ์ •์ด๋ฏ€๋กœ ํŽธํ•˜๊ฒŒ '์  - 1'๊ฐ’์œผ๋กœ ์ƒ๊ฐํ–ˆ๋‹ค.)
  • visited๋Š” ๊ฐ ์ ๋“ค์„ ์ฒดํฌํ–ˆ๋Š”์ง€ ๊ฒ€์‚ฌํ•˜๊ณ , ์  ๋งˆ๋‹ค ์–ด๋–ค ๊ทธ๋ฃน์— ์†ํ–ˆ๋Š”์ง€ ํ‘œ์‹œํ•˜๋Š” ๋ฆฌ์ŠคํŠธ์ด๋‹ค.
    dfs ํ•จ์ˆ˜๋กœ ํ•ด๋‹น ์ธ๋ฑ์Šค์— ํ•ด๋‹นํ•˜๋Š” ์ ์„ ์ฒดํฌํ–ˆ๋‹ค๋ฉด count ๋ณ€์ˆ˜ ๊ฐ’์„ ์ €์žฅํ•ด์ฃผ์—ˆ๋‹ค.
  • count ๋ณ€์ˆ˜๋Š” ํ•˜๋‚˜๋กœ ์—ฐ๊ฒฐ๋œ ์ ๋“ค์˜ dfs ํƒ์ƒ‰์ด ๋๋‚  ๋•Œ๋งˆ๋‹ค +1 ํ•ด์ฃผ๋ฏ€๋กœ,
    ๋ช‡ ๊ฐœ์˜ ๋ชจ์ž„์œผ๋กœ ์ด๋ฃจ์–ด์ ธ์žˆ๋Š”์ง€ ๊ตฌ๋ถ„ํ•  ์ˆ˜ ์žˆ๊ฒŒ ํ•œ๋‹ค.

 

[ ๋ฉ”์ธ ์‹คํ–‰ ]

  • ์ฒซ ๋ฒˆ์งธ for๋ฌธ์—์„œ๋Š” connected ๋ฆฌ์ŠคํŠธ์— ์—ฐ๊ฒฐ๋œ ์ ๋“ค์„ ํ‘œ์‹œํ•ด์ฃผ์—ˆ๋‹ค.
  • ๋‘ ๋ฒˆ์งธ for๋ฌธ์—์„œ๋Š” 0๋ถ€ํ„ฐ n-1๊นŒ์ง€ ๋ฐ˜๋ณตํ•˜๋ฉด์„œ ์•Œ๋งž์€ ์กฐ๊ฑด์— dfs ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ–ˆ๋‹ค.
    (ํ•ด๋‹น ์ ์„ ํ•œ ๋ฒˆ๋„ ์ฒดํฌํ•œ ์  ์—†๊ณ , ํ•ด๋‹น ์ ์ด ์—ฐ๊ฒฐ๋œ ์ ์ด ํ•˜๋‚˜๋ผ๋„ ์žˆ์„ ์กฐ๊ฑด)
    ๊ทธ๋ฆฌ๊ณ  dfs ํ•จ์ˆ˜๋ฅผ ์‹คํ–‰ํ•˜๊ธฐ ์ „์—๋Š” count๋ฅผ ์ฆ๊ฐ€์‹œ์ผœ์„œ ์—ฐ๊ฒฐ ์š”์†Œ๋“ค์„ ๊ตฌ๋ถ„ํ•ด์ฃผ์—ˆ๋‹ค.
  • dfs ํ•จ์ˆ˜์—์„œ๋Š” ํ˜ธ์ถœ๋˜๋Š” ์ ๋งˆ๋‹ค ๊ทธ์— ์—ฐ๊ฒฐ๋œ ์ ์„ ๋ชจ๋‘ ํƒ์ƒ‰ํ•  ์ˆ˜ ์žˆ๊ฒŒ ํ–ˆ๋‹ค.
    ๊ทธ๋ฆฌ๊ณ  visited๊ฐ€ 0๋ณด๋‹ค ํด ๊ฒฝ์šฐ return ํ•ด์ฃผ์–ด์„œ ์ค‘๋ณต ํƒ์ƒ‰์„ ํ”ผํ–ˆ๊ณ ,
    ํƒ์ƒ‰ํ•˜๊ฒŒ ๋  ๊ฒฝ์šฐ ํ•ด๋‹น ์ ์˜ ์œ„์น˜์— ํ•ด๋‹นํ•˜๋Š” visited[x]์— count๋ฅผ ๋„ฃ์–ด์คฌ๋‹ค.
  • ์„ธ ๋ฒˆ์งธ for๋ฌธ์€ ์•„๋ฌด ์ ๊ณผ๋„ ์ด์–ด์ง€์ง€ ์•Š์€ ํ˜ผ์ž ๋‚จ์€ ์ ๋“ค์„ ์„ธ๊ธฐ ์œ„ํ•จ์ด๋‹ค.
    visited ๋ฆฌ์ŠคํŠธ๋ฅผ ํƒ์ƒ‰ํ•˜์—ฌ 0์˜ ๊ฐœ์ˆ˜๋ฅผ ์„ธ์–ด์ฃผ์—ˆ๋‹ค.

 

๐Ÿ’ก ํ”ผ๋“œ๋ฐฑ
  • ์ฒ˜์Œ์— input์˜ ๋ฆฌ๋ฏธํŠธ๋ฅผ ๋”ฐ๋กœ ์ง€์ •ํ•ด์ฃผ์–ด์•ผ ํ•˜๋Š”์ง€๋ฅผ ๋ชฐ๋ผ์„œ ๋Ÿฐํƒ€์ž„ ์—๋Ÿฌ๊ฐ€ ๋‚ฌ๋Š”๋ฐ,
    ๊ตฌ๊ธ€๋งํ•ด์„œ ์›์ธ์„ ์ฐพ์„ ์ˆ˜ ์žˆ์—ˆ๋‹ค. ์ด๋ถ€๋ถ„ ๊ณต๋ถ€ํ•ด๋†”์•ผ ํ• ๋“ฏ
  • ๋ฌธ์ œ์—์„œ ์ฃผ์–ด์ง„ ์ผ€์ด์Šค๋“ค๋กœ๋Š” ํ˜ผ์ž ๋‚จ์€ ์—ฐ๊ฒฐ ์š”์†Œ๋ฅผ ์ƒ๊ฐํ•˜๊ธฐ ์–ด๋ ค์›Œ์„œ
    ๊ณ„์† ๋ฐ˜๋ก€๋ฅผ ๋ชป์ฐพ์•˜๋Š”๋ฐ, ๋ฐฑ์ค€ ์งˆ๋ฌธ๊ฒ€์ƒ‰์—์„œ ์•Œ๋งž์€ ๋ฐ˜๋ก€๋ฅผ ์ฐพ์•˜๊ณ ,, ๊ทธ๋ž˜์„œ ์˜ค๋ž˜๊ฑธ๋ ธ๋‹ค.

๋Œ“๊ธ€