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
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); } }
相关文章推荐
- UVa 11518 - Dominos 2
- (beginer)DFS (强连通)UVA 11504 Dominos
- UVa 11504 - Dominos
- UVa 11504 - Dominos(缩点)
- UVA - 11504 Dominos 强连通分量
- uva 11504 - Dominos (scc)
- UVA 11504 - Dominos(强连通分量)
- uva 11504 - Dominos(强联通分量)
- UVA-11504 - Dominos(有向图的强连通分量)
- uva 10453 Make Palindrome
- uva 101 The Blocks Problem
- UVA 531 - Compromise(dp + LCS打印路径)
- (intermediate) 平面区域 UVA Art of War
- uva10537
- UVa 1590 - IP Networks(进制转换+细节处理)
- uva 1442 Cav(左右各扫描一次)
- 2349 poj &&uva 10369 Arctic Network【最小生成树】
- uvalive4015
- 动态规划(基金管理,uva 1412)
- 紫书章六例题10 下落的树叶 UVA 699 (根据前序遍历建树)