結果
問題 | No.2914 正閉路検出 |
ユーザー | Shirotsume |
提出日時 | 2024-07-29 19:17:40 |
言語 | PyPy3 (7.3.15) |
結果 |
AC
|
実行時間 | 912 ms / 2,000 ms |
コード長 | 3,322 bytes |
コンパイル時間 | 453 ms |
コンパイル使用メモリ | 82,496 KB |
実行使用メモリ | 140,900 KB |
最終ジャッジ日時 | 2024-07-29 19:17:59 |
合計ジャッジ時間 | 17,630 ms |
ジャッジサーバーID (参考情報) |
judge2 / judge1 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 46 ms
56,340 KB |
testcase_01 | AC | 48 ms
57,904 KB |
testcase_02 | AC | 46 ms
57,256 KB |
testcase_03 | AC | 47 ms
57,332 KB |
testcase_04 | AC | 48 ms
57,396 KB |
testcase_05 | AC | 46 ms
56,760 KB |
testcase_06 | AC | 47 ms
57,496 KB |
testcase_07 | AC | 47 ms
56,628 KB |
testcase_08 | AC | 47 ms
56,960 KB |
testcase_09 | AC | 47 ms
56,792 KB |
testcase_10 | AC | 48 ms
57,420 KB |
testcase_11 | AC | 48 ms
57,644 KB |
testcase_12 | AC | 47 ms
57,328 KB |
testcase_13 | AC | 49 ms
57,300 KB |
testcase_14 | AC | 48 ms
57,808 KB |
testcase_15 | AC | 47 ms
57,524 KB |
testcase_16 | AC | 50 ms
57,560 KB |
testcase_17 | AC | 49 ms
57,164 KB |
testcase_18 | AC | 48 ms
57,976 KB |
testcase_19 | AC | 131 ms
92,260 KB |
testcase_20 | AC | 181 ms
102,380 KB |
testcase_21 | AC | 101 ms
84,480 KB |
testcase_22 | AC | 196 ms
102,896 KB |
testcase_23 | AC | 188 ms
99,056 KB |
testcase_24 | AC | 270 ms
110,844 KB |
testcase_25 | AC | 96 ms
80,128 KB |
testcase_26 | AC | 104 ms
82,876 KB |
testcase_27 | AC | 271 ms
107,756 KB |
testcase_28 | AC | 457 ms
119,648 KB |
testcase_29 | AC | 711 ms
138,460 KB |
testcase_30 | AC | 127 ms
88,952 KB |
testcase_31 | AC | 284 ms
108,732 KB |
testcase_32 | AC | 563 ms
117,824 KB |
testcase_33 | AC | 129 ms
90,368 KB |
testcase_34 | AC | 912 ms
139,896 KB |
testcase_35 | AC | 903 ms
139,824 KB |
testcase_36 | AC | 894 ms
139,516 KB |
testcase_37 | AC | 346 ms
140,900 KB |
testcase_38 | AC | 247 ms
120,176 KB |
testcase_39 | AC | 259 ms
113,212 KB |
testcase_40 | AC | 372 ms
138,948 KB |
testcase_41 | AC | 294 ms
117,064 KB |
testcase_42 | AC | 385 ms
140,016 KB |
testcase_43 | AC | 348 ms
132,652 KB |
testcase_44 | AC | 262 ms
108,188 KB |
testcase_45 | AC | 343 ms
131,296 KB |
testcase_46 | AC | 327 ms
128,900 KB |
testcase_47 | AC | 316 ms
125,432 KB |
testcase_48 | AC | 255 ms
111,824 KB |
testcase_49 | AC | 374 ms
130,524 KB |
testcase_50 | AC | 316 ms
125,488 KB |
testcase_51 | AC | 301 ms
120,380 KB |
testcase_52 | AC | 301 ms
121,704 KB |
testcase_53 | AC | 294 ms
123,052 KB |
testcase_54 | AC | 326 ms
122,992 KB |
ソースコード
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() assert 1 <= n <= 10 ** 5 assert 1 <= m <= 10 ** 5 graph = [[] for _ in range(n)] U = WeightedUnionFind(n) EDGE = {} cnt = 1 for _ in range(m): u, v, w = mi() assert u != v assert 1 <= u <= n and 1 <= v <= n assert 0 <= w <= 10 ** 5 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(u + 1) print(*ans) exit() elif U.same(u, v) and U.diff(u, v) < w: D, r = Dijkstra(v, graph) rest = DijkstraRest(r, u) 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)