• hdu 1213 How Many Tables


    #include<stdio.h>
    int bin[1024];
    int findx(int x)
    {
        while(bin[x]!=x)
            x=bin[x];
        return x;
    }
    void mergeab(int x,int y)
    {
        int fx,fy;
        fx=findx(x);
        fy=findx(y);
        if(fx>fy)
        bin[fx]=fy;
        else bin[fy]=fx;
    }
    int main()
    {
        int i,n,m,ans,a,b,T;
        scanf("%d",&T);
        while(T--)
        {
            scanf("%d%d",&n,&m);
            for(i=1;i<=n;i++)
                bin[i]=i;
            for(i=0;i<m;i++)
            {
                scanf("%d%d",&a,&b);
                mergeab(a,b);
            }
            for(i=1,ans=0;i<=n;i++)
            {
                if(bin[i]==i) ans++;
            }
            printf("%d
    ",ans);
        }
        return 0;
    }
    

    版权声明:本文为博主原创文章,未经博主允许不得转载。http://xiang578.top/

  • 相关阅读:
    117. Populating Next Right Pointers in Each Node II
    50. Pow(x, n)
    494. Target Sum
    Word Ladder
    HDU 4417
    POJ 2104
    POJ 3277
    【转】图论500题
    POJ 2991
    POJ 1436
  • 原文地址:https://www.cnblogs.com/xryz/p/4848015.html
Copyright © 2020-2023  润新知