๐Ÿ‘ฉโ€๐ŸŽ“๊ณต๋ถ€ - 2024.01.19 ์˜ค๋Š˜์˜ ๋ฐฑ์ค€ ๋ฌธ์ œํ’€์ด

์œ ๋ น๊ฐœยท2024๋…„ 1์›” 19์ผ
0

PS-์•Œ๊ณ ๋ฆฌ์ฆ˜2

๋ชฉ๋ก ๋ณด๊ธฐ
16/34
post-thumbnail

2660 - ํšŒ์žฅ๋ฝ‘๊ธฐ

ํŠธ๋ฆฌ๊ฐ€ ํ•˜๋‚˜ ์ฃผ์–ด์ง€๋ฉด ๋…ธ๋“œ ํ•˜๋‚˜๋ฅผ ์žก๊ณ  ์ด ๋…ธ๋“œ๋ฅผ ํšŒ์›์ด๋ผ๊ณ  ํ•ฉ์‹œ๋‹ค. ์ด ํšŒ์›๊ณผ ์ด์–ด์ง„ ๋…ธ๋“œ๋Š” ํšŒ์›์˜ ์นœ๊ตฌ์ด๊ณ , ํšŒ์›๊ณผ ์ด์–ด์ง„ ๋…ธ๋“œ์˜ ์ด์–ด์ง„ ๋…ธ๋“œ๋Š” ํšŒ์›์˜ ์นœ๊ตฌ์˜ ์นœ๊ตฌ์ž…๋‹ˆ๋‹ค. ํšŒ์›์ด ๋ชจ๋‘์™€ ์นœ๊ตฌ๋ผ๋ฉด (ํ•ด๋‹น ๋…ธ๋“œ๊ฐ€ ๋ชจ๋“  ๋…ธ๋“œ์™€ ์ง์ ‘์ ์œผ๋กœ ์ด์–ด์ ธ์žˆ๋‹ค๋ฉด) ์ ์ˆ˜ 1, ํšŒ์›์˜ ์นœ๊ตฌ์˜ ์นœ๊ตฌ๊ฐ€ ์กด์žฌํ•œ๋‹ค๋ฉด ์ ์ˆ˜2 ... ์ด๋Ÿฐ์”ฉ์œผ๋กœ ๊ฐˆ ๊ฒฝ์šฐ ์ ์ˆ˜๊ฐ€ ์ œ์ผ ์ž‘์€ ํšŒ์›์„ ํšŒ์žฅ์ด๋ผ ์นญํ•ฉ๋‹ˆ๋‹ค. ์ด ํšŒ์žฅ์ด ๋  ์ˆ˜ ์žˆ๋Š” ํšŒ์›์˜ ์ˆ˜์™€ ํšŒ์žฅ์˜ ์ ์ˆ˜, ๊ทธ๋ฆฌ๊ณ  ํšŒ์žฅ ํ›„๋ณด๋“ค์„ ์˜ค๋ฆ„์ฐจ์ˆœ์œผ๋กœ ์ถœ๋ ฅํ•˜๋Š” ๋ฌธ์ œ์ž…๋‹ˆ๋‹ค.

์–ผํ• ๋ณด๋ฉด ๋ณต์žกํ•ด ๋ณด์ด์ง€๋งŒ ์ด ๋ฌธ์ œ์˜ ํ•ต์‹ฌ์€ BFS - LEVEL์„ ๊ตฌํ•˜๋Š” ๊ฒ๋‹ˆ๋‹ค.
DFS์™€ ๊ฐ™์€ ๊ฒฝ์šฐ๋Š” ๋“ค์–ด๊ฐˆ ๋•Œ๋งˆ๋‹ค depth + 1์„ ํ•˜๊ณ  ์žฌ๊ท€๊ฐ€ ๋๋‚  ๋•Œ depth -= 1์„ ํ•ด์„œ ๊นŠ์ด๋ฅผ ์‰ฝ๊ฒŒ ๊ตฌํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
๊ทธ๋Ÿฌ๋‚˜ ์ €ํฌ๊ฐ€ ํ†ต์ƒ์ ์œผ๋กœ BFS์˜ ๊นŠ์ด๋ฅผ ๊ตฌํ•  ๊ฒฝ์šฐ๋Š” ๋งŽ์ง€ ์•Š๊ธฐ์— ๊ดœ์ฐฎ์€ ๋ฌธ์ œ์ธ ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค.


๊ธฐ๋ณธ ์„ ์–ธ

