Thursday 24 December 2015

UVa 793 - Network Connections

//\\__ hr1212 __//\\

#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef vector<int> vi;
typedef pair<int,int> pii;
typedef map<int,int> mi;

#define si(a) scanf("%d",&a)
#define sii(a,b) scanf("%d %d",&a,&b)
#define siii(a,b,c) scanf("%d %d %d",&a,&b,&c)
#define pi(a) printf("%d\n",a)
#define nl printf("\n");
#define pb push_back
#define mp make_pair
#define all(c) (c).begin(),(c).end()
#define f(i,a,b) for(i=a;i<b;i++)
#define rf(i,a,b) for(i=a;i>=b;i--)
#define clr(x,a) memset(x,a,sizeof(x))
#define MAX 1000100
#define MOD 1000000007

int n,m,ht[MAX],p[MAX];
char s[MAX];

int findp(int x){
    return (p[x]==x)?x:findp(p[x]);
}

void unionset(int x,int y){
    if(findp(x)!=findp(y)){
        x=findp(x);y=findp(y);
        if(ht[x]>ht[y])
            p[y]=x;
        else{
            p[x]=y;
            if(ht[x]==ht[y])
                ht[y]++;
        }
    }
}

int main(){
    int r,k,i,c=0,x=0,y=0,j,t,l,z,x1=0,y1=0;
    ll ans=0;
    char ch;

    scanf("%d\n",&t);
    while(t--){
        x1=0;y1=0;
        clr(ht,0);
        scanf("%d\n\n",&n);
        f(i,1,n+1)
            p[i]=i;
        while(1){
            gets(s);
            if (strcmp(s, "") == 0 || feof(stdin)) break;
            sscanf(s, "%c %d %d", &ch, &x, &y);
            if(ch=='c')
                unionset(x,y);
            else if(ch=='q'){
                if(findp(x)==findp(y))
                    x1++;
                else
                    y1++;
            }
        }
        printf("%d,%d\n",x1,y1);
        if(t)
            nl;
    }

    return 0;
}

No comments:

Post a Comment