您的位置:首页 > 其它

uva11518 Dominos 2

2015-02-18 21:16 316 查看
11518 Dominos 2

Dominos are lots of fun. Children like to stand the tiles on their side in long lines. When one domino

falls, it knocks down the next one, which knocks down the one after that, all the way down the line.

However, sometimes a domino fails to knock the next one down. In that case, we have to knock it down

by hand to get the dominos falling again.

Given a set of dominos that are knocked down by hand, your task is to determine the total number

of dominos that fall.

Input

The rst line of input contains one integer specifying the number of test cases to follow. Each test case

begins with a line containing three integers n, m, l no larger than 10 000, followed by m + l additional

lines. The rst integer n is the number of domino tiles. The domino tiles are numbered from 1 to n.

Each of the m lines after the rst line contains two integers x and y indicating that if domino number

x falls, it will cause domino number y to fall as well.

Each of the following l lines contains a single integer z indicating that the domino numbered z is

knocked over by hand.

Output

For each test case, output a line containing one integer, the total number of dominos that fall over.

Sample Input

1

3 2 1

1 2

2 3

2

Sample Output

2

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
using namespace std;
#define maxn 10000005
int n,m,l;
int T;
int mp[maxn];
int first[maxn],nxt[maxn],to[maxn],e;
bool vis[maxn];
void add(int u,int v){
to[e]=v;
nxt[e]=first[u];
first[u]=e++;
}
void dfs(int s){
vis[s]=true;
for(int i=first[s];~i;i=nxt[i]){
int v=to[i];
if(!vis[v]){
dfs(v);
}
}
}
int main()
{
int u,v;
freopen("in.txt","r",stdin);
scanf("%d",&T);
while(T--){
e=0;
memset(vis,0,sizeof vis);
memset(first,-1,sizeof first);
scanf("%d%d%d",&n,&m,&l);
for(int i=0;i<m;i++){
scanf("%d%d",&u,&v);
add(u,v);
}
int sum=0;
int ss;
for(int i=0;i<l;i++){
scanf("%d",&ss);
dfs(ss);
}
for(int i=1;i<=n;i++){
if(vis[i]==1)sum++;
}
printf("%d\n",sum);
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: