사용자 도구

사이트 도구


ps:problems:boj:6543

그래프의 싱크

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

SCC

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

풀이

  • 문제에서 말하는 '그래프의 싱크'는, 주어진 그래프의 SCC들로부터 condensation graph를 만들었을때 outdegree가 0인 scc들에 포함된 노드들과 동일하다.
  • condensation graph 를 만든 뒤에, outdegree가 0인 scc를 찾기만 하면 되므로, 시간복잡도는 O(V+E)

코드

"""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()

토론

댓글을 입력하세요:
M O L A G
 
ps/problems/boj/6543.txt · 마지막으로 수정됨: 2022/10/14 08:46 저자 teferi