N = int(input())
que = deque()
graph = [[0]*(N+1) for _ in range(N+1)]
visited = [0]*(N+1)
depth = -1

BFS ์‚ฌ์šฉ์„ ์œ„ํ•ด que์™€ ๊ทธ๋ž˜ํ”„ ๋ฐฉ๋ฌธ์—ฌ๋ถ€ ๋“ฑ์„ ๋ฐ›์•„์ค๋‹ˆ๋‹ค.
์ž๊ธฐ์ž์‹ ์€ ๊นŠ์ด๊ฐ€ 0์ด๊ธฐ ๋•Œ๋ฌธ์— ๊นŠ์ด ์ดˆ๊ธฐ๊ฐ’์€ -1๋ถ€ํ„ฐ ์‹œ์ž‘ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค (-1์— 1 ๋”ํ•˜๋ฉด 0์ด๋ฏ€๋กœ).

while True:
    a,b = map(int,input().split())
    graph[a][b] = graph[b][a] = 1
    if a == -1 and b == -1:
        break

์ž…๋ ฅ๊ฐ’์ด ๊ฐ๊ฐ -1,-1์ด ๋‚˜์˜ฌ ๋•Œ๊นŒ์ง€ a์™€ b๋ฅผ ๋ฐ›๊ณ  ๊ทธ๋ž˜ํ”„์— ์ด์–ด์ง„ ๋…ธ๋“œ๋กœ ํ‘œํ˜„ํ•ด์ค์‹œ๋‹ค.

ans = []
for i in range(1,N+1):
    ans.append(bfs(i))
    depth = -1
    visited = [0]*(N+1)

์ด์ œ ๊ฐ ๋…ธ๋“œ(ํšŒ์›)๋งˆ๋‹ค BFS๋ฅผ ๋Œ๋ฆฐ ํ›„ ํ•œ๋ฒˆ ๋Œ๋ ธ์œผ๋ฉด ๋‹ค์‹œ ์จ๋จน์„ ์ˆ˜ ์žˆ๊ฒŒ ๊นŠ์ด์™€ ๋ฐฉ๋ฌธ์—ฌ๋ถ€๋ฅผ ์ดˆ๊ธฐํ™”ํ•ด์ค๋‹ˆ๋‹ค.


BFS

from collections import deque

def bfs(chairman):
    global depth
    que.append(chairman)
    visited[chairman] = 1

deque๋ฅผ import ํ•˜๊ณ  ํƒ์ƒ‰์„ ์‹œ์ž‘ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค.
์ผ๋‹จ ํšŒ์žฅ์ด๋ผ ๋ณ€์ˆ˜๋ฅผ chairman์œผ๋กœ ๋‘๊ณ  ํ์— ์ถ”๊ฐ€ํ•ด์ค€ ๋‹ค์Œ ๋“ค์–ด์˜จ ์ฒซ ๋…ธ๋“œ๋ฅผ ๋ฐฉ๋ฌธ์ฒ˜๋ฆฌ ํ•ด์ฃผ์—ˆ์Šต๋‹ˆ๋‹ค.

    while que:

์ด์ œ ํ๊ฐ€ ์‚ฌ๋ผ์งˆ ๋•Œ ๋™์•ˆ ๋Œ๋ฆฌ๋Š”๊ฑด ํ•ญ์ƒ ํ•˜๋˜ ์ผ์ธ๋ฐ

        for _ in range(len(que)):

์ด๊ฒŒ ์ด ๋ฌธ์ œ์˜ ํ•ต์‹ฌ์ž…๋‹ˆ๋‹ค. ๋“ค์–ด์˜จ que์˜ ๊ธธ์ด๋งŒํผ๋งŒ ์ผ๋‹จ ๋ฐ˜๋ณตํ•ด์„œ ํ•ด๋‹น level์„ ์œ ์ถ”ํ•ด๋‚ด๋„๋ก ํ•ฉ์‹œ๋‹ค.

์˜ˆ๋ฅผ ๋“ค์–ด ๋ฃจํŠธ๋…ธ๋“œ5, ํ•˜์œ„๋…ธ๋“œ 1,2,3์ด ์žˆ๋‹ค๋ฉด 5์ผ๋•Œ len(que)๋Š” 1์ด ๋‚˜์˜ค๊ณ  ํ•œ๋ฒˆ๋งŒ ๋ˆ ํ›„ depth, ๊นŠ์ด๋ฅผ +1 ํ•ด์ฃผ๋Š” ๊ฒ๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ๋‹ค์‹œ 1,2,3์ผ๋•Œ len(que)๋Š” 3์ด ๋‚˜์˜ค๊ณ  ์„ธ๋ฒˆ๋งŒ ๋ˆ ํ›„ depth, ๊นŠ์ด๋ฅผ +1 ํ•ด์ฃผ๋Š” ์”ฉ์œผ๋กœ ํ•˜๋ฉด BFS ํƒ์ƒ‰ ์ค‘์—์„œ๋„ ๊นŠ์ด๋ฅผ ๊ตฌํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

            chairman = que.popleft()
            for i in range(N+1):
                if visited[i] == 0 and graph[chairman][i] == 1:
                    visited[i] = 1
                    que.append(i) 

