UVa : 924 (Spreading the News)



// http://uva.onlinejudge.org/external/9/924.html
// Runtime: 0.016s
// Tag: BFS


// @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 INF_MAX 2147483647
#define INF_MIN -2147483647
#define pi acos(-1.0)
#define N 1000
#define LL long long

#define For(i, a, b) for( int i = (a); i < (b); i++ )
#define Fors(i, sz) for( size_t i = 0; i < sz.size (); i++ )
#define Fore(it, x) for(typeof (x.begin()) it = x.begin(); it != x.end (); it++)
#define Set(a, s) memset(a, s, sizeof (a))
#define Read(r) freopen(r, "r", stdin)
#define Write(w) freopen(w, "w", stdout)

using namespace std;

int employee;
vector <int> matrix [2500 + 5];
int heard_in_days [2500 + 5];

void bfs (int s)
{
    queue <int> q;
    q.push (s);

    heard_in_days [s] = 0;

    while ( !q.empty () ) {
        int p = q.front (); q.pop ();

        Fors (i, matrix [p]) {
            if ( heard_in_days [matrix [p] [i]] == INF_MIN ) {
                heard_in_days [matrix [p] [i]] = heard_in_days [p] + 1;
                q.push (matrix [p] [i]);
            }
        }
    }
}

void print_output (int s)
{
    int frq [2500 + 5]; Set (frq, 0);

    for ( int i = 0; i < 2500; i++ ) {
        if ( heard_in_days [i] != INF_MIN ) frq [heard_in_days [i]]++;
    }

    int maxi = INF_MIN;
    int ind;

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

    if ( matrix [s].size () == 0 ) printf ("0\n");
    else printf ("%d %d\n", frq [ind], ind);
}

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

    for ( int i = 0; i < employee; i++ ) {
        int n; scanf ("%d", &n);
        while ( n-- ) {
            int inp; scanf ("%d", &inp);
            matrix [i].push_back (inp);
        }
    }

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

    while ( testCase-- ) {
        for ( int i = 0; i < 2505; i++ ) heard_in_days [i] = INF_MIN;
        int start; scanf ("%d", &start);
        bfs (start);
        print_output (start);
    }


	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