问题描述
输入
输出
样例输入
4 1 2 3 4 5 6 1 6 4 1 2 3 4 5 6 7 8
样例输出
4 2
提示
A and B are friends(direct or indirect), B and C are friends(direct or indirect), then A and C are also friends(indirect). In the first sample {1,2,5,6} is the result. In the second sample {1,2},{3,4},{5,6},{7,8} are four kinds of answers. 简单的并查集:#include <iostream> #include <cstdio> #include <cstring> using namespace std; const int maxx=10000005; int MAX; int fa[maxx]; int num[maxx]; int find(int a) { if(fa[a]!=a) fa[a]=find(fa[a]); return fa[a]; } void Union(int x,int y) { int fx=find(x); int fy=find(y); if(fx!=fy) { fa[fx]=fy; num[fy]+=num[fx]; MAX=max(num[fy],MAX); } } int main() { int n; while(scanf("%d",&n)!=EOF) { MAX=0; if(n==0) { cout<<1<<endl; continue; } for(int i=1;i<maxx;i++) { fa[i]=i; num[i]=1; } for(int i=1;i<=n;i++) { int a,b; scanf("%d%d",&a,&b); Union(a,b); } cout<<MAX<<endl; } }