uva11518 Dominos 2
来源:互联网 发布:淘宝名忘了怎么找回 编辑:程序博客网 时间:2024/06/05 05:35
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
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 10000005int 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); }}
0 0
- uva11518 Dominos 2
- Dominos 2(DFS)(容器)
- UVa 11518 - Dominos 2
- uva 11504 - Dominos (scc)
- UVa 11504 - Dominos
- UVA11504- Dominos(Tarjan+缩点)
- Dominos<并查集求解>
- Dominos 拓扑排序 强连通 缩点
- UVa 11504 - Dominos(缩点)
- UVA 11504 - Dominos(强连通分量)
- UVA - 11504 Dominos 强连通分量
- uva 11504 - Dominos(强联通分量)
- (beginer)DFS (强连通)UVA 11504 Dominos
- 2
- 2
- 2
- 2
- 2
- 1339 - Ancient Cipher
- Tomcat配置虚拟主机
- 12412 - A Typical Homework (a.k.a Shi Xiong Bang Bang Mang)
- POJ 3070 Fibonacci (矩阵快速幂 Fibonacci数列新求法)
- Activity之间数据交流(onActivityResult的用法)
- uva11518 Dominos 2
- zoj 2008 && poj 1511 Invitation Cards
- centos6.5+nginx1.7+php5.6+mysql5.6
- coreData详解
- 一般处理程序中使用Session
- Gentoo内核配置驱动篇
- NYOJ 237 游戏高手的烦恼 && POJ3041-Asteroids ( 二分图的最大匹配 )
- 目标导向的交互设计:About face 3 -- The essentials of interaction design 读书分享
- 201 - Squares