LightOj 1111 - Best Picnic Ever

/**
*   Author: johnsondu
*   time: 2013-4-11
*   problem: LightOj 1111 - Best Picnic Ever
*   url: http://lightoj.com/volume_showproblem.php?problem=1111
*   stratege: DFS, count the person who can run into city i, and
*               let the corresponding dp[i] plus 1, finally, when
*               city i meet the problem need, dp[i] must be the person
*               number.
*/

#include <iostream>
#include <algorithm>
#include <string>
#include <vector>
#include <cstring>
#include <cstdio>

#define M 1005
#define inf 0xfffffff
#define min(x, y) (x < y ? x : y)
using namespace std ;
int mat[M][M], person[M] ;
int n, m, t, ans ;
bool mark[M] ;
bool flag, flag1 ;
int dp[M] ;

void init ()
{
    int a, b, i ;
    ans = 0;
    for (int i = 0; i < t; i ++)
        scanf ("%d", &person[i]) ;

    memset (mat, 0, sizeof (mat)) ;
    for (i = 0; i < m; i ++)
    {
        scanf ("%d%d", &a, &b) ;
        mat[a][b] = 1 ;
    }
}

void DFS (int x, int y)
{
    for (int i = 1; i <= n; i ++)
    {
        if (x == i)
            continue ;

        if (!mark[i] && mat[x][i])
        {
            mark[i] = true ;
            dp[i] ++ ;
            DFS (i, y) ;
        }
    }
}

void solve ()
{
    int i, j ;
    memset (dp, 0, sizeof (dp)) ;

    for (j = 0; j < t; j ++)
    {
        memset (mark, false, sizeof (mark)) ;
        mark[person[j]] = true ;
        dp[person[j]] ++ ;
        DFS (person[j], person[j]) ;

    }
    for (i = 1; i <= n; i ++)
        if (dp[i] == t)
            ans ++ ;

}

int main ()
{
    int tcase, cas = 1 ;

    //freopen ("data.txt", "r", stdin) ;
    scanf ("%d", &tcase) ;
    while (tcase --)
    {
        scanf ("%d%d%d", &t, &n, &m) ;
        init () ;
        solve () ;

        printf ("Case %d: %d\n", cas ++, ans) ;
    }
    return 0 ;
}

你可能感兴趣的:(LightOj 1111 - Best Picnic Ever)