結果
問題 | No.2914 正閉路検出 |
ユーザー | Shirotsume |
提出日時 | 2024-07-29 19:12:39 |
言語 | PyPy3 (7.3.15) |
結果 |
WA
|
実行時間 | - |
コード長 | 2,872 bytes |
コンパイル時間 | 383 ms |
コンパイル使用メモリ | 82,320 KB |
実行使用メモリ | 142,240 KB |
最終ジャッジ日時 | 2024-07-29 19:13:02 |
合計ジャッジ時間 | 21,303 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge4 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 41 ms
57,768 KB |
testcase_01 | AC | 45 ms
57,128 KB |
testcase_02 | AC | 42 ms
56,356 KB |
testcase_03 | AC | 46 ms
57,056 KB |
testcase_04 | WA | - |
testcase_05 | AC | 41 ms
56,024 KB |
testcase_06 | AC | 47 ms
57,532 KB |
testcase_07 | AC | 45 ms
56,556 KB |
testcase_08 | AC | 44 ms
58,132 KB |
testcase_09 | AC | 42 ms
56,796 KB |
testcase_10 | AC | 44 ms
58,124 KB |
testcase_11 | AC | 45 ms
56,632 KB |
testcase_12 | AC | 46 ms
57,804 KB |
testcase_13 | WA | - |
testcase_14 | WA | - |
testcase_15 | WA | - |
testcase_16 | WA | - |
testcase_17 | AC | 46 ms
57,472 KB |
testcase_18 | WA | - |
testcase_19 | AC | 98 ms
91,256 KB |
testcase_20 | AC | 157 ms
103,320 KB |
testcase_21 | AC | 86 ms
84,628 KB |
testcase_22 | AC | 172 ms
103,268 KB |
testcase_23 | AC | 178 ms
99,292 KB |
testcase_24 | AC | 216 ms
111,188 KB |
testcase_25 | AC | 82 ms
79,768 KB |
testcase_26 | AC | 88 ms
82,540 KB |
testcase_27 | AC | 244 ms
108,224 KB |
testcase_28 | AC | 394 ms
116,052 KB |
testcase_29 | AC | 560 ms
134,408 KB |
testcase_30 | AC | 108 ms
88,256 KB |
testcase_31 | WA | - |
testcase_32 | WA | - |
testcase_33 | WA | - |
testcase_34 | WA | - |
testcase_35 | WA | - |
testcase_36 | WA | - |
testcase_37 | WA | - |
testcase_38 | AC | 222 ms
121,160 KB |
testcase_39 | WA | - |
testcase_40 | WA | - |
testcase_41 | WA | - |
testcase_42 | WA | - |
testcase_43 | WA | - |
testcase_44 | WA | - |
testcase_45 | WA | - |
testcase_46 | WA | - |
testcase_47 | WA | - |
testcase_48 | WA | - |
testcase_49 | WA | - |
testcase_50 | WA | - |
testcase_51 | AC | 277 ms
125,216 KB |
testcase_52 | AC | 274 ms
123,344 KB |
testcase_53 | AC | 285 ms
125,160 KB |
testcase_54 | WA | - |
ソースコード
class WeightedUnionFind: def __init__(self, n): self.par = [i for i in range(n+1)] self.rank = [0] * (n+1) # 根への距離を管理 self.weight = [0] * (n+1) # 検索 def find(self, x): if self.par[x] == x: return x else: y = self.find(self.par[x]) # 親への重みを追加しながら根まで走査 self.weight[x] += self.weight[self.par[x]] self.par[x] = y return y # 併合 def union(self, x, y, w): rx = self.find(x) ry = self.find(y) # xの木の高さ < yの木の高さ if self.rank[rx] < self.rank[ry]: self.par[rx] = ry self.weight[rx] = w - self.weight[x] + self.weight[y] # xの木の高さ ≧ yの木の高さ else: self.par[ry] = rx self.weight[ry] = -w - self.weight[y] + self.weight[x] # 木の高さが同じだった場合の処理 if self.rank[rx] == self.rank[ry]: self.rank[rx] += 1 # 同じ集合に属するか def same(self, x, y): return self.find(x) == self.find(y) # xからyへのコスト def diff(self, x, y): return self.weight[x] - self.weight[y] def Dijkstra(s, graph): INF = 2 ** 63 - 1 import heapq n = len(graph) dist = [INF] * n dist[s] = 0 bef = [0] * n bef[s] = s hq = [(0, s)] heapq.heapify(hq) while hq: c, now = heapq.heappop(hq) if c > dist[now]: continue for to, cost in graph[now]: if dist[now] + cost < dist[to]: dist[to] = cost + dist[now] bef[to] = now heapq.heappush(hq, (dist[to], + to)) return dist, bef def DijkstraRest(bef, t): now = t ret = [] while bef[now] != now: ret.append((bef[now], now)) now = bef[now] ret.reverse() return ret import sys, time, random from collections import deque, Counter, defaultdict input = lambda: sys.stdin.readline().rstrip() ii = lambda: int(input()) mi = lambda: map(int, input().split()) li = lambda: list(mi()) inf = 2 ** 61 - 1 mod = 998244353 n, m = mi() graph = [[] for _ in range(n)] U = WeightedUnionFind(n) EDGE = {} cnt = 1 for _ in range(m): u, v, w = mi() u -= 1 v -= 1 if U.same(u, v) and U.diff(u, v) != w: D, r = Dijkstra(u, graph) rest = DijkstraRest(r, v) ans = [] for i in range(len(rest)): ans.append(EDGE[rest[i]]) ans.append(cnt) print(len(ans)) print(v + 1) print(*ans) exit() else: EDGE[(u, v)] = cnt EDGE[(v, u)] = cnt U.union(u, v, w) graph[u].append((v, 1)) graph[v].append((u, 1)) cnt += 1 print(-1)