HDU : 1704 (Rank)


// http://acm.hdu.edu.cn/showproblem.php?pid=1704

// @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 <utility>
#include <set>
#include <math.h>
#define pi acos(-1.0)
#define N 1000000
using namespace std;

enum Color {Black, White, Gray} color [500 + 10];
int matrix [500 + 10] [500 + 10];
int current;
int n;

void reset ()
{
    for ( int i = 0; i < 510; i++ ) {
        for ( int j = 0; j < 510; j++ )
            matrix [i] [j] = 0;
    }
}

void dfs (int u)
{
    color [u] = Gray;
    matrix [current] [u] = 1; // current wins u
    matrix [u] [current] = -1;

    for ( int v = 1; v <= n; v++ ) {
        if ( matrix [u] [v] == 1 ) {
            if ( color [v] == White )
                dfs (v);
        }
    }

    color [u] = Black;
}

void colorReset ()
{
    for ( int i = 0; i < 510; i++ )
        color [i] = White;
}

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

    while ( testCase-- ) {
        reset ();

        int m;
        scanf ("%d %d", &n, &m);

        for ( int i = 0; i < m; i++ ) {
            int a, b;
            scanf ("%d %d", &a, &b);

            matrix [a] [b] = 1;
            matrix [b] [a] = -1;
        }

        for ( int i = 1; i <= n; i++ ) {
            current = i;
            colorReset ();
            dfs (i);
        }

        int cnt = 0;

        for ( int i = 1; i <= n; i++ ) {
            for ( int j = i + 1; j <= n; j++ ) {
                if ( matrix [i] [j] == 0 )
                    cnt++;
            }
        }

        printf ("%d\n", cnt);

    }

    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