結果

問題 No.2780 The Bottle Imp
ユーザー lydialitvyak
提出日時 2024-06-09 10:05:15
言語 Python3
(3.13.1 + numpy 2.2.1 + scipy 1.14.1)
結果
WA  
実行時間 -
コード長 1,763 bytes
コンパイル時間 283 ms
コンパイル使用メモリ 12,672 KB
実行使用メモリ 57,008 KB
最終ジャッジ日時 2024-12-29 16:53:35
合計ジャッジ時間 9,496 ms
ジャッジサーバーID
(参考情報)
judge2 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2 WA * 2
other AC * 13 WA * 3 RE * 24
権限があれば一括ダウンロードができます

ソースコード

diff #

def create_graph(N, connections):
    graph = [[] for _ in range(N)]
    for i, conn in enumerate(connections):
        M = conn[0]
        if M > 0:
            graph[i] = conn[1:]
    return graph


def dfs(graph, v, visited, stack=None):
    visited[v] = True
    for neighbor in graph[v]:
        if not visited[neighbor - 1]:
            dfs(graph, neighbor - 1, visited, stack)
    if stack is not None:
        stack.append(v)


def transpose_graph(graph):
    transposed = [[] for _ in range(len(graph))]
    for v in range(len(graph)):
        for neighbor in graph[v]:
            transposed[neighbor - 1].append(v + 1)
    return transposed


def kosaraju(N, graph):
    visited = [False] * N
    stack = []

    # Step 1: Fill vertices in stack according to their finishing times
    for i in range(N):
        if not visited[i]:
            dfs(graph, i, visited, stack)

    # Step 2: Transpose the graph
    transposed = transpose_graph(graph)

    # Step 3: Process all vertices in order defined by Stack
    visited = [False] * N
    while stack:
        v = stack.pop()
        if not visited[v]:
            component_stack = []
            dfs(transposed, v, visited, component_stack)
            if len(component_stack) == N:
                return True
    return False


def can_all_receive(N, connections):
    graph = create_graph(N, connections)
    return "Yes" if kosaraju(N, graph) else "No"


# メイン関数
if __name__ == "__main__":
    import sys

    input = sys.stdin.read
    data = input().strip().split("\n")

    N = int(data[0].strip())
    connections = []
    for i in range(1, N + 1):
        connections.append(list(map(int, data[i].strip().split())))

    result = can_all_receive(N, connections)
    print(result)
0