Saturday, June 25, 2016

UVA 10583 Solution

Unknown
#include<cstdio>
using namespace std;

int parent[50001];

int findparent(int n)
{
    return parent[n]==n? n: findparent(parent[n]);
}

int main()
{
    int n,m,tc=1,a,b;
    while (scanf("%d%d",&n,&m)!=EOF)
    {
        if (m+n==0) return 0;
        for (int i=1;i<=n;i++) parent[i]=i;
        while (m--)
        {
            scanf("%d%d",&a,&b);
            parent[findparent(a)]=findparent(b);
        }
        int r=0;
        for (int i=1;i<=n;i++)
            if (parent[i]==i) r++;
       
        printf("Case %d: %d\n",tc++,r);
       
    }
   
    return 0;
}

Unknown

Studying at Shahjalal University of Science and Technology, Sylhet.

0 comments:

Post a Comment

Coprights @ 2016,