[Python/ํŒŒ์ด์ฌ] [๐Ÿฅˆ3] ๋ฐฑ์ค€ ์•Œ๊ณ ๋ฆฌ์ฆ˜ 2606 - ๋ฐ”์ด๋Ÿฌ์Šค

keyneneยท2022๋…„ 10์›” 31์ผ
0

Python

๋ชฉ๋ก ๋ณด๊ธฐ
17/26
post-custom-banner

๐Ÿ“–[Python/ํŒŒ์ด์ฌ][๐Ÿฅˆ3] ๋ฐฑ์ค€ ์•Œ๊ณ ๋ฆฌ์ฆ˜ 2606 - ๋ฐ”์ด๋Ÿฌ์Šค

๐Ÿ“œ๋ฌธ์ œ



DFS(๊นŠ์ด์šฐ์„ ํƒ์ƒ‰)

  • ์ž„์˜์˜ ํ•œ ๋…ธ๋“œ์—์„œ ์‹œ์ž‘ํ•ด์„œ ๋‹ค์Œ ๋ถ„๊ธฐ๋กœ ๋„˜์–ด๊ฐ€๊ธฐ ์ „์— ํ•ด๋‹น ๋ถ„๊ธฐ๋ฅผ ์™„๋ฒฝํ•˜๊ฒŒ ํƒ์ƒ‰ํ•˜๋Š” ๋ฐฉ๋ฒ•
  • ์ž๊ธฐ ์ž์‹ ์„ ํ˜ธ์ถœํ•˜๋Š” ์žฌ๊ท€/์ˆœํ™˜ ์•Œ๊ณ ๋ฆฌ์ฆ˜์˜ ํ˜•ํƒœ์ž„
  • ๋…ธ๋“œ ๋ฐฉ๋ฌธ ์—ฌ๋ถ€๋ฅผ ๊ฒ€์‚ฌํ•ด๋†”์•ผ ๋ฌดํ•œ๋ฃจํ”„์— ๋น ์งˆ ์œ„ํ—˜์ด ์ค„์–ด๋“ฆ
  • ์ฃผ๋กœ ๋ฐฑํŠธ๋ž˜ํ‚น๊ณผ ํ•จ๊ป˜ ์“ฐ์—ฌ, ๋ชจ๋“  ๋…ธ๋“œ๋ฅผ ๋‹ค ๋ฐฉ๋ฌธํ•˜๋Š” ๊ฒƒ์„ ์ฐจ๋‹จํ•จ

๐Ÿ“•ํ’€์ด๋ฐฉํ–ฅ

๋‚ด ํฌ์ŠคํŒ… : [Python/ํŒŒ์ด์ฌ] [๐Ÿฅˆ2] ๋ฐฑ์ค€ ์•Œ๊ณ ๋ฆฌ์ฆ˜ 11724 - ์—ฐ๊ฒฐ ์š”์†Œ์˜ ๊ฐœ์ˆ˜

์ด์ „์— ํฌ์ŠคํŒ…ํ•œ "์—ฐ๊ฒฐ ์š”์†Œ์˜ ๊ฐœ์ˆ˜"๋ฌธ์ œ์˜ DFS๊ฐœ๋…๊ณผ ํ’€์ด๋ฐฉ๋ฒ•์ด ๋งค์šฐ ํก์‚ฌํ•˜๋‹ค!
๋˜‘๊ฐ™์ด ์ž…๋ ฅ๊ฐ’ ์ €์žฅ, ๋…ธ๋“œ๋ฅผ MAP ํ˜•ํƒœ๋กœ ์ €์žฅํ•˜๊ณ , visited๋ผ๋Š” ๋นˆ list๋ฅผ ๋งŒ๋“ค์–ด,
dfs(i) ํ•จ์ˆ˜๋กœ ๋ฐฉ๋ฌธ๊ฒ€์‚ฌ๋ฅผ ํ•˜๋ฉด์„œ ์ฒซ ๋ฐฉ๋ฌธ์‹œ visited[i]=1 ์ฒดํฌํ•ด์ฃผ๋ฉด ๋œ๋‹ค.
๋‹ค๋งŒ, ์ด์ „ํฌ์ŠคํŒ…๊ณผ ๋‹ฌ๋ผ์ง„ ์ ์€ 1๋ฒˆ ์ปดํ“จํ„ฐ๊ฐ€ ์›œ๋ฐ”์ด๋Ÿฌ์Šค์— ๊ฑธ๋ ธ์„ ๋•Œ๋งŒ ๊ฒ€์‚ฌํ•˜๋ฉด ๋œ๋‹ค.
๊ทธ๋ฆฌ๊ณ  sum(visited)-1์„ ์ถœ๋ ฅํ•ด์ฃผ๋ฉด ๋! (1์ปดํ“จํ„ฐ ์ž์‹ ์€ ๋นผ์•ผํ•˜๋‹ˆ๊นŒ)

