結果

問題 No.30 たこやき工場
ユーザー Mao-betaMao-beta
提出日時 2024-02-29 15:58:31
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 66 ms / 5,000 ms
コード長 2,511 bytes
コンパイル時間 261 ms
コンパイル使用メモリ 81,700 KB
実行使用メモリ 70,804 KB
最終ジャッジ日時 2024-02-29 15:58:34
合計ジャッジ時間 2,472 ms
ジャッジサーバーID
(参考情報)
judge11 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 45 ms
57,696 KB
testcase_01 AC 44 ms
57,696 KB
testcase_02 AC 45 ms
57,696 KB
testcase_03 AC 46 ms
57,696 KB
testcase_04 AC 44 ms
57,696 KB
testcase_05 AC 45 ms
57,696 KB
testcase_06 AC 45 ms
57,696 KB
testcase_07 AC 45 ms
57,696 KB
testcase_08 AC 46 ms
57,696 KB
testcase_09 AC 47 ms
57,696 KB
testcase_10 AC 66 ms
70,804 KB
testcase_11 AC 46 ms
57,696 KB
testcase_12 AC 46 ms
57,696 KB
testcase_13 AC 46 ms
57,696 KB
testcase_14 AC 47 ms
57,696 KB
testcase_15 AC 47 ms
57,696 KB
testcase_16 AC 47 ms
57,696 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
import math
import bisect
from heapq import heapify, heappop, heappush
from collections import deque, defaultdict, Counter
from functools import lru_cache
from itertools import accumulate, combinations, permutations, product

sys.setrecursionlimit(1000000)
MOD = 10 ** 9 + 7
MOD99 = 998244353

input = lambda: sys.stdin.readline().strip()
NI = lambda: int(input())
NMI = lambda: map(int, input().split())
NLI = lambda: list(NMI())
SI = lambda: input()
SMI = lambda: input().split()
SLI = lambda: list(SMI())
EI = lambda m: [NLI() for _ in range(m)]

from collections import deque


def topological_sort(graph):
    """
    BFSによるトポロジカルソート O(V+E)
    :param graph: 隣接リスト
    :return: 長さnならトポソ可能、そうでなければサイクルあり
    """
    n = len(graph)
    dims = [0] * n
    for i, adj in enumerate(graph):
        for goto in adj:
            dims[goto] += 1

    que = deque()
    for i, d in enumerate(dims):
        if d == 0:
            que.append(i)

    res = []
    while que:
        now = que.popleft()
        res.append(now)

        for goto in graph[now]:
            dims[goto] -= 1
            if dims[goto] == 0:
                que.append(goto)

    return res


def adjlist(n, edges, directed=False, in_origin=1):
    if len(edges) == 0:
        return [[] for _ in range(n)]

    weighted = True if len(edges[0]) > 2 else False
    if in_origin == 1:
        if weighted:
            edges = [[x-1, y-1, w] for x, y, w in edges]
        else:
            edges = [[x-1, y-1] for x, y in edges]

    res = [[] for _ in range(n)]

    if weighted:
        for u, v, c in edges:
            res[u].append([v, c])
            if not directed:
                res[v].append([u, c])

    else:
        for u, v in edges:
            res[u].append(v)
            if not directed:
                res[v].append(u)

    return res


def main():
    N = NI()
    M = NI()
    PR = []
    RP = []
    Q = {}
    for _ in range(M):
        p, q, r = NMI()
        p -= 1
        r -= 1
        PR.append([p, r])
        RP.append([r, p])
        Q[(p, r)] = q
    G = adjlist(N, PR, directed=True, in_origin=0)
    RG = adjlist(N, RP, directed=True, in_origin=0)
    topo = topological_sort(G)
    X = [0] * N
    X[N-1] = 1
    for r in topo[::-1]:
        for p in RG[r]:
            X[p] += X[r] * Q[(p, r)]
        if RG[r]:
            X[r] = 0
    for x in X[:-1]:
        print(x)


if __name__ == "__main__":
    main()
0