結果

問題 No.2290 UnUnion Find
ユーザー outline
提出日時 2023-06-14 20:35:15
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 251 ms / 2,000 ms
コード長 2,848 bytes
コンパイル時間 1,272 ms
コンパイル使用メモリ 144,944 KB
最終ジャッジ日時 2025-02-14 02:43:17
ジャッジサーバーID
(参考情報)
judge1 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 1
other AC * 46
権限があれば一括ダウンロードができます

ソースコード

diff #
プレゼンテーションモードにする

#include <iostream>
#include <vector>
#include <algorithm>
#include <cmath>
#include <queue>
#include <string>
#include <map>
#include <set>
#include <stack>
#include <tuple>
#include <deque>
#include <array>
#include <numeric>
#include <bitset>
#include <iomanip>
#include <cassert>
#include <chrono>
#include <random>
#include <limits>
#include <iterator>
#include <functional>
#include <sstream>
#include <fstream>
#include <complex>
#include <cstring>
#include <unordered_map>
#include <unordered_set>
#include <memory>
using namespace std;
// #pragma GCC optimize("O3")
// #pragma GCC optimize("unroll-loops")
// #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,avx2,tune=native")
// #pragma GCC target("avx512f,avx512dq,avx512cd,avx512bw,avx512vl")
using ll = long long;
constexpr int INF = 1001001001;
// constexpr int mod = 1000000007;
constexpr int mod = 998244353;
template<class T>
inline bool chmax(T& x, T y){
if(x < y){
x = y;
return true;
}
return false;
}
template<class T>
inline bool chmin(T& x, T y){
if(x > y){
x = y;
return true;
}
return false;
}
constexpr int dx[] = {1, 0, -1, 0, 1, 1, -1, -1};
constexpr int dy[] = {0, 1, 0, -1, 1, -1, 1, -1};
struct UnionFind{
int sz;
vector<int> par;
vector<int> rank;
UnionFind(int n) : sz(n) {
par.resize(sz);
rank.assign(sz, 0);
for(int i = 0; i < sz; ++i){
par[i] = i;
}
}
int find(int x){
if(par[x] == x) return x;
else return par[x] = find(par[x]);
}
bool same(int x, int y){
return find(x) == find(y);
}
bool unite(int x, int y){
x = find(x), y = find(y);
if(x == y) return false;
if(rank[x] < rank[y]) par[x] = y;
else{
par[y] = x;
if(rank[x] == rank[y]) ++rank[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.emplace(i);
for(int q = 0; q < Q; ++q){
int t;
cin >> t;
if(t == 1){
int u, v;
cin >> u >> v;
--u, --v;
u = uf.find(u);
v = uf.find(v);
st.erase(u);
st.erase(v);
uf.unite(u, v);
st.emplace(uf.find(u));
}
else{
int v;
cin >> v;
--v;
v = uf.find(v);
if(st.size() == 1) cout << -1 << '\n';
else{
auto it = st.upper_bound(v);
if(it == st.end()){
it = prev(st.find(v));
}
cout << *it + 1 << '\n';
}
}
}
return 0;
}
הההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההה
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
0