๐Ÿ‘‰๐Ÿป ex) n=7์ผ ๋•Œ 1-2-3-4๋งŒ ์—ฐ๊ฒฐ๋˜์–ด ์žˆ์œผ๋ฉด, 5-6-7์€ ์›œ๋ฐ”์ด๋Ÿฌ์Šค ๊ฑธ๋ ธ๋“  ๋ง๋“  ๊ฒ€์‚ฌํ•  ํ•„์š”๊ฐ€ ์—†์Œ!


๐Ÿ“์•Œ๊ณ ๋ฆฌ์ฆ˜ ๊ตฌํ˜„์ˆœ์„œ

  1. n,m๊ณผ node๋ฅผ ์ž…๋ ฅ๋ฐ›์œผ๋ฉด์„œ amap์— mapํ˜•ํƒœ๋กœ ์ €์žฅํ•˜์ž
  2. visited list๋ฅผ 0์œผ๋กœ ์ดˆ๊ธฐํ™”ํ•˜๊ณ  dfs()ํ•จ์ˆ˜๋ฅผ ๋งŒ๋“ค๊ณ , for loof๋กœ amap[1]์˜ ์š”์†Œ(i)๋งŒ
    dfs(i)๋ฅผ ํ˜ธ์ถœํ•˜์ž
    ๐Ÿ‘‰๐Ÿป 1๋ฒˆ์— ์—ฐ๊ฒฐ๋˜์–ด์žˆ๋Š” ์ปดํ“จํ„ฐ๋“ค๋งŒ ํ™•์ธํ•˜๋ฉด, ๋‚˜๋จธ์ง€ ์ปดํ“จํ„ฐ๋Š” ์›œ๋ฐ”์ด๋Ÿฌ์Šค ๊ฑธ๋ฆฌ๋“ ๋ง๋“  ์ƒ๊ด€X
    (for loof๋กœ i๋ฅผ 1~n๊นŒ์ง€ ๋‹ค ๋Œ ํ•„์š”๊ฐ€ ์—†์Œ)

๐Ÿ’ป๊ฒฐ๊ณผ์ฝ”๋“œ

import sys
input = sys.stdin.readline

n = int(input().rstrip())
m = int(input().rstrip())
node = [list(map(int, input().split())) for _ in range(m)]
amap = [[] for _ in range(n+1)]
visited = [0]*(n+1)

for u,v in node:
    amap[u].append(v)
    amap[v].append(u)

def dfs(v):
    visited[v] = 1
    for i in amap[v]:
        if not visited[i]:
            dfs(i)

for i in amap[1]:
    if not visited[i]:
        dfs(i)

print(sum(visited)-1)   #1๋ฒˆ์ปดํ“จํ„ฐ ์ž์‹  ์นด์šดํŒ…์€ ๋นผ์ฃผ๊ธฐ

โœ๏ธ1. n, m, node ์ž…๋ ฅ๋ฐ›์œผ๋ฉด์„œ amap ์ €์žฅ

import sys
input = sys.stdin.readline

n = int(input().rstrip())
m = int(input().rstrip())
node = [list(map(int, input().split())) for _ in range(m)]
amap = [[] for _ in range(n+1)]

#Graph ํ˜•์‹์œผ๋กœ ์ €์žฅ
for u,v in node:
    amap[u].append(v)
    amap[v].append(u)

