Saturday, December 10, 2016

Most easy Disjoint Set Union Problem. UVa-793-Network Connections.

C++ Source Code.
#include<bits/stdc++.h>
using namespace std;

int par[11111];

int find(int a)
{
    if(par[a]==a)
        return a;
    par[a] = find(par[a]);
    return par[a];
}

int main()
{
    int test,i;
    int n,m,computer,u,v;
    int ans1=0,ans2=0;

    char c;
    scanf("%d",&test);

    while(test--)
    {

        scanf("%d",&computer);
        getchar();
        for(i=1; i<=computer; i++)
        {
            par[i] = i;
        }
        ans1=0,ans2=0;
        while((c = getchar())&&isalpha(c))
        {

            scanf("%d%d",&n,&m);
            getchar();
            if(c=='c')
            {
                u = find(n);
                v = find(m);
                par[u] = v;
            }
            else
            {
                u = find(n);
                v = find(m);

                if(u==v)
                {
                    ans1++;
                }
                else
                    ans2++;
            }
        }

        printf("%d,%d\n",ans1,ans2);
        if(test)
            printf("\n");
    }

    return 0;
}

2 comments:

  1. Slots: Best Casino Games Online | DRMCD
    Top Slots. Here you 오산 출장마사지 can 춘천 출장샵 find hundreds of different slot games from the top casino games and 목포 출장안마 more! Play Slots with Real Money 동해 출장마사지 or 충청북도 출장샵 Bitcoin, Enjoy Progressive Jackpots!

    ReplyDelete
  2. Woori Casino Review - Online slots from PlayTech
    Woori poormansguidetocasinogambling casino is an online sol.edu.kg casino that offers games from Playtech. View the latest woori casino review novcasino for real 토토 사이트 money https://octcasino.com/ here.

    ReplyDelete