UVa 10926 - How Many Dependencies?

/*
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.

*/

 // Accepted
 // DFS

#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
#define mset(c,v) memset(c , v , sizeof c)
#define loop0(n) for(int i=0; i<n; i++)
#define loop1(n) for(int i=1; i<=n; i++)
#define mpair(x , y) make_pair(x , y)
#define all(x) x.begin(), x.end()
#define pi acos(-1.0)
#define psb push_back

typedef unsigned long long ull;
typedef long long LL;
typedef vector<int>vii;
typedef vector<LL>vll;
typedef vector<string>vs;
typedef map<int, int>mpii;
typedef map<string, int>mpsi;
typedef map<char, int>mpci;
typedef map<LL, LL>mpll;

const int mod = 1000007;
const int high = 102;

vii adj[high];
int visited[high] , cost[high] , cnt=0;

void DFS(int u)
{
    visited[u] = 1;

    vii::iterator it;

    for(it=adj[u].begin(); it != adj[u].end(); it++)
    {
        int v = *it;

        if(!visited[v])
        {
            visited[v] = 1;
            cnt+=1;
            DFS(v);
        }
    }
}

void CLR()
{
    for(int i=0; i<=high; i++)
    {
        adj[i].clear();
    }
}

int main()
{
    fast;
    int N , T , i , x;
    while(cin >> N && N)
    {
        cnt = 0;
        CLR();
        mset(cost, 0);

        for(i=1; i<=N; i++)
        {
            cin >> T;

            while(T--)
            {
                cin >> x;

                adj[i].psb(x);
            }
        }

        for(i=1; i<=N; i++)
        {
            mset(visited , 0);

            cnt = 0;

            DFS(i);

            cost[i] = cnt;
        }

        //for(i=1; i<=N; i++) cout << cost[i] << "; ";
        int mx = -1 , idx=1;

        for(i=1; i<=N; i++)
        {
            if(mx < cost[i])
            {
                mx = cost[i];
                idx = i;
            }
        }

        cout << idx << "\n";
    }

    return 0;
}

Comments

Popular posts from this blog

SPOJ-CMG - Collecting Mango

LightOJ 1009 - Back to Underworld

LeetCode Palindrome Number