โœ๏ธ2. dfs()ํ•จ์ˆ˜ ๊ตฌํ˜„๊ณผ for loof์—์„œ ํ˜ธ์ถœ

visited = [0]*(n+1)


def dfs(v):
    visited[v] = 1      #๋ฐฉ๋ฌธ์•ˆํ•œ ๋…ธ๋“œ๋งˆ๋‹ค ๋ฐฉ๋ฌธ๊ธฐ๋ก 0 โ†’ 1 
    for i in amap[v]:   #ํ•ด๋‹น ๋…ธ๋“œ์™€ ์—ฐ๊ฒฐ๋œ ๋…ธ๋“œ๋“ค ๋ฐฉ๋ฌธ๊ฒ€์‚ฌ
        if not visited[i]:
            dfs(i)

for i in amap[1]:       #amap[1] ์š”์†Œ๋งŒ ๊ฒ€์‚ฌํ•˜๋ฉด ๋จ
    if not visited[i]:
        dfs(i)

print(sum(visited)-1)   #1๋ฒˆ์ปดํ“จํ„ฐ ์นด์šดํŒ… ๋นผ๊ธฐ
โ“"for i in amap[1]"๋งŒ ๊ฒ€์‚ฌํ•˜๋Š” ์ด์œ 

์•Œ๊ณ ๋ฆฌ์ฆ˜ ๊ตฌํ˜„์ˆœ์„œ 1๋ฒˆ์—์„œ amap์œผ๋กœ [1]๋ฒˆ ์ปดํ“จํ„ฐ์™€ ์—ฐ๊ฒฐ๋œ ๋…ธ๋“œ๋ฅผ amap[1]์— ์ €์žฅํ•ด๋’€๊ธฐ ๋•Œ๋ฌธ!

amap[1] = 2, 5
amap[2] = 1, 3, 5
...
...
amap[7] = 4

์ด๋ ‡๊ฒŒ ์ธ๋ฑ์‹ฑ ํ•ด๋’€๊ธฐ ๋•Œ๋ฌธ์— amap[1]์€ 1๋ฒˆ์ปดํ“จํ„ฐ์˜ ์—ฐ๊ฒฐ๋…ธ๋“œ๋งŒ ๊ฒ€์‚ฌํ•จ

๐Ÿ“š์ •๋ฆฌ

๋‚ด ํฌ์ŠคํŒ… : [Python/ํŒŒ์ด์ฌ] [๐Ÿฅˆ2] ๋ฐฑ์ค€ ์•Œ๊ณ ๋ฆฌ์ฆ˜ 11724 - ์—ฐ๊ฒฐ ์š”์†Œ์˜ ๊ฐœ์ˆ˜

์ด์ „ ํฌ์ŠคํŒ…์—์„œ ๋‹ค๋ค„๋ดค๋˜ ์œ ํ˜•์˜ DFS ๊ฐœ๋…์ด๋ผ ํ’€์ดํ•˜๊ธฐ ์ˆ˜์›”ํ–ˆ๋‹ค.
์ด์ „ ํฌ์ŠคํŒ…์—์„œ๋„ ์–ธ๊ธ‰ํ•œ๋Œ€๋กœ, DFS ๋ผ๊ณ ํ•ด์„œ dfs()ํ•จ์ˆ˜ ์•ˆ์—์„œ ๋ฌด์กฐ๊ฑด ํ•ด๊ฒฐํ•˜๋ ค๊ณ  ํ•˜์ง€๋ง๊ณ ,

  • DFS๋ฅผ ์–ด๋–ค์‹œ์ ์—์„œ ํ˜ธ์ถœํ• ์ง€,
  • ํ˜ธ์ถœํ–ˆ์„ ๋•Œ๋Š” ๋˜ ์–ด๋–ค๋ถ€๋ถ„์—์„œ return, ์ฆ‰ ์ข…๋ฃŒํ• ์ง€

๋ฅผ ์ƒ๊ฐํ•˜๋ฉด์„œ ์•Œ๊ณ ๋ฆฌ์ฆ˜์„ ์„ค๊ณ„ํ•˜์ž!

profile
keynene
post-custom-banner

0๊ฐœ์˜ ๋Œ“๊ธ€