結果

問題 No.30 たこやき工場
ユーザー lloyzlloyz
提出日時 2023-08-27 15:02:46
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
AC  
実行時間 23 ms / 5,000 ms
コード長 732 bytes
コンパイル時間 700 ms
コンパイル使用メモリ 10,744 KB
実行使用メモリ 8,664 KB
最終ジャッジ日時 2023-08-27 15:02:48
合計ジャッジ時間 1,943 ms
ジャッジサーバーID
(参考情報)
judge13 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 17 ms
8,452 KB
testcase_01 AC 18 ms
8,576 KB
testcase_02 AC 18 ms
8,612 KB
testcase_03 AC 18 ms
8,448 KB
testcase_04 AC 18 ms
8,596 KB
testcase_05 AC 18 ms
8,560 KB
testcase_06 AC 19 ms
8,532 KB
testcase_07 AC 19 ms
8,552 KB
testcase_08 AC 19 ms
8,540 KB
testcase_09 AC 19 ms
8,476 KB
testcase_10 AC 23 ms
8,560 KB
testcase_11 AC 19 ms
8,500 KB
testcase_12 AC 18 ms
8,640 KB
testcase_13 AC 18 ms
8,568 KB
testcase_14 AC 18 ms
8,664 KB
testcase_15 AC 19 ms
8,576 KB
testcase_16 AC 20 ms
8,540 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

from collections import defaultdict, deque

n = int(input())
m = int(input())
G = defaultdict(list)
INcnt = [0 for _ in range(n)]
OUTs = defaultdict(list)
for _ in range(m):
    p, q, r = map(int, input().split())
    G[r - 1].append((p - 1, q))
    OUTs[p - 1].append(r - 1)
    INcnt[r - 1] += 1

Topo = []
Que = deque([i for i in range(n) if INcnt[i] == 0])
while Que:
    u = Que.popleft()
    Topo.append(u)
    for v in OUTs[u]:
        INcnt[v] -= 1
        if INcnt[v] == 0:
            Que.append(v)

Topo.reverse()
ANS = [0 for _ in range(n)]
ANS[n - 1] = 1
for cp in Topo:
    for np, q in G[cp]:
        ANS[np] += q * ANS[cp]
    if G[cp]:
        ANS[cp] = 0
print(*ANS[:-1], sep='\n')
0