結果
問題 | No.1364 [Renaming] Road to Cherry from Zelkova |
ユーザー |
![]() |
提出日時 | 2021-01-22 22:27:53 |
言語 | PyPy3 (7.3.15) |
結果 |
WA
|
実行時間 | - |
コード長 | 2,220 bytes |
コンパイル時間 | 863 ms |
コンパイル使用メモリ | 82,048 KB |
実行使用メモリ | 174,160 KB |
最終ジャッジ日時 | 2024-12-28 02:51:38 |
合計ジャッジ時間 | 13,918 ms |
ジャッジサーバーID (参考情報) |
judge2 / judge1 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 42 WA * 3 |
ソースコード
import sysinput = sys.stdin.buffer.readlinedef topological_sorted(digraph):n = len(digraph)indegree = [0] * nfor v in range(n):for nxt_v in digraph[v]:indegree[nxt_v] += 1tp_order = [i for i in range(n) if indegree[i] == 0]stack = tp_order[:]while stack:v = stack.pop()for nxt_v in digraph[v]:indegree[nxt_v] -= 1if indegree[nxt_v] == 0:stack.append(nxt_v)tp_order.append(nxt_v)return len(tp_order) == n, tp_ordern, m = map(int, input().split())edges = [list(map(int, input().split())) for i in range(m)]MOD = 10 ** 9 + 7graph = [[] for i in range(n + 1)]for u, v, _, _ in edges:graph[u].append(v)rev_graph = [[] for i in range(n + 1)]for u, v, _, _ in edges:rev_graph[v].append(u)root = 0reach = [False] * (n + 1)reach[0] = Truestack = [root]while stack:v = stack.pop()for nxt_v in graph[v]:if reach[nxt_v]:continuereach[nxt_v] = Truestack.append(nxt_v)root = nrev_reach = [False] * (n + 1)rev_reach[n] = Truestack = [root]while stack:v = stack.pop()for nxt_v in rev_graph[v]:if rev_reach[nxt_v]:continuerev_reach[nxt_v] = Truestack.append(nxt_v)if not reach[n]:print("INF")exit()for i in range(n + 1):reach[i] = reach[i] & rev_reach[i]graph = [set() for i in range(n + 1)]memo = {}for u, v, l, a in edges:if reach[u] and reach[v]:graph[u].add(v)if (u, v) not in memo:memo[u, v] = []memo[u, v].append((l, a))flag, tp_order = topological_sorted(graph)if not flag:print("INF")exit()nxt_ptn = [0] * (n + 1)nxt_ptn[n] = 1for v in tp_order[::-1]:for nxt_v in graph[v]:for l, a in memo[v, nxt_v]:nxt_ptn[v] += nxt_ptn[nxt_v] * anxt_ptn[v] %= MODans = 0ptn = [0] * (n + 1)ptn[0] = 1for v in tp_order:for nxt_v in graph[v]:for l, a in memo[v, nxt_v]:ptn[nxt_v] += ptn[v] * aans += ptn[v] * nxt_ptn[nxt_v] * l * aptn[nxt_v] %= MODans %= MODprint(ans)