UVA 10608

并查集简单题
#include <iostream>
#include <cstdio>
using namespace std;
#define max 30010
int f[max];
int getf(int k){
    while(k!=f[k]){
        k=f[k];
    }
    return k;
}
void combine(int a,int b){
    int roota=getf(a);
    int rootb=getf(b);
    if(roota>rootb)f[roota]=rootb;
    else if(roota<rootb)f[rootb]=roota;
}
int main(){
    int a,b,t,n,m;
    //freopen("test.txt","r",stdin);
    cin>>t;
    while (t--){
        cin>>n>>m;
        for(int i=1;i<=n;i++){
            f[i]=i;
        }
        for(int i=1;i<=m;i++){
            cin>>a>>b;
            combine(a,b);
        }
        int max1=0;
        int c=0;
        for(int i=1;i<=n;i++) {
            if(f[i]==i){
                for(int j=i;j<=n;j++){
                    if(getf(f[j])==i){c++;}
                }
            }
            if(c>max1)max1=c;
            c=0;
        }
        cout<<max1<<endl;
    }
    return 0;
}

posted @ 2014-07-27 21:25  Mr.XuJH  阅读(119)  评论(0编辑  收藏  举报