一、输入输出
cpp
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define endl '\n'
void solve()
{
}
signed main()
{
ios::sync_with_stdio(false);
cin.tie(nullptr);
int _ = 1;
// cin >> _;
while(_--)
{
solve();
}
return 0;
}
二、并查集
Disjoint Set Union
中文:不相交集合合并 / 并查集
cpp
struct DSU
{
vector<int> fa, sz;
DSU(int n)
:fa(n + 1)
,sz(n + 1, 1)
{
iota(fa.begin(), fa.end(), 0);
}
int find(int x)
{
while(x != fa[x]) x = fa[x] = fa[fa[x]];
return x;
}
int size(int x)
{
return sz[find(x)];
}
bool same(int x, int y)
{
return find(x) == find(y);
}
bool merge(int x, int y)
{
x = find(x), y = find(y);
if(x == y) return false;
// 如果x的集合更大,就交换x和y,保证把小的挂到大的上
if(sz[x] > sz[y]) swap(x, y);
sz[y] += sz[x];
fa[x] = y;
return true;
}
};