結果
問題 | No.2914 正閉路検出 |
ユーザー | tassei903 |
提出日時 | 2024-10-07 00:19:27 |
言語 | PyPy3 (7.3.15) |
結果 |
AC
|
実行時間 | 520 ms / 2,000 ms |
コード長 | 2,872 bytes |
コンパイル時間 | 405 ms |
コンパイル使用メモリ | 82,460 KB |
実行使用メモリ | 132,720 KB |
最終ジャッジ日時 | 2024-10-07 00:19:43 |
合計ジャッジ時間 | 15,390 ms |
ジャッジサーバーID (参考情報) |
judge3 / judge1 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 36 ms
52,224 KB |
testcase_01 | AC | 37 ms
52,224 KB |
testcase_02 | AC | 37 ms
52,480 KB |
testcase_03 | AC | 38 ms
52,992 KB |
testcase_04 | AC | 38 ms
52,352 KB |
testcase_05 | AC | 38 ms
52,736 KB |
testcase_06 | AC | 37 ms
52,608 KB |
testcase_07 | AC | 37 ms
52,224 KB |
testcase_08 | AC | 37 ms
52,480 KB |
testcase_09 | AC | 38 ms
52,352 KB |
testcase_10 | AC | 38 ms
53,120 KB |
testcase_11 | AC | 38 ms
52,864 KB |
testcase_12 | AC | 37 ms
52,736 KB |
testcase_13 | AC | 39 ms
53,248 KB |
testcase_14 | AC | 38 ms
53,120 KB |
testcase_15 | AC | 53 ms
52,992 KB |
testcase_16 | AC | 38 ms
52,736 KB |
testcase_17 | AC | 38 ms
52,736 KB |
testcase_18 | AC | 38 ms
52,736 KB |
testcase_19 | AC | 88 ms
80,552 KB |
testcase_20 | AC | 120 ms
86,272 KB |
testcase_21 | AC | 77 ms
77,440 KB |
testcase_22 | AC | 129 ms
88,948 KB |
testcase_23 | AC | 128 ms
85,632 KB |
testcase_24 | AC | 183 ms
105,072 KB |
testcase_25 | AC | 86 ms
77,312 KB |
testcase_26 | AC | 78 ms
77,312 KB |
testcase_27 | AC | 151 ms
92,404 KB |
testcase_28 | AC | 240 ms
93,808 KB |
testcase_29 | AC | 341 ms
96,656 KB |
testcase_30 | AC | 90 ms
80,128 KB |
testcase_31 | AC | 216 ms
98,448 KB |
testcase_32 | AC | 327 ms
100,632 KB |
testcase_33 | AC | 103 ms
83,516 KB |
testcase_34 | AC | 505 ms
117,772 KB |
testcase_35 | AC | 520 ms
117,808 KB |
testcase_36 | AC | 520 ms
118,288 KB |
testcase_37 | AC | 254 ms
132,720 KB |
testcase_38 | AC | 150 ms
92,268 KB |
testcase_39 | AC | 263 ms
117,548 KB |
testcase_40 | AC | 306 ms
128,312 KB |
testcase_41 | AC | 287 ms
121,696 KB |
testcase_42 | AC | 320 ms
132,080 KB |
testcase_43 | AC | 307 ms
131,136 KB |
testcase_44 | AC | 259 ms
112,452 KB |
testcase_45 | AC | 305 ms
129,044 KB |
testcase_46 | AC | 300 ms
128,316 KB |
testcase_47 | AC | 296 ms
126,224 KB |
testcase_48 | AC | 271 ms
118,376 KB |
testcase_49 | AC | 318 ms
129,648 KB |
testcase_50 | AC | 289 ms
126,280 KB |
testcase_51 | AC | 188 ms
94,996 KB |
testcase_52 | AC | 222 ms
116,368 KB |
testcase_53 | AC | 226 ms
115,344 KB |
testcase_54 | AC | 227 ms
116,884 KB |
ソースコード
import sys input = lambda :sys.stdin.readline()[:-1] ni = lambda :int(input()) na = lambda :list(map(int,input().split())) yes = lambda :print("yes");Yes = lambda :print("Yes");YES = lambda : print("YES") no = lambda :print("no");No = lambda :print("No");NO = lambda : print("NO") ####################################################################### 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 dfs(n, e, U, V): g = [[] for _ in range(n)] for u, v, w, i in e: g[u].append((v, w, i)) g[v].append((u, -w, i)) prev_v = [-1] * n prev_e = [-1] * n seen = [False] * n q = [U] seen[U] = True while q: x = q.pop() for y, w, i in g[x]: if seen[y]: continue seen[y] = True prev_v[y] = x prev_e[y] = i q.append(y) path = [] x = V while x != U: path.append(prev_e[x]) x = prev_v[x] return path[::-1] n, m = na() e = [] for i in range(m): u, v, w = na() e.append((u-1, v-1, w, i)) wuf = WeightedUnionFind(n) for u, v, w, i in e: if wuf.same(u, v): if wuf.diff(u, v) > w: path = dfs(n, e[:i], u, v) print(len(path) + 1) print(u+1) print(*([path[i]+1 for i in range(len(path))] + [i+1])) exit() elif wuf.diff(u, v) < w: path = dfs(n, e[:i], v, u) print(len(path) + 1) print(v+1) print(*([path[i]+1 for i in range(len(path))] + [i+1])) exit() else: wuf.union(u, v, w) print(-1)