ํ•ด๋‹น ๋…ธ๋“œ์™€ ์—ฐ๊ฒฐ๋œ ๋…ธ๋“œ๋ฅผ ํƒ์ƒ‰ํ•ด์„œ ๋ฐฉ๋ฌธ์ฒ˜๋ฆฌํ•ด์ฃผ๊ณ  que์— ์ถ”๊ฐ€ํ•˜์—ฌ ๋‹ค์Œ ํƒ์ƒ‰์ง€์ ์œผ๋กœ ์‚ผ์Šต๋‹ˆ๋‹ค.

        depth += 1
    return depth

๊ทธ๋ฆฌ๊ณ  ๋“ค์–ด์˜จ que์˜ ๊ธธ์ด๋งŒํผ์˜ ๋ฐ˜๋ณต์ด ๋๋‚˜๋ฉด depth + 1์„ ํ•ด์ฃผ๊ณ  while๋ฌธ์œผ๋กœ ๋‹ค์‹œ ๋“ค์–ด๊ฐ€ ํƒ์ƒ‰ ํ›„๋ณด ๋…ธ๋“œ๋“ค์„ ํƒ์ƒ‰ํ•ด์ค๋‹ˆ๋‹ค. ์ตœ์ข…์ ์œผ๋กœ depth๋ฅผ return ํ•ด์ฃผ๋ฉด ์ด ํšŒ์›์˜ ์นœ๊ตฌ ์ ์ˆ˜๊ฐ€ ๋‚˜์˜ต๋‹ˆ๋‹ค.


์ถœ๋ ฅ

์ด ํšŒ์žฅ์ด ๋  ์ˆ˜ ์žˆ๋Š” ํšŒ์›์˜ ์ˆ˜์™€ ํšŒ์žฅ์˜ ์ ์ˆ˜, ๊ทธ๋ฆฌ๊ณ  ํšŒ์žฅ ํ›„๋ณด๋“ค์„ ์˜ค๋ฆ„์ฐจ์ˆœ์œผ๋กœ ์ถœ๋ ฅํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.
์•„๊นŒ ans๋ผ๋Š” ๋ฐฐ์—ด์— ํšŒ์›๋งˆ๋‹ค ์นœ๊ตฌ์ ์ˆ˜๋ฅผ ๋‹ค ๋‹ด์•„๋†จ์œผ๋ฏ€๋กœ

print(min(ans),ans.count(min(ans)))

๊ฐ€์žฅ ์ž‘์€ ์ ์ˆ˜์™€ ๊ฐ€์žฅ ์ž‘์€ ์ ์ˆ˜๋ฅผ ๊ฐ€์ง„ ํšŒ์›์˜ ์ˆ˜(ํ›„๋ณด์˜ ์ˆ˜)

for i in range(N):
    if ans[i] == min(ans):
        print(i+1,end=' ')

๊ทธ๋ฆฌ๊ณ  ๊ฐ€์žฅ ์ž‘์€ ์ ์ˆ˜๋ฅผ ๊ฐ€์ง„ ํšŒ์›๋“ค (for๋ฌธ์€ 0๋ถ€ํ„ฐ ์‹œ์ž‘ํ•˜๋ฏ€๋กœ i+1ํ•˜๋ฉด ํ•ด๋‹น ํšŒ์žฅํ›„๋ณด ์ˆ˜๋กœ ์น˜ํ™˜๊ฐ€๋Šฅ)์„ ์ถœ๋ ฅํ•˜๋ฉด ๋ฌธ์ œ ํ•ด๊ฒฐ ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค.




profile
ํ•œ๋ฆผ๋Œ€ํ•™๊ต ์ •๋ณด๊ณผํ•™๋Œ€ 2ํ•™๋…„ ์žฌํ•™์ค‘ / ์œก๊ตฐ ์ •๋ณด๋ณดํ˜ธ๋ณ‘ 22-2๊ธฐ

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