首页 > 代码库 > UVA 10608

UVA 10608

并查集的水题,比赛时怎么都AC不了,后来发现是没有考虑先3 2 后1 2的情况,下次注意!

#include<iostream>#include<algorithm>#include<cstring>using namespace std;int x[30010];int k[30010];int num[30010];int find(int y){    int r= y;    while(x[r]!= r)    {        r= x[r];    }    /*int i = y;    while(i!= r)    {        int j = x[i];        x[i] = r;        i = j;    }*/    return r;}void merge(int a,int b){    a= find(a);    b= find(b);    if(a!= b)    {        x[a] = b;        num[a]+=num[b];        num[b]=num[a];    }}int main(){    int n ;    cin >> n;    while(n--)    {        for(int i =0; i< 30001;i++){k[i] = 0;num[i] = 1;}        int a,b;        cin >>a>> b;        for(int i =1; i<= a;i++)        {            x[i] = i;        }        int p,q;        for(int i =0;i< b;i++)        {            cin >> p>> q;            merge(p,q);        }        int max = 0;        //cout<< num[2];        for(int i =1; i<30010;i++)        {            if(num[i] > max)            {                max = num[i];                //cout<<num[i];            }        }        cout<< max<< endl;    }    return 0;}