ZCMU—1900
2017-03-19 22:18
162 查看
1900: Problem D: Dominos
Time Limit: 1 Sec Memory Limit: 128 MB[Submit][Status][Web
Board]
Description
Problem D: Dominos
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 nextone down. In that case, we have to knock it down by hand to get the dominos falling again.
Your task is to determine, given the layout of some domino tiles, the minimum number of dominos that must be knocked down by hand in order for all of the dominos to fall.
Input
The first line of input contains one integer specifying the number of test cases to follow. Each test case begins with a line containing two integers, each no larger than 100 000. The first integer n isthe number of domino tiles and the second integer m is the number of lines to follow in the test case. The domino tiles are numbered from 1 to n.
Each of the following lines contains two integers x and y indicating
that if domino number x falls, it will cause domino number y to
fall as well.
Output
For each test case, output a line containing one integer, the minimum number of dominos that must be knocked over by hand in order for all the dominos to fall.Sample Input
13 2
1 2
2 3
Sample Output
1【分析】
题意:给出多米诺骨牌的关系,x,y,如果x倒下,y也会倒下。首先这道题不要以逻辑去考虑...因为会出现环啊或者是特殊不符合逻辑的情况...
一开始我以为是并查集,但是后来发现一件事,因为并查集的关系是双向的,但是这道题的关系其实是单向的,x倒了y会倒,但是y倒下x是不会倒下的....所以这道题就变成了强连通图,首先把所有环缩成一个点然后.....
但是我这里还是写了并查集,因为可以考虑一点,并查集会出现问题的情况只有一种,也就是1-2,3-2这种情况
这种情况是需要两次的,但是在并查集中它们会变成同一个集合,所以这里可以计算一下在读取时没有父亲的牌,然后并查集后的集合数,输出较大的那个。
但是其实我觉得...理解的话应该这样更容易,可以记录一下某个集合中的起点数,比如1-2,3-2这种情况,起点数就是2...不过这样写的话其实就是强联通图了hhh还是自己太菜...图论学的不是很好
【代码】
#include <stdio.h> #include <string.h> #include <algorithm> using namespace std; int father[100100]; int vis[100100]; int n,m; int find(int x) { int xx=x; while (father[xx]!=xx) xx=father[xx]; while (father[x]!=x) { int t=x; x=father[x]; father[t]=xx; } return xx; } int main() { int pp;scanf("%d",&pp); while (pp--) { scanf("%d%d",&n,&m); memset(vis,0,sizeof(vis)); for (int i=0;i<=n;i++) father[i]=i; for (int i=0;i<m;i++) { int x,y;scanf("%d%d",&x,&y); vis[y]=1; int fx=find(x); int fy=find(y); if (fx!=fy) father[fy]=fx; } int ans=0; int aans=0; for (int i=1;i<=n;i++) { if (father[i]==i) ans++; if (!vis[i]) aans++; } printf("%d\n",max(aans,ans)); } }
相关文章推荐
- 《海上钢琴师》The Legend Of 1900
- HOST: 239.255.255.250:1900
- 1900Android买什么国产 我不是有钱人
- poj 1900 Game
- 239.255.255.250:1900
- 【zzuliOJ】1900 - 985的“树”难题(bfs & 并查集)
- 【vijos1900】 学姐吃寿司
- ZCMU—1623
- ZCMU—1126
- ZCMU—1779
- ZCMU—1587
- ZCMU—1230
- ZCMU-1163-分数划分
- ZCMU—1200
- ZCMU-1618-骨牌覆盖(矩阵乘法+快速幂)
- ZCMU-1127-(stringstream的应用)
- ZCMU-1409-因子和
- ZCMU-1770-打字
- ZCMU-训练赛-Problem A: Freckles
- ZCMU-1769-约数问题