并查集模板:
#include <bits/stdc++.h>
using namespace std;
#define FAST ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
typedef long long LL;
typedef pair<int, int> PII;
const int N = 1e5 + 10;
int n, m, a[N];
int cnt[N];
int p[N];
int find(int x)
{
if (p[x] != x)
p[x] = find(p[x]);
return p[x];
}
void solve()
{
cin >> n >> m;
int hh=n;
for (int i = 0; i <= n; i++)
p[i] = i, cnt[i] = 1;
while (m--)
{
int x, y;
string s;
cin >> s;
if (s == "C")
{
cin >> x >> y;
if(find(x)!=find(y))
{
cnt[find(y)] += cnt[find(x)];
hh--;
p[find(x)] = find(y);
}
}
else if (s == "Q1")
{
cin >> x >> y;
if (find(x) == find(y))
cout << "YES" << endl;
else cout<<"No"<<endl;
}
else
{
cin >> x;
cout << cnt[find(x)] << endl;//注意得找其父节点
}
}
// cout<<hh<<endl;
}
signed main()
{
FAST;
int T = 1;
// cin>>T;
while (T--)
{
solve();
}
}
并查集反集:
在基础数的基础加上n虚拟参与并查集的维护并查集 就是开到两倍的并查集的集合 超出数据范围的数称为反集其中他们的属性是和原来的集合属性是相反的
并查集的反集只适用两中性质的集合 例如可以将a b合并 , a+n 和b 合并 b+n和 a合并 有c的话 a+n和c合并 c+n和a 合并那么b就和c合并 (敌人的敌人是朋友)
#include <bits/stdc++.h>
using namespace std;
#define FAST ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
typedef long long LL;
typedef pair<int, int> PII;
const int N = 2e3 + 10;
int n, m, a[N];
int p[N];
int find(int x)
{
if (p[x] != x)
p[x] = find(p[x]);
return p[x];
}
void solve()
{
cin >> n >> m;
int ct = 0;
for (int i = 1; i <= 2 * n; i++)
p[i] = i;
while (m--)
{
string s;
int x, y, z;
cin >> s >> x >> y;
if (s == "F")
{
p[find(x)] = find(y);
}
else
{
p[find(x + n)] = find(y);//合并反集
p[find(y + n)] = find(x);
}
}
for (int i = 1; i <= n; i++)
{
if (p[i] == i)//祖先是自己
ct++;
}
cout << ct << endl;
}
signed main()
{
FAST;
int T = 1;
// cin>>T;
while (T--)
{
solve();
}
}
判断二分图
#include <bits/stdc++.h>
using namespace std;
#define FAST ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
typedef long long LL;
typedef pair<int, int> PII;
const int N = 2e3 + 10;
int n, m, a[N];
int p[N];
int idx = 0;
int find(int x)
{
if(p[x]!=x) p[x]=find(p[x]);
return p[x];
}
void solve()
{
cin >> n >> m;
for (int i = 1; i <= 2 * n; i++)
p[i] = i;
//放上反集如果形成奇数环就结束(二分图无奇数环)
while (m--)
{
int u, v;
cin >> u >> v;
p[find(u + n)] = find(v);
p[find(v + n)] = find(u);
}
for (int i = 1; i <= n;i++)
{
if(find(i+n)==find(i))//判断是否与反集相连 相连的话形成奇数环结束
{
cout<<"case "<<++idx<<":"<<endl<<"No"<<endl;
return ;
}
}
cout<<"case "<<++idx<<":"<<endl<<"Yes"<<endl;
}
signed main()
{
FAST;
int T = 1;
cin >> T;
while (T--)
{
solve();
}
}
二分图+并查集判断二分图应用
#include <bits/stdc++.h>
using namespace std;
#define FAST ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
typedef long long LL;
typedef pair<int, int> PII;
const int N = 4e4 + 10,M=2e5+10;
int n, m, a[N];
int p[N];
int find(int x)
{
if(p[x]!=x) p[x]=find(p[x]);
return p[x];
}
struct node
{
int a,b,c;
bool operator <(const node&A) const {
return c>A.c;
}
}s[M];
void solve()
{
cin>>n>>m;
for(int i=1;i<=2*n;i++) p[i]=i;
for(int i=1;i<=m;i++)
{
int x,y,z;
cin>>x>>y>>z;
s[i]={x,y,z};
}
sort(s+1,s+m+1);
for(int i=1;i<=m;i++)
{
int a=s[i].a,b=s[i].b,c=s[i].c;
if(find(a)==find(b))
{
cout<<c<<endl;
return ;
}
else
{
p[find(a+n)]=find(b);
p[find(b+n)]=find(a);
}
}
cout<<"0"<<endl;
}
signed main()
{
FAST;
int T = 1;
// cin>>T;
while (T--)
{
solve();
}
}