uva 1160 - X-Plosives(並查集)

簡單的並查集。

#include<cstdio>
#define mx 100010

int fa[mx];

int find(int x)
{
	while(fa[x]!=x) x=fa[x];
	return x;
}

int main()
{
	int i,x,y,cnt;
	while(scanf("%d",&x)!=EOF)
	{
		for(i=0;i<mx;i++) fa[i]=i;
		cnt=0;
		while(x!=-1)
		{
			scanf("%d",&y);
			x=find(x);y=find(y);
			if(x==y) cnt++;
			else fa[x]=y;
			scanf("%d",&x);
		}
		printf("%d\n",cnt);
	}
	return 0;
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章