結果

問題 No.30 たこやき工場
ユーザー H3PO4H3PO4
提出日時 2021-02-12 09:25:28
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
AC  
実行時間 24 ms / 5,000 ms
コード長 852 bytes
コンパイル時間 90 ms
コンパイル使用メモリ 10,932 KB
実行使用メモリ 8,740 KB
最終ジャッジ日時 2023-08-23 05:23:54
合計ジャッジ時間 1,254 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 19 ms
8,584 KB
testcase_01 AC 19 ms
8,676 KB
testcase_02 AC 20 ms
8,712 KB
testcase_03 AC 19 ms
8,712 KB
testcase_04 AC 19 ms
8,680 KB
testcase_05 AC 19 ms
8,524 KB
testcase_06 AC 19 ms
8,552 KB
testcase_07 AC 19 ms
8,596 KB
testcase_08 AC 19 ms
8,692 KB
testcase_09 AC 20 ms
8,568 KB
testcase_10 AC 24 ms
8,740 KB
testcase_11 AC 19 ms
8,628 KB
testcase_12 AC 19 ms
8,564 KB
testcase_13 AC 19 ms
8,564 KB
testcase_14 AC 19 ms
8,700 KB
testcase_15 AC 20 ms
8,672 KB
testcase_16 AC 20 ms
8,596 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

from collections import defaultdict, deque


def topological_sort(N, edges):
    outs = defaultdict(list)
    ins = defaultdict(int)
    for v1, v2 in edges:
        outs[v1].append(v2)
        ins[v2] += 1

    q = deque(v1 for v1 in range(N) if ins[v1] == 0)
    while q:
        v1 = q.popleft()
        yield v1
        for v2 in outs[v1]:
            ins[v2] -= 1
            if ins[v2] == 0:
                q.append(v2)


N = int(input())
M = int(input())
edges = []
material = [[] for _ in range(N)]
for _ in range(M):
    p, q, r = map(int, input().split())
    p -= 1
    r -= 1
    edges.append((p, r))
    material[r].append((p, q))

ans = [0] * N
ans[N - 1] = 1
for x in reversed(list(topological_sort(N, edges))):
    for p, q in material[x]:
        ans[p] += q * ans[x]
    if material[x]:
        ans[x] = 0
print(*ans[:-1], sep='\n')
0