BOJ - 1325 - 효율적인 해킹

Updated:

import sys
from collections import deque

def BFS(computer, idx, N):
    ret = 0
    dq = deque()
    dq.append(idx)
    visit = [0] * (N + 1)
    visit[idx] = 1
    while dq:
        u = dq.popleft()
        ret += 1
        for w in computer[u]:
            if not visit[w]:
                visit[w] = 1
                dq.append(w)
    return ret

def solution():
    N, M = map(int, sys.stdin.readline().split())
    computer = [[] for _ in range(N + 1)]

    for i in range(M):
        A, B = map(int, sys.stdin.readline().split())
        computer[B].append(A)

    ans = 0
    ret = []
    for i in range(1, N + 1):
        if computer[i]:
            tmp = BFS(computer, i, N)
            if ans <= tmp:
                if ans < tmp:
                    ret = []
                ans = tmp
                ret.append(i)

    print(*ret)

solution()

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

Categories:

Updated:

Leave a comment