并查集&带权并查集&种类并查集 入门基础题

HDU 3038 How Many Answers Are Wrong(带权并查集)

 

#include <iostream>
#include <algorithm>
#include <math.h>
#include <vector>
#include<cstdio>
#include <cstring>
#include <queue>
#include<bitset>
typedef long long ll;

using namespace std;
const int inf=0x3f3f3f3f;
 const int maxn  = 2e5+100;
int dp[(1<<20)][20];
int g[100][100];
int n,m;
int f[maxn],sum[maxn];
int find(int x)
{
    if(f[x]==x)
        return x;
    int z=f[x];
    f[x]=find(f[x]);//状态压缩
   sum[x]+=sum[z];//更新权值(x到根节点的距离)
    return f[x];
}

int main()
{
    while(cin>>n>>m)
    {
    int ans=0;
    for(int i=1;i<=n+1;i++)//注意加一
        f[i]=i;
    memset(sum, 0, sizeof(sum));
    for(int i=1;i<=m;i++)
    {
        int a,b,c;
        cin>>a>>b>>c;
        b++;   //向后到最大节点的距离,所以要加1
        int fa=find(a);
        int fb=find(b);
        if(fa==fb&&sum[a]!=sum[b]+c)//与已有的权值不想等
            ans++;
        else if(fa!=fb)
        {
            if(fa>fb)
            {
                f[fb]=fa;//合并
                sum[fb]=sum[a]-sum[b]-c;//因为a<b,所以a<b<fb<fa
            }
            else
            {
                f[fa]=fb;
                sum[fa] = sum[b]-sum[a]+c;//a<b<fa<fb,画一条横坐标有助于理解
            }
        }
    }
    cout<<ans<<endl;
    }
    return 0;
}

http://acm.hdu.edu.cn/showproblem.php?pid=2860

HDU 2860 Regroup(简单并查集)

 

【HDOJ】3047 Zjnu Stadium(带权并查集)

#include <iostream>
#include <algorithm>
#include <math.h>
#include <vector>
#include<cstdio>
#include <cstring>
#include <queue>
#include<bitset>
typedef long long ll;

using namespace std;
const int inf=0x3f3f3f3f;
 const int maxn  = 2e5+100;
int dp[(1<<20)][20];
int g[100][100];
int n,k,m;
int f[maxn],r[maxn];
int find(int x)
{
      if(x!=f[x]){
      int z=f[x];
      f[x]=find(f[x]);
      r[x]+=r[z];//更新x的位置
      }
  return f[x];
}

int main()
{
    while(~scanf("%d%d",&n,&m))
    {
        for(int i=0;i<=n;i++)
        {
            f[i]=i;
            r[i]=0;//设置根结点的位置为0
        }
        int cnt=0;
        for(int i=0;i<m;i++)
        {
            int a,b,c;
            scanf("%d%d%d",&a,&b,&c);
            int fa=find(a);
            int fb=find(b);
            if(fa!=fb)
            {
                f[fb]=fa;
                r[fb]=r[a]+c-r[b];//b与fb(b根)的距离为r【b】,与a根的距离为r【a】+c,所以fb与根的距离为r[a]+c-r[b]
            }
            else
            {
                if(r[a]+c!=r[b])//b不在a后距离c处
                    cnt++;
            }
        }
        cout<<cnt<<endl;
    }
    return 0;
}

POJ-1182 食物链(种类并查集)

#include <cstdio>
#include <iostream>

using namespace std;

const int N = 50005;
int father[N];
int relation[N];//根点节到点节的关系

