我是靠谱客的博主 合适灯泡,这篇文章主要介绍并查集poj1611The Suspects,现在分享给大家,希望可以做个参考。

#include<iostream>
using namespace std;
const int MAX=30010;
int pre[MAX];
void init(int n){
	for(int i=0;i<n;i++)
	pre[i]=i;
}
int find(int x){
	if(pre[x]==x) return x;
	return pre[x]=find(pre[x]);
}
void unite(int x,int y){
	x=find(x);
	y=find(y);
	if(x==y) return;
	pre[x]=y;
}
bool same(int x,int y){
	return find(x)==find(y);
}
int n,m;
int main(){
	ios::sync_with_stdio(false);
	while(cin>>n>>m&&n){
		init(n);
		int t,tmp,k;
		while(m--){
			cin>>k;
		if(k==0) continue;		
			cin>>t;
			for(int i=1;i<k;i++){
				cin>>tmp;
				unite(tmp,t);
			}		
		}
		int ans=0;
		for(int i=0;i<n;i++){
			if(same(0,i)) ans++;
		}
	cout<<ans<<endl;	
	}
	return 0;
}

最后

以上就是合适灯泡最近收集整理的关于并查集poj1611The Suspects的全部内容,更多相关并查集poj1611The内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(48)

评论列表共有 0 条评论

立即
投稿
返回
顶部