并查集及并查集反集的应用

并查集模板:

#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合并  (敌人的敌人是朋友)

P1892 [BOI2003] 团伙

#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();
  }
}

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值