UVa 10685 - Nature

/*
Lionel Messi is such a player that you may catch him, you may touch him, you may feel him
and definitely you may Love him.
Lionel Messi is Messi. A little Magician in this World.

*/

#include<bits/stdc++.h>

using namespace std;

#define fast ios_base::sync_with_stdio(0)
#define bfast cin.tie(0)
#define outs(x) cout << x << " "
#define outn(x) cout << x << "\n"
#define sf scanf
#define pf printf
#define nl puts("")
#define psb push_back

typedef long long LL;
typedef vector<int>vii;
typedef vector<LL>vll;
typedef unsigned long long ull;
typedef map<string, int>mpsi;

const int mod = 1000007;
const int high = 5003;

int par[high] , num[high];

void set_initial(int n)
{
    int i=0;

    for(i=1; i<=n; i++)
    {
        par[i] = i;
        num[i] = 1;
    }
}

int findd(int n)
{
    if(par[n] == n) return n;
    else return par[n] = findd(par[n]);
}

void unionn(int a , int b)
{
    int u = findd(a);
    int v = findd(b);

    if(u == v) return;

    else
    {
        par[u] = v;

        num[v] += num[u]; // always old parent stores in u and always new parent in v, so just update the num[v], opposite of par[u]
    }
}

char s[high] , rs[high];

int main()
{
    int C , R , i=1;
    mpsi mp;
    //string s , rs;

    while(~sf("%d %d", &C , &R))
    {
        if(!C and !R) break;

        set_initial(C);

        mp.clear();

        for(i=1; i<=C; i++)
        {
            sf("%s", s);

            mp[s] = i;
        }

        while(R--)
        {
            sf("%s %s", s , rs);

            unionn(mp[s] , mp[rs]);
        }

        int mx = 0;

        for(i=1; i<=C; i++)
        {
            mx = max(mx , num[i]);
        }

        pf("%d\n" , mx);
    }

    return 0;
}

Comments

Popular posts from this blog

SPOJ-CMG - Collecting Mango

LightOJ 1009 - Back to Underworld

LeetCode Palindrome Number