1. 程式人生 > >浙江大學 PTA 5-25 朋友圈 (25分)

浙江大學 PTA 5-25 朋友圈 (25分)

#include "iostream"
using namespace std;

int A[30001],B[30001];  //一個存數,一個存節點
  
int Find(int x) 
{
	 if(A[x]!=x)
	 {
		 return Find(A[x]);
	 }
      
	 return x;
}

void f(int a,int b)   //子集和並
{
   int x,y;
   x=Find(a);
   y=Find(b);
   
   if(B[x]>=B[y])
   {    
	   A[y]=x;
	   B[x]+=B[y];
	   
   }
   else 
   {
      A[x]=y;
	  B[y]+=B[x];
   }
}
int main( )
{    

	 int m,n;
	 cin>>m>>n;
     int i,j; 
	 for(i=1;i<=m;i++)
	 {
		 A[i]=i;
		 B[i]=1;
	 }
	 
	 while(n--)
	 {
		 int x1,x2,x3;
		 cin>>x1;
		 if(x1!=0)	  
		 cin>>x2;
		 for(j=1;j<x1;j++)
		 { 
			 cin>>x3;
			 if(Find(x2)!=Find(x3))
			 f(x2,x3);
		 }
	 }
	 
     int max=1;

	 for(i=1;i<=m;i++)
	 {
		 if(max<B[i]) max=B[i];
	 }
	 cout<<max<<endl;

 
    return 0;
}