void init(int n)
{
    for(int i = 0; i <= n; ++i)
    {
        father[i]= i;
        relation[i] = 0;
    }
}
//更新的步调,先将当前点节与其根点节相连,然后更新其与根点节的关系
//当前节点x与根节点r的关系更新的方法:
//    (x与其父点节的关系+其父点节的关系与根点节的关系)%3
//所以在更新节点x的数据之前需要更新其父节点的数据,这是find为什么搞成递归函数的原因
//其更新的次序是从根节点开始往下,始终到当前点节x的父点节。
int find(int x)
{
    if(x != father[x])//不是根点节
    {
        int temp = father[x];
        //将当前点节的父点节设置为根点节
        father[x] = find(temp);
        //更新当前点节与根点节的关系,由x->x父和x父->父根的关系失掉x->父根的关系
        //所以在这之前必须更新其父点节与根点节的关系
        relation[x] = (relation[x] + relation[temp]) % 3;
    }
    return father[x];
}

int main()
{
    int n, m, x, y, d, fx, fy, cnt;

    while(~scanf("%d %d", &n, &m))//POJ上只要需一次入输,所以不要需while循环
    {
        cnt = 0;
        init(n);
        for(int i = 0; i < m; ++i)
        {
            scanf("%d %d %d", &d, &x, &y);
            if(x > n || y > n)
            {
                ++cnt;
                continue;
            }
            if(d == 2 && x == y)
            {
                ++cnt;
                continue;
            }
            fx = find(x);
            fy = find(y);
            if(fx == fy)//属于同一个子集
            {
                //如果x、y是同类,那么他们相对根点节的关系应该是一样的
                if(d == 1 && relation[x] != relation[y])
                    ++cnt;
                //如果不是同类,加入x与y的关系之后,x相对根点节的关系(x根->y,y->x(即3-(d-1)=2).即x根->x)应该是不变的
                //这里d=2表示x - y = 2-1=1;而y->x=3-(x->y)=3-1=2;
                if(d == 2 && relation[x] != (relation[y] + 2)%3)
                    ++cnt;
            }
            else//合并两个连通区域
            {
                father[fy] = fx;//y根的父点节更新成x根
                //(d-1)为x与y的关系,3-relation[y]是y与y的根点节的关系,注意方向,relation[x]是其根点节与x的关系
                //x根->x,x->y,y->y根:即x根->y根
                relation[fy] = (relation[x] + (d-1) + (3-relation[y])) % 3;//注意这里只更新的是fy相对于根的关系
            }
        }
        printf("%d\n", cnt);
    }
    return 0;
}

POJ-1182  食物链(种类并查集)

#include <iostream>
#include <algorithm>
#include <math.h>
#include <vector>
#include<cstdio>
#include <cstring>
#include <queue>
#include<bitset>
typedef long long ll;

using namespace std;
const int inf=0x3f3f3f3f;
 const int maxn  = 2e5+100;
int dp[(1<<20)][20];
int g[100][100];
int n,k,m,flag;
int f[maxn],r[maxn];

//所有节点与根节点性别相反
int find(int x)
{
    if(x!=f[x])
    {
        int t=f[x];
        f[x]=find(f[x]);
        r[x]=(r[t]+r[x])%2;//更新性别
        //r[x]=r[t]^r[x];
    }
    return f[x];
}


int main()
{
    int q;
    scanf("%d",&q);
    int cnt=1;
    while(q--)
    {

        flag=1;
        scanf("%d%d",&n,&m);
        for(int i=0;i<=n;i++)
        {
            f[i]=i;
            r[i]=0;//0和1来表示性别
        }
        for(int i=0;i<m;i++)
        {
            int a,b;
            scanf("%d%d",&a,&b);//配对
            int fa=find(a);
            int fb=find(b);
            if(fa!=fb)//未在一个集合内配对
            {
                f[fb]=fa;
                r[fb]=(r[a]+1-r[b])%2;//维护消失的根节点,a与fa相反,fa与fb相反,fb与b相反 用亦或更好
                //r[fb]=r[a]^r[b]^1;
            }
            else
            {
               if(r[a]==r[b])//如果性别相同则失败
                  flag=0;
            }
        }
        printf("Scenario #%d:\n",cnt);
        if(flag)
            printf("No suspicious bugs found!\n");
        else
          printf("Suspicious bugs found!\n");
        printf("\n");
        cnt++;
    }
    return 0;
}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值