結果
問題 | No.2914 正閉路検出 |
ユーザー | Kude |
提出日時 | 2024-10-04 22:23:32 |
言語 | C++23 (gcc 12.3.0 + boost 1.83.0) |
結果 |
AC
|
実行時間 | 59 ms / 2,000 ms |
コード長 | 4,234 bytes |
コンパイル時間 | 3,157 ms |
コンパイル使用メモリ | 282,016 KB |
実行使用メモリ | 22,508 KB |
最終ジャッジ日時 | 2024-10-04 22:23:40 |
合計ジャッジ時間 | 7,668 ms |
ジャッジサーバーID (参考情報) |
judge4 / judge3 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 2 ms
6,816 KB |
testcase_01 | AC | 2 ms
6,820 KB |
testcase_02 | AC | 1 ms
6,820 KB |
testcase_03 | AC | 1 ms
6,816 KB |
testcase_04 | AC | 1 ms
6,820 KB |
testcase_05 | AC | 2 ms
6,816 KB |
testcase_06 | AC | 2 ms
6,816 KB |
testcase_07 | AC | 2 ms
6,816 KB |
testcase_08 | AC | 2 ms
6,820 KB |
testcase_09 | AC | 1 ms
6,820 KB |
testcase_10 | AC | 2 ms
6,820 KB |
testcase_11 | AC | 2 ms
6,816 KB |
testcase_12 | AC | 1 ms
6,820 KB |
testcase_13 | AC | 1 ms
6,820 KB |
testcase_14 | AC | 2 ms
6,816 KB |
testcase_15 | AC | 2 ms
6,816 KB |
testcase_16 | AC | 1 ms
6,816 KB |
testcase_17 | AC | 1 ms
6,816 KB |
testcase_18 | AC | 1 ms
6,820 KB |
testcase_19 | AC | 7 ms
6,820 KB |
testcase_20 | AC | 15 ms
6,816 KB |
testcase_21 | AC | 5 ms
6,820 KB |
testcase_22 | AC | 19 ms
6,816 KB |
testcase_23 | AC | 13 ms
6,820 KB |
testcase_24 | AC | 21 ms
6,820 KB |
testcase_25 | AC | 3 ms
6,816 KB |
testcase_26 | AC | 4 ms
6,816 KB |
testcase_27 | AC | 24 ms
6,816 KB |
testcase_28 | AC | 26 ms
6,820 KB |
testcase_29 | AC | 42 ms
11,008 KB |
testcase_30 | AC | 10 ms
7,552 KB |
testcase_31 | AC | 23 ms
6,816 KB |
testcase_32 | AC | 26 ms
6,816 KB |
testcase_33 | AC | 8 ms
7,424 KB |
testcase_34 | AC | 46 ms
10,880 KB |
testcase_35 | AC | 45 ms
10,880 KB |
testcase_36 | AC | 46 ms
11,008 KB |
testcase_37 | AC | 53 ms
22,508 KB |
testcase_38 | AC | 41 ms
11,008 KB |
testcase_39 | AC | 44 ms
15,488 KB |
testcase_40 | AC | 56 ms
21,676 KB |
testcase_41 | AC | 48 ms
17,640 KB |
testcase_42 | AC | 59 ms
22,304 KB |
testcase_43 | AC | 54 ms
21,420 KB |
testcase_44 | AC | 42 ms
15,104 KB |
testcase_45 | AC | 55 ms
20,592 KB |
testcase_46 | AC | 53 ms
20,252 KB |
testcase_47 | AC | 52 ms
19,448 KB |
testcase_48 | AC | 44 ms
16,256 KB |
testcase_49 | AC | 55 ms
21,076 KB |
testcase_50 | AC | 50 ms
19,580 KB |
testcase_51 | AC | 34 ms
10,920 KB |
testcase_52 | AC | 35 ms
11,008 KB |
testcase_53 | AC | 34 ms
10,920 KB |
testcase_54 | AC | 36 ms
11,008 KB |
ソースコード
#include<bits/stdc++.h> namespace { #pragma GCC diagnostic ignored "-Wunused-function" #include<atcoder/all> #pragma GCC diagnostic warning "-Wunused-function" using namespace std; using namespace atcoder; #define rep(i,n) for(int i = 0; i < (int)(n); i++) #define rrep(i,n) for(int i = (int)(n) - 1; i >= 0; i--) #define all(x) begin(x), end(x) #define rall(x) rbegin(x), rend(x) template<class T> bool chmax(T& a, const T& b) { if (a < b) { a = b; return true; } else return false; } template<class T> bool chmin(T& a, const T& b) { if (b < a) { a = b; return true; } else return false; } using ll = long long; using P = pair<int,int>; using VI = vector<int>; using VVI = vector<VI>; using VL = vector<ll>; using VVL = vector<VL>; // S : group template <class S, S (*op)(S, S), S (*e)(), S (*inv)(S)> struct weighted_union_find { public: weighted_union_find() : _n(0) {} explicit weighted_union_find(int n) : _n(n), parent_or_size(n, -1), weight(n, e()) {} int merge(int a, int b, S w) { // W(a->b) = Wa^-1 Wb = w assert(0 <= a && a < _n); assert(0 <= b && b < _n); int x = leader(a), y = leader(b); assert(x != y); if (-parent_or_size[x] < -parent_or_size[y]) { std::swap(x, y); std::swap(a, b); w = inv(w); } // Wa^-1 Wy Wb = w // Wy = Wa w Wb^-1 weight[y] = op(op(weight[a], w), inv(weight[b])); parent_or_size[x] += parent_or_size[y]; parent_or_size[y] = x; return x; } S diff(int a, int b) { // W(a->b) = Wa^-1 Wb int x = leader(a), y = leader(b); assert(x == y); return op(inv(weight[a]), weight[b]); } bool same(int a, int b) { assert(0 <= a && a < _n); assert(0 <= b && b < _n); return leader(a) == leader(b); } int leader(int a) { assert(0 <= a && a < _n); int pre = -1; while (parent_or_size[a] >= 0) { int na = parent_or_size[a]; parent_or_size[a] = pre; pre = a; a = na; } S w = e(); while (pre != -1) { w = op(w, weight[pre]); weight[pre] = w; int npre = parent_or_size[pre]; parent_or_size[pre] = a; pre = npre; } return a; } int size(int a) { assert(0 <= a && a < _n); return -parent_or_size[leader(a)]; } std::vector<std::vector<int>> groups() { std::vector<int> leader_buf(_n), group_size(_n); for (int i = 0; i < _n; i++) { leader_buf[i] = leader(i); group_size[leader_buf[i]]++; } std::vector<std::vector<int>> result(_n); for (int i = 0; i < _n; i++) { result[i].reserve(group_size[i]); } for (int i = 0; i < _n; i++) { result[leader_buf[i]].push_back(i); } result.erase( std::remove_if(result.begin(), result.end(), [&](const std::vector<int>& v) { return v.empty(); }), result.end()); return result; } private: int _n; // root node: -1 * component size // otherwise: parent std::vector<int> parent_or_size; std::vector<S> weight; }; ll op(ll x, ll y) { return x + y; } ll e() { return 0; } ll inv(ll x) { return -x; } } int main() { ios::sync_with_stdio(false); cin.tie(0); int n, m; cin >> n >> m; struct E { int u, v, w; }; vector<E> es(m); for (auto& [u, v, w] : es) cin >> u >> v >> w, u--, v--; weighted_union_find<ll, op, e, inv> uf(n); vector<vector<P>> to(n); for (int i = -1; auto [u, v, w] : es) { i++; if (uf.same(u, v)) { if (uf.diff(u, v) != w) { VI st; auto dfs = [&](auto&& self, int u, int p) -> bool { if (u == v) return true; for (auto [v, eid] : to[u]) if (v != p) { st.emplace_back(eid); if (self(self, v, u)) return true; st.pop_back(); } return false; }; bool res = dfs(dfs, u, -1); assert(res); st.emplace_back(i); ll now = uf.diff(u, v) - w; if (now < 0) reverse(all(st)); cout << ssize(st) << '\n'; cout << u + 1 << '\n'; rep(i, ssize(st)) cout << st[i] + 1 << " \n"[i + 1 == ssize(st)]; return 0; } } else { uf.merge(u, v, w); to[u].emplace_back(v, i); to[v].emplace_back(u, i); } } cout << -1 << '\n'; }