結果

問題 No.30 たこやき工場
ユーザー nagitaosunagitaosu
提出日時 2020-03-13 16:42:19
言語 Python3
(3.11.6 + numpy 1.26.0 + scipy 1.11.3)
結果
AC  
実行時間 22 ms / 5,000 ms
コード長 1,868 bytes
コンパイル時間 92 ms
コンパイル使用メモリ 11,160 KB
実行使用メモリ 8,848 KB
最終ジャッジ日時 2023-08-23 05:21:56
合計ジャッジ時間 1,279 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 19 ms
8,760 KB
testcase_01 AC 20 ms
8,828 KB
testcase_02 AC 20 ms
8,768 KB
testcase_03 AC 20 ms
8,708 KB
testcase_04 AC 19 ms
8,848 KB
testcase_05 AC 19 ms
8,700 KB
testcase_06 AC 20 ms
8,700 KB
testcase_07 AC 19 ms
8,616 KB
testcase_08 AC 19 ms
8,716 KB
testcase_09 AC 19 ms
8,780 KB
testcase_10 AC 22 ms
8,752 KB
testcase_11 AC 19 ms
8,676 KB
testcase_12 AC 19 ms
8,744 KB
testcase_13 AC 19 ms
8,768 KB
testcase_14 AC 20 ms
8,780 KB
testcase_15 AC 20 ms
8,680 KB
testcase_16 AC 20 ms
8,708 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#!/usr/bin/env python3
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10**6)
from collections import deque

class DirectedGraph:
    def __init__(self, adj):
        self.n = len(adj)
        self.adj = adj
        self.is_asyclic = False
        self.max_path_len = None

    def topological_sort(self):
        indegree = [0] * self.n
        for vs in self.adj:
            for dest, c in vs:
                indegree[dest] += 1
        zero_v = []
        for v, indeg in enumerate(indegree):
            if indeg == 0:
                zero_v.append(v)
        max_path_len = 1
        tp_sorted = []
        to_be_added = []
        while True:
            while zero_v:
                v = zero_v.pop()
                tp_sorted.append(v)
                for dest, c in self.adj[v]:
                    indegree[dest] -= 1
                    if indegree[dest] == 0:
                        to_be_added.append(dest)
            if len(to_be_added) > 0:
                zero_v.extend(to_be_added)
                to_be_added = []
                max_path_len += 1
            else:
                break
        if len(tp_sorted) == self.n:
            self.is_asyclic = True
            self.max_path_len = max_path_len
            return tp_sorted
        else:
            self.is_asyclic = False
            return None

n = int(input())
m = int(input())
edge = [[] for _ in range(n)]
for _ in range(m):
    p, q, r = [int(item) for item in input().split()]
    p -= 1; r -= 1
    edge[r].append((p, q))
DG = DirectedGraph(edge)
ret = DG.topological_sort()

ans = [0] * n
ans[-1] = 1
visited = set()
for v in ret:
    visited.add(v)
    dest = True
    for nv, c in edge[v]:
        if nv in visited:
            continue
        ans[nv] += ans[v] * c
        dest = False
    if not dest:
        ans[v] = 0
for item in ans[:-1]:
    print(item)
0