結果
問題 | No.2914 正閉路検出 |
ユーザー | Shirotsume |
提出日時 | 2024-07-29 19:16:20 |
言語 | PyPy3 (7.3.15) |
結果 |
AC
|
実行時間 | 912 ms / 2,000 ms |
コード長 | 3,186 bytes |
コンパイル時間 | 530 ms |
コンパイル使用メモリ | 82,400 KB |
実行使用メモリ | 141,088 KB |
最終ジャッジ日時 | 2024-07-29 19:16:41 |
合計ジャッジ時間 | 18,383 ms |
ジャッジサーバーID (参考情報) |
judge3 / judge2 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 69 ms
55,936 KB |
testcase_01 | AC | 44 ms
56,576 KB |
testcase_02 | AC | 42 ms
55,936 KB |
testcase_03 | AC | 42 ms
56,064 KB |
testcase_04 | AC | 44 ms
56,320 KB |
testcase_05 | AC | 42 ms
56,192 KB |
testcase_06 | AC | 45 ms
56,576 KB |
testcase_07 | AC | 45 ms
56,448 KB |
testcase_08 | AC | 45 ms
56,704 KB |
testcase_09 | AC | 42 ms
56,320 KB |
testcase_10 | AC | 44 ms
56,448 KB |
testcase_11 | AC | 44 ms
56,448 KB |
testcase_12 | AC | 44 ms
55,936 KB |
testcase_13 | AC | 45 ms
56,448 KB |
testcase_14 | AC | 45 ms
56,832 KB |
testcase_15 | AC | 45 ms
56,448 KB |
testcase_16 | AC | 44 ms
56,704 KB |
testcase_17 | AC | 44 ms
56,320 KB |
testcase_18 | AC | 56 ms
56,576 KB |
testcase_19 | AC | 118 ms
93,880 KB |
testcase_20 | AC | 167 ms
101,168 KB |
testcase_21 | AC | 95 ms
84,364 KB |
testcase_22 | AC | 182 ms
103,396 KB |
testcase_23 | AC | 175 ms
99,108 KB |
testcase_24 | AC | 234 ms
111,184 KB |
testcase_25 | AC | 104 ms
80,340 KB |
testcase_26 | AC | 100 ms
82,912 KB |
testcase_27 | AC | 263 ms
107,800 KB |
testcase_28 | AC | 427 ms
116,828 KB |
testcase_29 | AC | 671 ms
138,880 KB |
testcase_30 | AC | 118 ms
88,324 KB |
testcase_31 | AC | 271 ms
108,752 KB |
testcase_32 | AC | 547 ms
119,720 KB |
testcase_33 | AC | 124 ms
90,560 KB |
testcase_34 | AC | 865 ms
140,328 KB |
testcase_35 | AC | 878 ms
140,164 KB |
testcase_36 | AC | 912 ms
139,552 KB |
testcase_37 | AC | 307 ms
141,088 KB |
testcase_38 | AC | 261 ms
120,076 KB |
testcase_39 | AC | 248 ms
113,448 KB |
testcase_40 | AC | 361 ms
138,688 KB |
testcase_41 | AC | 279 ms
117,252 KB |
testcase_42 | AC | 366 ms
140,020 KB |
testcase_43 | AC | 338 ms
132,504 KB |
testcase_44 | AC | 228 ms
107,700 KB |
testcase_45 | AC | 355 ms
131,104 KB |
testcase_46 | AC | 318 ms
128,736 KB |
testcase_47 | AC | 304 ms
125,460 KB |
testcase_48 | AC | 271 ms
111,840 KB |
testcase_49 | AC | 335 ms
131,988 KB |
testcase_50 | AC | 303 ms
125,096 KB |
testcase_51 | AC | 314 ms
121,024 KB |
testcase_52 | AC | 290 ms
121,776 KB |
testcase_53 | AC | 288 ms
122,652 KB |
testcase_54 | AC | 284 ms
122,532 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() 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(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)