結果

問題 No.1364 [Renaming] Road to Cherry from Zelkova
ユーザー H3PO4H3PO4
提出日時 2021-01-22 22:59:43
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
WA  
実行時間 -
コード長 1,698 bytes
コンパイル時間 287 ms
コンパイル使用メモリ 10,792 KB
実行使用メモリ 168,048 KB
最終ジャッジ日時 2023-08-27 21:33:12
合計ジャッジ時間 42,739 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 209 ms
42,688 KB
testcase_01 AC 204 ms
42,760 KB
testcase_02 AC 205 ms
42,756 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 394 ms
62,444 KB
testcase_24 AC 415 ms
60,312 KB
testcase_25 AC 1,128 ms
120,312 KB
testcase_26 AC 1,774 ms
168,048 KB
testcase_27 AC 1,261 ms
134,356 KB
testcase_28 AC 844 ms
100,808 KB
testcase_29 AC 1,232 ms
133,576 KB
testcase_30 AC 896 ms
104,364 KB
testcase_31 AC 535 ms
76,232 KB
testcase_32 AC 1,031 ms
116,236 KB
testcase_33 AC 1,648 ms
162,980 KB
testcase_34 AC 1,520 ms
146,732 KB
testcase_35 AC 815 ms
102,456 KB
testcase_36 AC 819 ms
102,692 KB
testcase_37 AC 870 ms
96,632 KB
testcase_38 WA -
testcase_39 WA -
testcase_40 WA -
testcase_41 WA -
testcase_42 WA -
testcase_43 AC 1,229 ms
138,856 KB
testcase_44 AC 605 ms
76,556 KB
testcase_45 AC 1,085 ms
138,776 KB
testcase_46 AC 210 ms
43,760 KB
testcase_47 AC 202 ms
42,924 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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]])
0