uva 1106(并查集)

我是傻逼,少删一个注释找一年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;
}

 

posted on 2017-10-31 18:06  发牌员  阅读(125)  评论(0)    收藏  举报

导航