목차

그래프의 싱크

ps
링크acmicpc.net/…
출처BOJ
문제 번호6543
문제명그래프의 싱크
레벨플래티넘 4
분류

SCC

시간복잡도O(V+E)
인풋사이즈V<=5000, E<=100000
사용한 언어Python
제출기록35176KB / 172ms
최고기록172ms
해결날짜2022/10/14

풀이

코드

"""Solution code for "BOJ 6543. 그래프의 싱크".

- Problem link: https://www.acmicpc.net/problem/6543
- Solution link: http://www.teferi.net/ps/problems/boj/6543

Tags: [SCC]
"""

from teflib import graph as tgraph


def main():
    while (line := input()) != '0':
        n, m = [int(x) for x in line.split()]  # pylint: disable=unused-variable
        graph = [[] for _ in range(n)]
        edges = [int(x) for x in input().split()]
        for v, w in zip(edges[::2], edges[1::2]):
            graph[v - 1].append(w - 1)

        con_graph, scc = tgraph.condensation_graph(graph)
        answer = []
        for successor_u, scc_u in zip(con_graph, scc):
            if not successor_u:
                answer.extend(scc_u)
        print(*(u + 1 for u in sorted(answer)))


if __name__ == '__main__':
    main()