結果
問題 | No.2290 UnUnion Find |
ユーザー | kusaf_ |
提出日時 | 2023-05-28 00:50:00 |
言語 | C++17 (gcc 12.3.0 + boost 1.83.0) |
結果 |
AC
|
実行時間 | 330 ms / 2,000 ms |
コード長 | 2,072 bytes |
コンパイル時間 | 2,603 ms |
コンパイル使用メモリ | 211,452 KB |
実行使用メモリ | 15,468 KB |
最終ジャッジ日時 | 2023-08-27 01:49:23 |
合計ジャッジ時間 | 19,543 ms |
ジャッジサーバーID (参考情報) |
judge12 / judge11 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 1 ms
4,376 KB |
testcase_01 | AC | 2 ms
4,380 KB |
testcase_02 | AC | 39 ms
4,380 KB |
testcase_03 | AC | 121 ms
9,324 KB |
testcase_04 | AC | 126 ms
9,288 KB |
testcase_05 | AC | 126 ms
9,380 KB |
testcase_06 | AC | 122 ms
9,436 KB |
testcase_07 | AC | 122 ms
9,432 KB |
testcase_08 | AC | 121 ms
9,396 KB |
testcase_09 | AC | 123 ms
9,284 KB |
testcase_10 | AC | 124 ms
9,500 KB |
testcase_11 | AC | 123 ms
9,436 KB |
testcase_12 | AC | 124 ms
9,436 KB |
testcase_13 | AC | 118 ms
9,440 KB |
testcase_14 | AC | 122 ms
9,420 KB |
testcase_15 | AC | 118 ms
9,424 KB |
testcase_16 | AC | 120 ms
9,456 KB |
testcase_17 | AC | 118 ms
9,324 KB |
testcase_18 | AC | 116 ms
9,428 KB |
testcase_19 | AC | 151 ms
10,212 KB |
testcase_20 | AC | 215 ms
15,360 KB |
testcase_21 | AC | 51 ms
4,384 KB |
testcase_22 | AC | 84 ms
6,256 KB |
testcase_23 | AC | 103 ms
6,252 KB |
testcase_24 | AC | 182 ms
12,236 KB |
testcase_25 | AC | 165 ms
5,480 KB |
testcase_26 | AC | 195 ms
13,832 KB |
testcase_27 | AC | 187 ms
12,768 KB |
testcase_28 | AC | 114 ms
8,232 KB |
testcase_29 | AC | 173 ms
11,404 KB |
testcase_30 | AC | 210 ms
4,388 KB |
testcase_31 | AC | 159 ms
10,640 KB |
testcase_32 | AC | 97 ms
5,460 KB |
testcase_33 | AC | 116 ms
8,028 KB |
testcase_34 | AC | 211 ms
15,468 KB |
testcase_35 | AC | 195 ms
13,868 KB |
testcase_36 | AC | 123 ms
5,636 KB |
testcase_37 | AC | 105 ms
7,488 KB |
testcase_38 | AC | 78 ms
5,636 KB |
testcase_39 | AC | 97 ms
6,388 KB |
testcase_40 | AC | 197 ms
12,984 KB |
testcase_41 | AC | 330 ms
5,204 KB |
testcase_42 | AC | 156 ms
9,812 KB |
testcase_43 | AC | 138 ms
9,408 KB |
testcase_44 | AC | 122 ms
9,440 KB |
testcase_45 | AC | 124 ms
9,288 KB |
testcase_46 | AC | 132 ms
9,288 KB |
ソースコード
#include <bits/stdc++.h> using namespace std; using ll = long long; struct Unionfind { vector<ll> par, usiz; ll e = 0, v; Unionfind(ll n): par(n), usiz(n), v(n) { for(ll i = 0; i < n; i++) { par[i] = i; usiz[i] = 1; } } // ↓に"/"追加でコメントアウト解除 /** Unionfind(graph g): par(g()), usiz(g()), v(g()) { for(ll i = 0; i < v; i++) { par[i] = i; usiz[i] = 1; } for(ll i = 0; i < v; i++) { for(auto &j : g[i]) { if(i < j) unite(i, j); } } } /**/ ll root(ll x) { if(par[x] == x) return x; return par[x] = root(par[x]); } void unite(ll x, ll y) { x = root(x), y = root(y); if(x == y) return; if(usiz[x] > usiz[y]) swap(x, y); par[x] = y; usiz[y] += usiz[x]; e++; } void unite(pair<ll, ll> x) { unite(x.first, x.second); } bool same(ll x, ll y) { return root(x) == root(y); } bool same(pair<ll, ll> x) { return same(x.first, x.second); } ll size(ll x) { return usiz[root(x)]; } vector<vector<ll>> group() { vector<vector<ll>> r(v); for(ll i = 0; i < v; i++) r[root(i)].push_back(i); r.erase(remove_if(begin(r), end(r), [&](vector<ll> &r_) { return r_.empty(); })); return r; } ll operator[](ll i) { return root(i); } void operator()(ll x, ll y) { unite(x, y); } void operator()(pair<ll, ll> x) { unite(x); } ll operator()() { return v - e; } }; int main() { ios::sync_with_stdio(false); cin.tie(0); ll n, Q; cin >> n >> Q; Unionfind u(n); set<ll> s; for(ll i = 0; i < n; i++) s.insert(i); while(Q--) { ll f; cin >> f; if(f == 1) { ll x, y; cin >> x >> y; x--, y--; ll rx = u[x], ry = u[y]; u.unite(x, y); if(u[x] == rx) s.erase(y); else s.erase(x); } else { ll v; cin >> v; v--; if(u() == 1) cout << -1 << "\n"; else { ll r = u[v]; for(auto &i : s) { if(u[i] != r) { cout << i + 1 << "\n"; break; } } } } } }