How Many Tables HDU - 1213

时间:2020-02-01 16:18:08   收藏:0   阅读:47
#include<iostream>
using namespace std;
const int N=1010;
int p[N];
int find(int x)
{
    if(p[x]!=x)
        p[x]=find(p[x]);
    return p[x];
}
int main()
{
    int t;
    int n,m;
    cin>>t;
    while(t--)
    {
        cin>>n>>m;
        for(int i=1;i<=n;i++)
            p[i]=i;
        while(m--)
        {
            int a,b;
            cin>>a>>b;
            int pa=find(a);
            int pb=find(b);
            if(pa!=pb)
                p[pa]=pb;
        }
        int res=0;
        for(int i=1;i<=n;i++)
            if(p[i]==i)
                res++;
        cout<<res<<endl;
    }
    return 0;
}

 

评论(0
© 2014 mamicode.com 版权所有 京ICP备13008772号-2  联系我们:gaon5@hotmail.com
迷上了代码!