[BOJ][๐ŸŸก5][๋ฐฑ์ค€#02660] ํšŒ์žฅ๋ฝ‘๊ธฐ

์ž‘์„ฑ:    

์—…๋ฐ์ดํŠธ:

์นดํ…Œ๊ณ ๋ฆฌ:

ํƒœ๊ทธ: , , ,

๋ฌธ์ œ ์ถœ์ฒ˜

BAEKJOON Online Judge #2660


๋ฌธ์ œ

์›”๋“œ์ปต ์ถ•๊ตฌ์˜ ์‘์›์„ ์œ„ํ•œ ๋ชจ์ž„์—์„œ ํšŒ์žฅ์„ ์„ ์ถœํ•˜๋ ค๊ณ  ํ•œ๋‹ค. ์ด ๋ชจ์ž„์€ ๋งŒ๋“ค์–ด์ง„์ง€ ์–ผ๋งˆ ๋˜์ง€ ์•Š์•˜๊ธฐ ๋•Œ๋ฌธ์— ํšŒ์› ์‚ฌ์ด์— ์„œ๋กœ ๋ชจ๋ฅด๋Š” ์‚ฌ๋žŒ๋„ ์žˆ์ง€๋งŒ, ๋ช‡ ์‚ฌ๋žŒ์„ ํ†ตํ•˜๋ฉด ๋ชจ๋‘๊ฐ€ ์„œ๋กœ ์•Œ ์ˆ˜ ์žˆ๋‹ค. ๊ฐ ํšŒ์›์€ ๋‹ค๋ฅธ ํšŒ์›๋“ค๊ณผ ๊ฐ€๊นŒ์šด ์ •๋„์— ๋”ฐ๋ผ ์ ์ˆ˜๋ฅผ ๋ฐ›๊ฒŒ ๋œ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด ์–ด๋Š ํšŒ์›์ด ๋‹ค๋ฅธ ๋ชจ๋“  ํšŒ์›๊ณผ ์นœ๊ตฌ์ด๋ฉด, ์ด ํšŒ์›์˜ ์ ์ˆ˜๋Š” 1์ ์ด๋‹ค. ์–ด๋Š ํšŒ์›์˜ ์ ์ˆ˜๊ฐ€ 2์ ์ด๋ฉด, ๋‹ค๋ฅธ ๋ชจ๋“  ํšŒ์›์ด ์นœ๊ตฌ์ด๊ฑฐ๋‚˜ ์นœ๊ตฌ์˜ ์นœ๊ตฌ์ž„์„ ๋งํ•œ๋‹ค. ๋˜ํ•œ ์–ด๋Š ํšŒ์›์˜ ์ ์ˆ˜๊ฐ€ 3์ ์ด๋ฉด, ๋‹ค๋ฅธ ๋ชจ๋“  ํšŒ์›์ด ์นœ๊ตฌ์ด๊ฑฐ๋‚˜, ์นœ๊ตฌ์˜ ์นœ๊ตฌ์ด๊ฑฐ๋‚˜, ์นœ๊ตฌ์˜ ์นœ๊ตฌ์˜ ์นœ๊ตฌ์ž„์„ ๋งํ•œ๋‹ค. 4์ , 5์  ๋“ฑ์€ ๊ฐ™์€ ๋ฐฉ๋ฒ•์œผ๋กœ ์ •ํ•ด์ง„๋‹ค. ๊ฐ ํšŒ์›์˜ ์ ์ˆ˜๋ฅผ ์ •ํ•  ๋•Œ ์ฃผ์˜ํ•  ์ ์€ ์–ด๋–ค ๋‘ ํšŒ์›์ด ์นœ๊ตฌ์‚ฌ์ด์ด๋ฉด์„œ ๋™์‹œ์— ์นœ๊ตฌ์˜ ์นœ๊ตฌ์‚ฌ์ด์ด๋ฉด, ์ด ๋‘์‚ฌ๋žŒ์€ ์นœ๊ตฌ์‚ฌ์ด๋ผ๊ณ  ๋ณธ๋‹ค. ํšŒ์žฅ์€ ํšŒ์›๋“ค ์ค‘์—์„œ ์ ์ˆ˜๊ฐ€ ๊ฐ€์žฅ ์ž‘์€ ์‚ฌ๋žŒ์ด ๋œ๋‹ค. ํšŒ์žฅ์˜ ์ ์ˆ˜์™€ ํšŒ์žฅ์ด ๋  ์ˆ˜ ์žˆ๋Š” ๋ชจ๋“  ์‚ฌ๋žŒ์„ ์ฐพ๋Š” ํ”„๋กœ๊ทธ๋žจ์„ ์ž‘์„ฑํ•˜์‹œ์˜ค.


์ž…๋ ฅ

์ž…๋ ฅ์˜ ์ฒซ์งธ ์ค„์—๋Š” ํšŒ์›์˜ ์ˆ˜๊ฐ€ ์žˆ๋‹ค. ๋‹จ, ํšŒ์›์˜ ์ˆ˜๋Š” 50๋ช…์„ ๋„˜์ง€ ์•Š๋Š”๋‹ค. ๋‘˜์งธ ์ค„ ์ดํ›„๋กœ๋Š” ํ•œ ์ค„์— ๋‘ ๊ฐœ์˜ ํšŒ์›๋ฒˆํ˜ธ๊ฐ€ ์žˆ๋Š”๋ฐ, ์ด๊ฒƒ์€ ๋‘ ํšŒ์›์ด ์„œ๋กœ ์นœ๊ตฌ์ž„์„ ๋‚˜ํƒ€๋‚ธ๋‹ค. ํšŒ์›๋ฒˆํ˜ธ๋Š” 1๋ถ€ํ„ฐ ํšŒ์›์˜ ์ˆ˜๋งŒํผ ๋ถ™์–ด ์žˆ๋‹ค. ๋งˆ์ง€๋ง‰ ์ค„์—๋Š” -1์ด ๋‘ ๊ฐœ ๋“ค์–ด์žˆ๋‹ค.


์ถœ๋ ฅ

์ฒซ์งธ ์ค„์—๋Š” ํšŒ์žฅ ํ›„๋ณด์˜ ์ ์ˆ˜์™€ ํ›„๋ณด์˜ ์ˆ˜๋ฅผ ์ถœ๋ ฅํ•˜๊ณ , ๋‘ ๋ฒˆ์งธ ์ค„์—๋Š” ํšŒ์žฅ ํ›„๋ณด๋ฅผ ์˜ค๋ฆ„์ฐจ์ˆœ์œผ๋กœ ๋ชจ๋‘ ์ถœ๋ ฅํ•œ๋‹ค.


์˜ˆ์ œ

์ž…๋ ฅ

5
1 2
2 3
3 4
4 5
2 4
5 3
-1 -1


์ถœ๋ ฅ

2 3
2 3 4


My Sol

from collections import deque

def BFS(i):
    Q = deque()
    Q.append(i)
    visited = [0]*(V+1)
    visited[i] = 1
    dis = -1

    while Q and (dis <= minV):
        for _ in range(len(Q)):
            s = Q.popleft()
            visited[s] = 1
            for v in rel[s]:
                if visited[v]: continue
                visited[v] = 1
                Q.append(v)
        dis += 1
    return dis


V = int(input())
rel = [[] for _ in range(V+1)]

while True:
    u, v = map(int, input().split())
    if u==v==-1: break
    rel[u].append(v)
    rel[v].append(u)


minV = 50
candis = []

for i in range(1, V+1):
    ret = BFS(i)
    if minV > ret:
        candis = [i]
        minV = ret

    elif minV==ret:
        candis.append(i)

print(minV, len(candis))
print(*sorted(candis))

1๋ฒˆ๋ถ€ํ„ฐ V๋ฒˆ๊นŒ์ง€ ๊ฐ ์‚ฌ๋žŒ์— ๋Œ€ํ•˜์—ฌ ์–ผ๋งˆ๋‚˜ ์งง์€ ๊ด€๊ณ„๋งŒ์— ์ „์ฒด ์ง‘๋‹จ์„ ์•Œ ์ˆ˜ ์žˆ๋Š”์ง€ ๊ทธ ์ตœ๋‹จ๊ฑฐ๋ฆฌ๋ฅผ ์ฐพ๋Š” ๋ฌธ์ œ์˜€๋‹ค. deque์™€ BFS๋ฅผ ์ด์šฉํ•˜์˜€๋‹ค. ์ž…๋ ฅ๋œ ๋‘ ์ˆ˜๊ฐ€ -1์ผ ๋•Œ๊นŒ์ง€ ๊ด€๊ณ„์— ๋Œ€ํ•œ ์ž…๋ ฅ์„ ๋ฐ›์•„ ์ฒ˜๋ฆฌํ•ด์ค€๋‹ค.

์ธ์ ‘ ๊ทธ๋ž˜ํ”„์™€ ์ธ์ ‘ ๋ฆฌ์ŠคํŠธ๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋Š”๋ฐ, ์ˆ˜๊ฐ€ ์ ์–ด์„œ ๋ฆฌ์ŠคํŠธ๋ฅผ ์‚ฌ์šฉํ•˜์˜€๋‹ค. ์‚ฌ๋žŒ์˜ ๋ฒˆํ˜ธ์— ํ•ด๋‹นํ•˜๋Š” ์ธ๋ฑ์Šค๋ฅผ ์‚ฌ์šฉํ•˜๋„๋ก ์‚ฌ๋žŒ ์ˆ˜๋ณด๋‹ค 1๊ฐœ ๋” ๋งŽ๊ฒŒ ๋นˆ ๋ฆฌ์ŠคํŠธ๋กœ ์ดˆ๊ธฐํ™”ํ•˜์˜€๊ณ , ์—ฌ๊ธฐ์— ๊ด€๊ณ„๋˜๋Š” ์‚ฌ๋žŒ์˜ ๋ฒˆํ˜ธ๋ฅผ ์ž…๋ ฅํ•ด ๋„ฃ๋Š” ๊ฒƒ์ด๋‹ค.

๊ทธ๋ฆฌ๊ณ  ๊ฐ ํ›„๋ณด๋งˆ๋‹ค ์ „์ฒด๋ฅผ ์•„๋Š”, ์ฆ‰ Q๊ฐ€ ๋นŒ ๋•Œ๊นŒ์ง€ ์–ผ๋งˆ๋งŒํผ for๋ฌธ์„ ๋ฐ˜๋ณตํ•ด์•ผํ•˜๋Š”์ง€ ์ฒดํฌํ•ด์„œ ๋ฐ˜ํ™˜ํ•˜๋Š”๋ฐ, ์ด ๋ฐ˜ํ™˜๊ฐ’์ด ์ „์—ญ๋ณ€์ˆ˜ minV๋ณด๋‹ค ์ž‘๋‹ค๋ฉด ๊ฐ€์žฅ ์งง์€ ํšŸ์ˆ˜๋งŒ์— ์ „์ฒด๋ฅผ ์•„๋Š” ๋ฐ์— ๋„๋‹ฌํ•œ ํ›„๋ณด์ด๋ฏ€๋กœ ๋‹จ๋… ํ›„๋ณด๊ฐ€ ๋˜์ง€๋งŒ, minV์™€ ๊ฐ™๋‹ค๋ฉด ๋‹จ๋…ํ›„๋ณด์™€ ์ „์ฒด๋ฅผ ์•„๋Š” ๋ฐ์— ๋„๋‹ฌํ•œ ํšŸ์ˆ˜๊ฐ€ ๊ฐ™์œผ๋ฏ€๋กœ ํ›„๋ณด ๋ชฉ๋ก์— ์ถ”๊ฐ€ํ•ด์ค€๋‹ค. ์ด๋ฅผ ๋๊นŒ์ง€ ๋ฐ˜๋ณตํ•˜์—ฌ ํ›„๋ณด ๋ชฉ๋ก์˜ ์ˆ˜์™€ ํšŸ์ˆ˜๋ฅผ ๊ตฌํ•  ์ˆ˜ ์žˆ์—ˆ๋‹ค.


๊ฒฐ๊ณผ

๋งž์•˜์Šต๋‹ˆ๋‹ค!!


๋ชจ๋ฒ”๋‹ต์•ˆ

์ถœ์ฒ˜

N = int(input())
graph = [[] for _ in range(N+1)]
visit = [[0] for _ in range(N+1)]
while True:
    a, b = map(int,input().split())
    if a == -1 and b == -1: break;
    graph[a].append(b)
    graph[b].append(a)

def BFS(c):
    q = [c]
    visit = [-1]*(N+1)
    visit[c] = 0
    while q:
        n = q.pop(0)
        for g in graph[n]:
            if visit[g]==-1:
                q.append(g)
                visit[g] = visit[n] + 1

    return max(visit)

ans = []
for i in range(1,N+1):
    ans.append(BFS(i))
min_v = min(ans)
print(min_v, ans.count(min_v))


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

BFS ๊ณผ์ •์ด ๋„์›€์ด ๋  ๊ฒƒ ๊ฐ™์•„ ๊ฐ€์ ธ์˜จ ํ’€์ด์ด๋‹ค. visited ๋ฐฐ์—ด ์ž์ฒด์— ๊ด€๊ณ„๊นŒ์ง€ ๋„๋‹ฌํ•˜๊ธฐ ์œ„ํ•œ ํšŸ์ˆ˜๋ฅผ ์„ธ์–ด ํ‘œ์‹œํ•˜์˜€๊ณ , ๋ชจ๋“  ์กฐํšŒ๊ฐ€ ๋๋‚˜๋ฉด ์ด visited ๋ฐฐ์—ด์—์„œ ๊ฐ€์žฅ ํฐ ๊ฐ’์„ ๋ฐ˜ํ™˜ํ•œ๋‹ค. ์ด ๊ฐ๊ฐ์„ ๋ชจ๋‘ ans์— ๋„ฃ์–ด ๋ชจ๋“  ์‚ฌ๋žŒ์— ๋Œ€ํ•œ ํšŸ์ˆ˜ ์กฐํšŒ๊ฐ€ ๋๋‚˜๋ฉด ans ๋ฐฐ์—ด์—์„œ ๊ฐ€์žฅ ์ž‘์€ ๊ฐ’์„ ์ฐพ์•„ ์ด๋ฅผ ๊ฐ€์ง„ ์ธ๋ฑ์Šค ๋ฒˆํ˜ธ๋“ค์„ ํ†ตํ•ด ํ›„๋ณด ๋ฒˆํ˜ธ๋ฅผ ์ถœ๋ ฅํ•˜๊ณ , count()๋ฅผ ํ•ด์„œ ํ›„๋ณด์˜ ์ˆ˜, ๊ทธ๋ฆฌ๊ณ  ํšŸ์ˆ˜๋ฅผ ์ถœ๋ ฅํ•˜๋Š” ๋ฐฉ์‹์ด๋‹ค.

๋Œ“๊ธ€๋‚จ๊ธฐ๊ธฐ