您的位置:首页 > 其它

uva 1160 - X-Plosives

2013-10-24 10:50 267 查看
并查集的简单应用:

代码:

#include<cstdio>
#define maxn 100005
using namespace std;
int f[maxn];
int find(int x){return x==f[x]?x:f[x]=find(f[x]);}
int main()
{
int x,y;
while(scanf("%d",&x)!=EOF)
{
for(int i=0;i<maxn;i++)f[i]=i;
int cnt=0;
while(x!=-1)
{
scanf("%d",&y);
x=find(x),y=find(y);
if(x==y)cnt++;
else f[x]=y;
scanf("%d",&x);
}
printf("%d\n",cnt);
}
return 0;
}


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