UVa : 11981 (Corrupted Friendship)



// http://uva.onlinejudge.org/external/119/11981.html
// Runtime: 0.168s
// 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 100010
#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;

vector <int> v [N];
int friends_top [N];        // number of friends at top
int friends_bottom [N];     // number of friends at bottom
int out_degree [N];         // number of edges going out
bool vis [N];               // visited node

void reset ()
{
    for ( int i = 0; i < N; i++ ) {
        v [i].clear ();
        out_degree [i] = 0;
        friends_bottom [i] = 0;
        vis [i] = false;
    }

    friends_top [1] = 0;
}

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

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

        reset ();

        for ( int i = 1; i < n; i++ ) {
            int a, b;
            scanf ("%d %d", &a, &b);
            out_degree [a]++;
            v [a].push_back (b);
            v [b].push_back (a);
        }

        queue <int> q;
        q.push (1);
        vis [1] = true;

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

            for ( size_t i = 0; i < v [p].size (); i++ ) {
                if ( !vis [v [p] [i]] ) {
                    friends_top [v [p] [i]] = friends_top [p] + 1;
                    q.push (v [p] [i]);
                    vis [v [p] [i]] = true;
                }
            }
        }

        Set (vis, false);

        for ( int i = 1; i <= n; i++ ) {
            if ( out_degree [i] == 0 )
                q.push (i);
        }

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

            vis [p] = true;

            for ( size_t i = 0; i < v [p].size (); i++ ) {
                if ( !vis [v [p] [i]] ) {
                    friends_bottom [v [p] [i]] += (friends_bottom [p] + 1);
                    out_degree [v [p] [i]]--;
                    if ( out_degree [v [p] [i]] == 0 ) q.push (v [p] [i]);
                }
            }
        }

        LL pairs = 0;

        for ( int i = 1; i <= n; i++ )
            pairs += (n - (friends_top [i] + friends_bottom [i] + 1));

        printf ("Case %d: %d ", ++cases, n - 1);
        cout << pairs / 2 << endl;
    }
	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