Friday, April 24, 2015

UVa - 793 - Network Connections

#include <bits/stdc++.h>
#define L 100003
using namespace std;

int RP[L];

int find_set(int n){
    if(RP[n] == n)return n;
    return RP[n] = find_set(RP[n]);
}

void merge_set(int p, int q){
    int u = find_set(p);
    int v = find_set(q);
    if(u != v)RP[v] = u;
}

int main(){
    int n, x, y, t, sp = 0;
    char check[L], q;
    cin >> t;
    while(t--){
        cin >> n;
        int yes = 0, no = 0;
        for(int i = 1; i <= n; i++)RP[i] = i;
        getchar();
        while(true){
            gets(check);
            if(strcmp(check, "") == 0 || feof(stdin))break;
            sscanf(check, "%c %d %d", &q, &x, &y);
            if(q == 'c')merge_set(x, y);
            else{
                (find_set(x) == find_set(y)) ? yes++ : no++;
            }
        }
        if(sp)puts(""); sp = 1;
        cout << yes << "," << no << endl;
    }
    return 0;
}

No comments:

Post a Comment