結果
問題 | No.2914 正閉路検出 |
ユーザー | SnowBeenDiding |
提出日時 | 2024-10-04 22:12:31 |
言語 | C++23 (gcc 12.3.0 + boost 1.83.0) |
結果 |
AC
|
実行時間 | 271 ms / 2,000 ms |
コード長 | 4,222 bytes |
コンパイル時間 | 5,302 ms |
コンパイル使用メモリ | 336,248 KB |
実行使用メモリ | 27,492 KB |
最終ジャッジ日時 | 2024-10-04 22:12:47 |
合計ジャッジ時間 | 13,173 ms |
ジャッジサーバーID (参考情報) |
judge3 / judge1 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 2 ms
6,816 KB |
testcase_01 | AC | 2 ms
6,820 KB |
testcase_02 | AC | 2 ms
6,820 KB |
testcase_03 | AC | 1 ms
6,816 KB |
testcase_04 | AC | 1 ms
6,816 KB |
testcase_05 | AC | 1 ms
6,816 KB |
testcase_06 | AC | 1 ms
6,820 KB |
testcase_07 | AC | 2 ms
6,816 KB |
testcase_08 | AC | 1 ms
6,816 KB |
testcase_09 | AC | 1 ms
6,816 KB |
testcase_10 | AC | 1 ms
6,816 KB |
testcase_11 | AC | 2 ms
6,816 KB |
testcase_12 | AC | 2 ms
6,816 KB |
testcase_13 | AC | 2 ms
6,820 KB |
testcase_14 | AC | 1 ms
6,816 KB |
testcase_15 | AC | 2 ms
6,816 KB |
testcase_16 | AC | 2 ms
6,820 KB |
testcase_17 | AC | 2 ms
6,816 KB |
testcase_18 | AC | 2 ms
6,816 KB |
testcase_19 | AC | 25 ms
6,820 KB |
testcase_20 | AC | 40 ms
6,816 KB |
testcase_21 | AC | 11 ms
6,820 KB |
testcase_22 | AC | 49 ms
6,816 KB |
testcase_23 | AC | 37 ms
6,820 KB |
testcase_24 | AC | 57 ms
6,816 KB |
testcase_25 | AC | 5 ms
6,816 KB |
testcase_26 | AC | 9 ms
6,816 KB |
testcase_27 | AC | 62 ms
6,820 KB |
testcase_28 | AC | 78 ms
6,820 KB |
testcase_29 | AC | 205 ms
23,552 KB |
testcase_30 | AC | 21 ms
10,624 KB |
testcase_31 | AC | 62 ms
6,816 KB |
testcase_32 | AC | 83 ms
6,820 KB |
testcase_33 | AC | 22 ms
10,900 KB |
testcase_34 | AC | 264 ms
23,808 KB |
testcase_35 | AC | 240 ms
23,936 KB |
testcase_36 | AC | 229 ms
23,936 KB |
testcase_37 | AC | 162 ms
27,492 KB |
testcase_38 | AC | 141 ms
25,984 KB |
testcase_39 | AC | 139 ms
18,816 KB |
testcase_40 | AC | 234 ms
26,436 KB |
testcase_41 | AC | 154 ms
21,600 KB |
testcase_42 | AC | 271 ms
27,088 KB |
testcase_43 | AC | 239 ms
26,280 KB |
testcase_44 | AC | 113 ms
18,432 KB |
testcase_45 | AC | 214 ms
25,260 KB |
testcase_46 | AC | 214 ms
24,836 KB |
testcase_47 | AC | 200 ms
24,020 KB |
testcase_48 | AC | 137 ms
19,396 KB |
testcase_49 | AC | 242 ms
25,968 KB |
testcase_50 | AC | 255 ms
23,932 KB |
testcase_51 | AC | 119 ms
20,864 KB |
testcase_52 | AC | 116 ms
21,248 KB |
testcase_53 | AC | 114 ms
21,148 KB |
testcase_54 | AC | 132 ms
21,120 KB |
ソースコード
#include <atcoder/all> #include <bits/stdc++.h> #define rep(i, a, b) for (ll i = (ll)(a); i < (ll)(b); i++) using namespace atcoder; using namespace std; typedef long long ll; struct UnionFindWeight { vector<ll> par; vector<ll> rank; vector<ll> diff_weight; UnionFindWeight(ll n = 1, ll SUM_UNITY = 0) { reset(n, SUM_UNITY); } void reset(ll n = 1, ll SUM_UNITY = 0) { par.resize(n); rank.resize(n); diff_weight.resize(n); for (ll i = 0; i < n; ++i) par[i] = i, rank[i] = 0, diff_weight[i] = SUM_UNITY; } ll root(ll x) { if (par[x] == x) { return x; } else { ll r = root(par[x]); diff_weight[x] += diff_weight[par[x]]; return par[x] = r; } } ll weight(ll x) { root(x); return diff_weight[x]; } bool same(ll x, ll y) { return root(x) == root(y); } bool unite(ll x, ll y, ll w) { // x ----(距離 w)---- y w += weight(x); w -= weight(y); x = root(x); y = root(y); if (x == y) return false; if (rank[x] < rank[y]) swap(x, y), w = -w; if (rank[x] == rank[y]) ++rank[x]; par[y] = x; diff_weight[y] = w; return true; } ll dist(ll x, ll y) { return weight(y) - weight(x); } }; template <typename T> struct Graph { struct edge { int to; T cost; }; int N; vector<vector<edge>> G; vector<T> dist; vector<int> prever; Graph(int n) { init(n); } T inf() { if (is_same_v<T, int>) return 1e9; else return 1e18; } T zero() { return T(0); } void init(int n) { N = n; G.resize(N); dist.resize(N, inf()); } void add_edge(int s, int t, T cost) { edge e; e.to = t, e.cost = cost; G[s].push_back(e); } void dijkstra(int s) { rep(i, 0, N) dist[i] = inf(); prever = vector<int>(N, -1); dist[s] = zero(); priority_queue<pair<T, int>, vector<pair<T, int>>, greater<pair<T, int>>> q; q.push({zero(), s}); while (!q.empty()) { int now; T nowdist; tie(nowdist, now) = q.top(); q.pop(); if (dist[now] < nowdist) continue; for (auto e : G[now]) { T nextdist = nowdist + e.cost; // 次の頂点への距離 if (dist[e.to] > nextdist) { prever[e.to] = now; dist[e.to] = nextdist; q.push({dist[e.to], e.to}); } } } } vector<int> get_path(int t) { // tへの最短路構築 if (dist[t] >= inf()) return {-1}; vector<int> path; for (; t != -1; t = prever[t]) { path.push_back(t); } reverse(path.begin(), path.end()); return path; } }; int main() { int n, m; cin >> n >> m; UnionFindWeight uf(n); Graph<ll> gr(n); map<pair<int, int>, int> ma; rep(i, 0, m) { ll l, r, d; cin >> l >> r >> d; l--, r--; if (uf.same(l, r)) { if (uf.dist(l, r) != d) { cerr << uf.dist(l, r) << " " << d << endl; if (uf.dist(l, r) < d) { swap(l, r); } gr.dijkstra(l); auto path = gr.get_path(r); vector<int> ans; rep(j, 0, path.size() - 1) { ans.push_back(ma[{path[j], path[j + 1]}]); } ans.push_back(i); cout << ans.size() << endl; cout << l + 1 << endl; rep(j, 0, ans.size()) { cout << ans[j] + 1 << " \n"[j == ans.size() - 1]; } return 0; } } else { uf.unite(l, r, d); gr.add_edge(l, r, d); gr.add_edge(r, l, d); ma[{l, r}] = i; ma[{r, l}] = i; } } cout << "-1\n"; }