HDU1829 - A Bug's Life 分组并查集
来源:互联网 发布:淘宝网店详情怎么上传 编辑:程序博客网 时间:2024/04/29 10:36
HDU1829 - A Bug's Life : http://acm.hdu.edu.cn/showproblem.php?pid=1829
题意:首先输入T,接下来是T组测试数据。每组测试数据第一行输入N,M代表共有N个人,接下来有M行输入.每行有两个数a和b,代表编号为a和b的两个人是异性。输入结束后判断是否存在错误的信息。如N = 3,M = 3,接下来是3组输入。1 2,2 3,1 3。此组数据是错的,因为1 和 3都和2是异性,说明1 和 3是同性,但第三组数据又说1和3是异性。所以错了,输出“Suspicious bugs found!”,若没错则输出“No suspicious bugs found!”.这里输出的是主要信息,其他格式不赘述。
代码:
#include <iostream>#include <cstdio>#include <cstring>using namespace std;const int MAXN = 2010;int Father[MAXN],flag;int T,N,M;int x,y;int Case = 0;void Initial(){for(int i = 0;i < MAXN*2 + 5;i++)Father[i] = i;flag = 0;}int Find(int x){if(x == Father[x])return x;return Father[x] = Find(Father[x]); //return x == Father[x] ? x : Find(Father[x]);}void Union(int x,int y){x = Find(x);y = Find(y);if(x != y)Father[x] = y;}bool Same(int x,int y){return Find(x) == Find(y);}int main(){scanf("%d",&T);while(T--){Initial();scanf("%d%d",&N,&M);for(int i = 0;i < M;i++){scanf("%d%d",&x,&y);if(flag)continue;if(Same(x,y))flag = 1;//判断x,y是否是同性,若输入没错,则输入的是一对异性 // 1 - N 表示的是男性,N + 1 表示的是女性 Union(x,y+N);//若x是男性,则y一定是女性 Union(x+N,y);//若x是女性,则y一定是男性 }printf("Scenario #%d:\n",++Case);if(flag)printf("Suspicious bugs found!\n\n");else printf("No suspicious bugs found!\n\n");}return 0;}PS:此题是POJ上题名为"食物链"的简化版本,有兴趣的可以去做做这题,思路是一样的.
0 0
- HDU1829 - A Bug's Life 分组并查集
- hdu1829 A Bug's Life --分组并查集
- hdu1829 A Bug's Life (分组并查集)
- HDU1829 A Bug's Life 并查集
- HDU1829(A Bug's Life )—并查集
- A Bug's Life hdu1829 并查集
- A Bug's Life(hdu1829种类并查集)
- hdu1829 A Bug's Life (并查集)
- hdu1829 A Bug's Life 并查集
- hdu1829 A Bug's Life(并查集)
- HDU1829:A Bug's Life(并查集)
- HDU1829:A Bug's Life(种类并查集)
- HDU1829 A Bug's Life(种类并查集)
- 【种类并查集】 hdu1829 A bug's Life
- hdu1829——A Bug's Life 种类、带权并查集 裸题
- HDU1829&POJ 2492 a bug's life [并查集||二分图染色]
- hdu1829题解(A BUG'S LIFE)简单并查集
- A Bug's Life (分组 并查集)
- uvalive 4975
- 【解决】SSH项目问题(1)--Could not initialize class net.sf.json.util.JSONUtils
- 操作python对象的属性
- Linux常用命令之man/mv/shutdown/history
- android自动化工具之--Monkey工具简单了解
- HDU1829 - A Bug's Life 分组并查集
- 关于输出用%lf和%f的问题
- Android向sd卡写入文件的大概流程
- 富文本(图文混排)—— TextKit & CoreText
- hdu1874-畅通工程续
- WORD BYTE 相互转换
- oracle ORA-12560: TNS: 协议适配器错误的问题
- URAL 1225Flags dp练习
- 自己实现的一个不会奇奇怪怪换行的TextView.