The Suspects

The Suspects


https://vjudge.net/contest/477987#problem/A

#include<iostream>
using namespace std;
int p[30010],sum[30010];
void init(int n)
{
	for(int i=0;i<n;i++)
	{
		p[i]=i;
		sum[i]=1;
	}
}
int getroot(int a)
{
	if(p[a]!=a)
	{
		p[a]=getroot(p[a]);
	}
	return p[a];
}
void merge(int a,int b)
{
	int x=getroot(a);
	int y=getroot(b);
	if(x!=y)
	{
		p[y]=x;
		sum[x]+=sum[y];
	}
}
int main()
{
	int n,m;
	while(cin>>n>>m)
	{
		if(n==0&&m==0)break;
		init(n);
		while(m--)
		{
			int k,one,two;
			cin>>k>>one;
			k--;
			while(k--)
			{
				cin>>two;
				merge(one,two);
			}
		}
		cout<<sum[getroot(0)]<<endl;
	}
	return 0;
}

 

posted @ 2022-02-11 14:58  是橙橙吖  Views(24)  Comments(0)    收藏  举报