首页 > 其他 > 详细

uva 1106(并查集)

时间:2017-10-31 19:17:48      阅读:196      评论:0      收藏:0      [点我收藏+]

我是傻逼,少删一个注释找一年bug

#include <iostream>
#include <cstring>
#include <algorithm>
#include <cstdio>
using namespace std;
const int maxn=1e5+100;
int c[maxn];
int a,b;
int found(int x)
{
    if(x==c[x]) return x;
    else return c[x]=found(c[x]);
}
bool same(int x,int y)
{
    return found(x)==found(y);
}
void init()
{
    for(int i=0;i<=100000;i++)
      c[i]=i;
}
void unit(int x,int y)
{
    x=found(x);
    y=found(y);
    if(x==y) return;
    c[x]=y;
}
int main()
{
    while(~scanf("%d",&a))
    {   int ans=0;
         init();
        while(a!=-1)
        {
            scanf("%d",&b);
            if(!same(a,b)) unit(a,b);
            else ans++;
            scanf("%d",&a);
        }
       printf("%d\n",ans);
    }

    return 0;
}

 

uva 1106(并查集)

原文:http://www.cnblogs.com/Wangwanxiang/p/7762845.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!