結果

問題 No.2780 The Bottle Imp
ユーザー lydialitvyaklydialitvyak
提出日時 2024-06-09 10:05:15
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
WA  
実行時間 -
コード長 1,763 bytes
コンパイル時間 338 ms
コンパイル使用メモリ 12,544 KB
実行使用メモリ 56,884 KB
最終ジャッジ日時 2024-06-09 10:05:25
合計ジャッジ時間 9,391 ms
ジャッジサーバーID
(参考情報)
judge3 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 31 ms
10,880 KB
testcase_01 AC 30 ms
10,752 KB
testcase_02 WA -
testcase_03 WA -
testcase_04 AC 31 ms
10,752 KB
testcase_05 AC 31 ms
10,752 KB
testcase_06 AC 30 ms
10,752 KB
testcase_07 RE -
testcase_08 RE -
testcase_09 RE -
testcase_10 RE -
testcase_11 RE -
testcase_12 AC 518 ms
56,884 KB
testcase_13 AC 533 ms
56,880 KB
testcase_14 RE -
testcase_15 RE -
testcase_16 RE -
testcase_17 RE -
testcase_18 RE -
testcase_19 RE -
testcase_20 RE -
testcase_21 RE -
testcase_22 RE -
testcase_23 RE -
testcase_24 RE -
testcase_25 RE -
testcase_26 RE -
testcase_27 AC 188 ms
33,188 KB
testcase_28 AC 185 ms
33,192 KB
testcase_29 RE -
testcase_30 AC 170 ms
29,536 KB
testcase_31 WA -
testcase_32 AC 65 ms
14,360 KB
testcase_33 RE -
testcase_34 RE -
testcase_35 AC 68 ms
16,568 KB
testcase_36 AC 29 ms
10,752 KB
testcase_37 AC 31 ms
10,880 KB
testcase_38 WA -
testcase_39 RE -
testcase_40 RE -
testcase_41 RE -
testcase_42 WA -
testcase_43 AC 29 ms
10,752 KB
権限があれば一括ダウンロードができます

ソースコード

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