LightOj 1111 - Best Picnic Ever
来源:互联网 发布:eclipse编译java 编辑:程序博客网 时间:2024/05/17 03:24
/*** 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 ;}