您的位置:首页 > 其它

uvalive 3644 X-Plosives

2015-09-15 19:42 288 查看
思路:简单的并查集应用,两个点有相同的祖先是组成环,会引起爆炸,不能放,num++。

#include <iostream>
#include <cstring>
#include <stdio.h>
using namespace std;
int f[100008];
int find(int x)
{
return x==f[x]?x:f[x]=find(f[x]);
}
int init()
{
for(int i=0;i<100008;i++)
f[i]=i;
return 1;
}
int main()
{
//freopen("in.txt","r",stdin);
int x,y,num=0;
init();
while(scanf("%d",&x)!=EOF)
{
if(x==-1)
{
printf("%d\n",num);
num=0;
init();
continue;
}
scanf("%d",&y);
int xx=find(x);
int yy=find(y);
if(xx==yy)
num++;
else
f[xx]=yy;
find(x);
}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: