UVa : 11503 (Virtual Friends)


// http://uva.onlinejudge.org/external/115/11503.html

// @BEGIN_OF_SOURCE_CODE

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <string>
#include <cctype>
#include <stack>
#include <queue>
#include <list>
#include <vector>
#include <map>
#include <sstream>
#include <utility>
#include <set>
#include <math.h>
#define pi acos(-1.0)
#define N 1000000
using namespace std;

int parent [100000 + 10];

int findParent (int n)
{
    if ( parent [n] < 0 )
        return n;
    return parent [n] = findParent(parent [n]);
}

int main ()
{
    int testCase;
    scanf ("%d", &testCase);

    while ( testCase-- ) {
        int frndShipFormed;
        scanf ("%d", &frndShipFormed);

        memset (parent, -1, sizeof (parent));

        int indexing = 1;
        map <string, int> m;

        for ( int i = 0; i < frndShipFormed; i++ ) {
            string first, second;
            cin >> first >> second;

            if ( !m [first] )
                m [first] = indexing++;
            if ( !m [second])
                m [second] = indexing++;

            int p = findParent (m [first]);
            int q = findParent (m [second]);

            if ( p == q ) {
                printf ("%d\n", parent [p] * -1);
                continue;
            }

            if ( parent [p] < parent [q] ) {
                parent [p] += parent [q];
                parent [q] = p;
                printf ("%d\n", parent [p] * -1);
            }
            else {
                parent [q] += parent [p];
                parent [p] = q;
                printf ("%d\n", parent [q] * -1);
            }
        }
    }

    return 0;
}

// @END_OF_SOURCE_CODE
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s