結果
問題 | No.1364 [Renaming] Road to Cherry from Zelkova |
ユーザー | H3PO4 |
提出日時 | 2021-01-22 22:59:43 |
言語 | Python3 (3.12.2 + numpy 1.26.4 + scipy 1.12.0) |
結果 |
WA
|
実行時間 | - |
コード長 | 1,698 bytes |
コンパイル時間 | 524 ms |
コンパイル使用メモリ | 12,800 KB |
実行使用メモリ | 178,840 KB |
最終ジャッジ日時 | 2024-06-08 17:11:48 |
合計ジャッジ時間 | 85,434 ms |
ジャッジサーバーID (参考情報) |
judge2 / judge3 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 1,476 ms
56,612 KB |
testcase_01 | AC | 857 ms
56,364 KB |
testcase_02 | AC | 849 ms
56,352 KB |
testcase_03 | WA | - |
testcase_04 | WA | - |
testcase_05 | WA | - |
testcase_06 | WA | - |
testcase_07 | WA | - |
testcase_08 | WA | - |
testcase_09 | WA | - |
testcase_10 | WA | - |
testcase_11 | WA | - |
testcase_12 | WA | - |
testcase_13 | WA | - |
testcase_14 | WA | - |
testcase_15 | WA | - |
testcase_16 | WA | - |
testcase_17 | WA | - |
testcase_18 | WA | - |
testcase_19 | WA | - |
testcase_20 | WA | - |
testcase_21 | WA | - |
testcase_22 | WA | - |
testcase_23 | AC | 1,112 ms
73,004 KB |
testcase_24 | AC | 1,145 ms
71,476 KB |
testcase_25 | AC | 1,951 ms
131,380 KB |
testcase_26 | TLE | - |
testcase_27 | AC | 2,203 ms
145,524 KB |
testcase_28 | AC | 1,600 ms
111,028 KB |
testcase_29 | AC | 2,131 ms
144,716 KB |
testcase_30 | AC | 1,679 ms
115,260 KB |
testcase_31 | AC | 1,277 ms
87,180 KB |
testcase_32 | AC | 1,898 ms
130,096 KB |
testcase_33 | TLE | - |
testcase_34 | AC | 2,357 ms
158,004 KB |
testcase_35 | AC | 1,784 ms
114,252 KB |
testcase_36 | AC | 1,738 ms
114,308 KB |
testcase_37 | AC | 1,711 ms
106,192 KB |
testcase_38 | WA | - |
testcase_39 | WA | - |
testcase_40 | WA | - |
testcase_41 | WA | - |
testcase_42 | WA | - |
testcase_43 | AC | 2,097 ms
149,784 KB |
testcase_44 | AC | 1,404 ms
87,072 KB |
testcase_45 | AC | 1,995 ms
150,008 KB |
testcase_46 | AC | 888 ms
56,240 KB |
testcase_47 | AC | 892 ms
110,956 KB |
ソースコード
import sys from collections import defaultdict, deque from scipy.sparse.csgraph import connected_components from scipy.sparse import csr_matrix input = sys.stdin.buffer.readline 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) MOD = 10 ** 9 + 7 N, M = map(int, input().split()) edges = [list(map(int, input().split())) for _ in range(M)] # 強連結成分分解により不要部分を削除 edges_set = set((u, v) for u, v, _, _ in edges) edges_set.add((N, 0)) frm, to = [], [] for u, v in edges_set: frm.append(u) to.append(v) matr = csr_matrix(([1] * len(frm), (frm, to)), shape=(N + 1, N + 1)) _, labels = connected_components(matr, connection='strong') l0 = labels[0] if l0 != labels[-1]: print(0);exit() # 弱連結でもない nodes = [i for i, x in enumerate(labels) if x == l0] # 座圧 comp = {x: i for i, x in enumerate(nodes)} N1 = len(comp) succ = [[] for _ in range(N1)] new_edges = set() for u, v, l, a in edges: if u in comp and v in comp: cu, cv = comp[u], comp[v] succ[cu].append((cv, l, a)) new_edges.add((cu, cv)) # トポソ ts = list(topological_sort(N1, new_edges)) if len(ts) != N1: print('INF');exit() assert ts[0] == comp[0] assert ts[-1] == comp[N] dp = [0] * N1 for v in ts: cur = dp[v] for s, l, a in succ[v]: dp[s] += (cur + l) * a dp[s] %= MOD print(dp[ts[-1]])