UVa : 11518 (Dominos 2)


// http://uva.onlinejudge.org/external/115/11518.html

// @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;

vector <int> dependentList [10000 + 10];
bool down [10000 + 10];

void recur (int k)
{
    if ( down [k] ) return;

    down [k] = true;

    for ( size_t i = 0; i < dependentList [k].size (); i++ ) {
        recur (dependentList [k] [i]);
    }
}

void reset ()
{
    memset (down, false, sizeof (down));

    for ( int i = 0; i < 10010; i++ )
        dependentList [i].clear ();
}

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

    while ( testCase-- ) {
        int n, m, l;

        cin >> n >> m >> l;

        reset ();

        for ( int i = 0; i < m; i++ ) {
            int p, q;
            cin >> p >> q;

            dependentList [p].push_back (q);
        }

        for ( int i = 0; i < l; i++ ) {
            int k;
            cin >> k;

            recur (k);
        }

        int fallOver = 0;

        for ( int i = 1; i <= n; i++ ) {
            if ( down [i] )
                fallOver++;
        }

        cout << fallOver << endl;
    }

    return 0;
}

// @END_OF_SOURCE_CODE

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