๋ฌธ์ œ๋ณด๋Ÿฌ๊ฐ€๊ธฐ

๐Ÿ•ต๏ธ‍โ™€๏ธ ๋ฌธ์ œ


ํ•ด์ปค ๊น€์ง€๋ฏผ์€ ์ž˜ ์•Œ๋ ค์ง„ ์–ด๋Š ํšŒ์‚ฌ๋ฅผ ํ•ดํ‚นํ•˜๋ ค๊ณ  ํ•œ๋‹ค. ์ด ํšŒ์‚ฌ๋Š” N๊ฐœ์˜ ์ปดํ“จํ„ฐ๋กœ ์ด๋ฃจ์–ด์ ธ ์žˆ๋‹ค. ๊น€์ง€๋ฏผ์€ ๊ท€์ฐฎ๊ธฐ ๋•Œ๋ฌธ์—, ํ•œ ๋ฒˆ์˜ ํ•ดํ‚น์œผ๋กœ ์—ฌ๋Ÿฌ ๊ฐœ์˜ ์ปดํ“จํ„ฐ๋ฅผ ํ•ดํ‚น ํ•  ์ˆ˜ ์žˆ๋Š” ์ปดํ“จํ„ฐ๋ฅผ ํ•ดํ‚นํ•˜๋ ค๊ณ  ํ•œ๋‹ค.

์ด ํšŒ์‚ฌ์˜ ์ปดํ“จํ„ฐ๋Š” ์‹ ๋ขฐํ•˜๋Š” ๊ด€๊ณ„์™€, ์‹ ๋ขฐํ•˜์ง€ ์•Š๋Š” ๊ด€๊ณ„๋กœ ์ด๋ฃจ์–ด์ ธ ์žˆ๋Š”๋ฐ, A๊ฐ€ B๋ฅผ ์‹ ๋ขฐํ•˜๋Š” ๊ฒฝ์šฐ์—๋Š” B๋ฅผ ํ•ดํ‚นํ•˜๋ฉด, A๋„ ํ•ดํ‚นํ•  ์ˆ˜ ์žˆ๋‹ค๋Š” ์†Œ๋ฆฌ๋‹ค.

์ด ํšŒ์‚ฌ์˜ ์ปดํ“จํ„ฐ์˜ ์‹ ๋ขฐํ•˜๋Š” ๊ด€๊ณ„๊ฐ€ ์ฃผ์–ด์กŒ์„ ๋•Œ, ํ•œ ๋ฒˆ์— ๊ฐ€์žฅ ๋งŽ์€ ์ปดํ“จํ„ฐ๋ฅผ ํ•ดํ‚นํ•  ์ˆ˜ ์žˆ๋Š” ์ปดํ“จํ„ฐ์˜ ๋ฒˆํ˜ธ๋ฅผ ์ถœ๋ ฅํ•˜๋Š” ํ”„๋กœ๊ทธ๋žจ์„ ์ž‘์„ฑํ•˜์‹œ์˜ค.

 

 

 

๐Ÿ’ก ์ž…๋ ฅ


์ฒซ์งธ ์ค„์—, N๊ณผ M์ด ๋“ค์–ด์˜จ๋‹ค. N์€ 10,000๋ณด๋‹ค ์ž‘๊ฑฐ๋‚˜ ๊ฐ™์€ ์ž์—ฐ์ˆ˜, M์€ 100,000๋ณด๋‹ค ์ž‘๊ฑฐ๋‚˜ ๊ฐ™์€ ์ž์—ฐ์ˆ˜์ด๋‹ค. ๋‘˜์งธ ์ค„๋ถ€ํ„ฐ M๊ฐœ์˜ ์ค„์— ์‹ ๋ขฐํ•˜๋Š” ๊ด€๊ณ„๊ฐ€ A B์™€ ๊ฐ™์€ ํ˜•์‹์œผ๋กœ ๋“ค์–ด์˜ค๋ฉฐ, "A๊ฐ€ B๋ฅผ ์‹ ๋ขฐํ•œ๋‹ค"๋ฅผ ์˜๋ฏธํ•œ๋‹ค. ์ปดํ“จํ„ฐ๋Š” 1๋ฒˆ๋ถ€ํ„ฐ N๋ฒˆ๊นŒ์ง€ ๋ฒˆํ˜ธ๊ฐ€ ํ•˜๋‚˜์”ฉ ๋งค๊ฒจ์ ธ ์žˆ๋‹ค.

 

 

 

 

 

๐Ÿ“Œ ์ถœ๋ ฅ


์ฒซ์งธ ์ค„์—, ๊น€์ง€๋ฏผ์ด ํ•œ ๋ฒˆ์— ๊ฐ€์žฅ ๋งŽ์€ ์ปดํ“จํ„ฐ๋ฅผ ํ•ดํ‚นํ•  ์ˆ˜ ์žˆ๋Š” ์ปดํ“จํ„ฐ์˜ ๋ฒˆํ˜ธ๋ฅผ ์˜ค๋ฆ„์ฐจ์ˆœ์œผ๋กœ ์ถœ๋ ฅํ•œ๋‹ค.

 

 

 

 

๐Ÿ™‹‍โ™€๏ธ ํ’€์–ด๋ณด์ž~


A๊ฐ€ B๋ฅผ ์‹ ๋ขฐํ•˜๋Š” ํ˜•์‹์ด๋ฏ€๋กœ ์ž…๋ ฅ์„ x, y๋กœ ๋ฐ›๊ณ  arr[y].append(x)๋กœ ํ•ด์ฃผ์ž.

bfs๋ฅผ ๋Œ๋ฆด ๋•Œ arr[y]์— ์—ฐ๊ฒฐ๋œ ์• ๋“ค์ด vistited ๋œ์ ์ด ์žˆ๋Š”์ง€ ํ™•์ธํ•ด๋ณด๊ณ  ์—†์œผ๋ฉด ๋ฐฉ๋ฌธ์„ ์ฐ๊ณ  ๋ฑ์— ์ฒจ๊ฐ€ํ•ด์ฃผ๋Š” ํ˜•์‹์œผ๋กœ ์ž‘์„ฑ

 

from collections import deque

n, m = map(int, input().split())

arr = [[] for _ in range(n+1)]

for _ in range(m):
    x, y = map(int, input().split())
    arr[y].append(x)

def bfs(v):
    q = deque([v])
    visited = [False] * (n + 1)
    visited[v] = True
    count = 1
    while q:
        v = q.popleft()
        for e in arr[v]:
            if not visited[e]:
                q.append(e)
                visited[e] = True
                count += 1
    return count

result = []
max_value = -1

for i in range(1, n+1):
    c = bfs(i)
    if c > max_value:
        result=[i]
        max_value=c
    elif c == max_value:
        result.append(i)
        max_value=c

for e in result:
    print(e, end=" ")

 

 

์‹œ๊ฐ„ ์ดˆ๊ณผ ๋จธ์„ ์ผ์ด๊ตฌ,,,

+ Recent posts