結果
| 問題 |
No.2290 UnUnion Find
|
| コンテスト | |
| ユーザー |
ぷら
|
| 提出日時 | 2023-05-05 21:24:29 |
| 言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
WA
|
| 実行時間 | - |
| コード長 | 1,657 bytes |
| コンパイル時間 | 2,449 ms |
| コンパイル使用メモリ | 205,424 KB |
| 最終ジャッジ日時 | 2025-02-12 17:26:18 |
|
ジャッジサーバーID (参考情報) |
judge1 / judge4 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | WA * 1 |
| other | AC * 2 WA * 44 |
ソースコード
#include <bits/stdc++.h>
using namespace std;
struct UnionFind {
vector<int> par;
vector<int> size;
UnionFind(int n) {
par.resize(n);
size.resize(n,1);
for(int i = 0; i < n; i++) {
par[i] = i;
}
}
int find(int x) {
if(par[x] == x) {
return x;
}
return par[x] = find(par[x]);
}
bool same(int x, int y) {
return find(x) == find(y);
}
int consize(int x) {
return size[find(x)];
}
bool unite(int x, int y) {
x = find(x);
y = find(y);
if(x == y) {
return false;
}
if(size[x] > size[y]) {
swap(x,y);
}
par[x] = y;
size[y] += size[x];
return true;
}
};
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int N,Q;
cin >> N >> Q;
UnionFind uf(N);
set<int>st;
for(int i = 0; i < N; i++) st.insert(i);
while(Q--) {
int f;
cin >> f;
if(f == 1) {
int u,v;
cin >> u >> v;
u--;
v--;
if(!uf.same(u,v)) {
st.erase(uf.find(u));
st.erase(uf.find(v));
uf.unite(u,v);
st.insert(uf.find(u));
}
}
else {
int v;
cin >> v;
v--;
if(uf.consize(v) == N) {
cout << -1 << "\n";
}
else {
st.erase(uf.find(v));
cout << *st.begin() << "\n";
st.insert(uf.find(v));
}
}
}
}
ぷら