結果
問題 | No.30 たこやき工場 |
ユーザー | Mao-beta |
提出日時 | 2024-02-29 15:58:31 |
言語 | PyPy3 (7.3.15) |
結果 |
AC
|
実行時間 | 62 ms / 5,000 ms |
コード長 | 2,511 bytes |
コンパイル時間 | 212 ms |
コンパイル使用メモリ | 82,344 KB |
実行使用メモリ | 72,008 KB |
最終ジャッジ日時 | 2024-09-29 12:43:43 |
合計ジャッジ時間 | 1,698 ms |
ジャッジサーバーID (参考情報) |
judge2 / judge4 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 42 ms
56,024 KB |
testcase_01 | AC | 43 ms
57,204 KB |
testcase_02 | AC | 41 ms
57,924 KB |
testcase_03 | AC | 40 ms
57,948 KB |
testcase_04 | AC | 42 ms
56,272 KB |
testcase_05 | AC | 40 ms
56,564 KB |
testcase_06 | AC | 40 ms
57,292 KB |
testcase_07 | AC | 41 ms
56,292 KB |
testcase_08 | AC | 42 ms
58,400 KB |
testcase_09 | AC | 42 ms
57,156 KB |
testcase_10 | AC | 62 ms
72,008 KB |
testcase_11 | AC | 42 ms
57,284 KB |
testcase_12 | AC | 41 ms
56,612 KB |
testcase_13 | AC | 41 ms
57,928 KB |
testcase_14 | AC | 43 ms
57,476 KB |
testcase_15 | AC | 42 ms
58,052 KB |
testcase_16 | AC | 43 ms
58,588 KB |
ソースコード
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()