UVa : 10926 (How Many Dependencies?)



// http://uva.onlinejudge.org/external/109/10926.html
// Runtime : 0.032s
// Tag : dfs


// @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 <cmath>
#include <bitset>
#include <utility>
#include <set>
#include <numeric>
#define INT_MAX 2147483647
#define INT_MIN -2147483647
#define pi acos(-1.0)
#define N 1000000
#define LL unsigned long long
using namespace std;

bool matrix [100 + 3] [100 + 3];
bool vis [100 + 3];
int n;

int countVis ()
{
    int ret = 0;

    for ( int i = 1; i <= n; i++ )
        if ( vis [i] ) ret++;

    return ret;
}

void dfs (int u)
{
    vis [u] = true;

    for ( int i = 1; i <= n; i++ )
        if ( matrix [u] [i] && !vis [i] ) dfs (i);
}

int main ()
{
    while ( scanf ("%d", &n) && n ) {
        memset (matrix, false, sizeof (matrix));

        for ( int i = 0; i < n; i++ ) {
            int m;
            scanf ("%d", &m);
            int k;
            for ( int j = 0; j < m; j++ ) {
                scanf ("%d", &k);
                matrix [i + 1] [k] = true;
            }
        }

        int freq [100 + 3];
        memset (freq, 0, sizeof (freq));

        for ( int i = 0; i < n; i++ ) {
            memset (vis, false, sizeof (vis));
            dfs (i + 1);
            freq [i + 1] = countVis ();
        }

        int maxi = INT_MIN;
        int ind;

        for ( int i = 1; i <= n; i++ ) {
            if ( maxi < freq [i] ) { maxi = freq [i]; ind = i; }
        }

        printf ("%d\n", ind);
    }

	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