HDU 1829 A Bug's Life (种类并查集)
传送门:
http://acm.hdu.edu.cn/showproblem.php?pid=1829
A Bug's Life
Time Limit: 15000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 19429 Accepted Submission(s): 6206
Professor Hopper is researching the sexual behavior of a rare species of bugs. He assumes that they feature two different genders and that they only interact with bugs of the opposite gender. In his experiment, individual bugs and their interactions were easy to identify, because numbers were printed on their backs.
Problem
Given a list of bug interactions, decide whether the experiment supports his assumption of two genders with no homosexual bugs or if it contains some bug interactions that falsify it.
3 3
1 2
2 3
1 3
4 2
1 2
3 4
Suspicious bugs found!
Scenario #2:
No suspicious bugs found!
Huge input,scanf is recommended.
#include <iostream>
#include<algorithm>
#include <cstdio>
#include<cstring>
#include<math.h>
#include<memory>
using namespace std;
typedef long long LL;
#define max_v 2005
int pa[max_v];//前驱
int re[max_v];//性别
int flag;
void make_set(int x)
{
pa[x]=x;
re[x]=;
}
int find_set(int x)
{
if(x==pa[x])
return pa[x];
int t=find_set(pa[x]);
re[x]=(re[pa[x]]+re[x])%;
pa[x]=t;
return pa[x];
}
void union_set(int x,int y)
{
int a=find_set(x);
int b=find_set(y);
if(a==b)
{
if(re[x]==re[y])
flag=;//根节点相同的且性别相同的则是同性恋。
return ;
}
pa[a]=b;
re[a]=(re[x]-re[y]+)%;//保证 0 1
}
int main()
{
int t,j=;
scanf("%d",&t);
while(t--)
{
int n,m;
scanf("%d %d",&n,&m);
flag=;
for(int i=;i<=n;i++)
make_set(i);
for(int i=;i<m;i++)
{
int a,b;
scanf("%d %d",&a,&b);
if(flag==)
continue;
union_set(a,b);
}
printf("Scenario #%d:\n",j++);
if(flag==)
printf("Suspicious bugs found!\n");
else
printf("No suspicious bugs found!\n");
printf("\n");
}
return ;
}
HDU 1829 A Bug's Life (种类并查集)的更多相关文章
- HDU 1829 A Bug's Life(种类并查集)
思路:见代码吧. #include <stdio.h> #include <string.h> #include <set> #include <vector ...
- hdoj 1829 A bug's life 种类并查集
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1829 并查集的一个应用,就是检测是否存在矛盾,就是两个不该相交的集合有了交集.本题就是这样,一种虫子有 ...
- 【POJ】2492 A bug's life ——种类并查集
A Bug's Life Time Limit: 10000MS Memory Limit: 65536K Total Submissions: 28211 Accepted: 9177 De ...
- POJ2492 A Bug's Life —— 种类并查集
题目链接:http://poj.org/problem?id=2492 A Bug's Life Time Limit: 10000MS Memory Limit: 65536K Total Su ...
- hdu1829A Bug's Life(种类并查集)
传送门 关键在于到根节点的距离,如果两个点到根节点的距离相等,那么他们性别肯定就一样(因为前面如果没有特殊情况,两个点就是一男一女的).一旦遇到性别一样的,就说明找到了可疑的 #include< ...
- 【进阶——种类并查集】hdu 1829 A Bug's Life (基础种类并查集)TUD Programming Contest 2005, Darmstadt, Germany
先说说种类并查集吧. 种类并查集是并查集的一种.但是,种类并查集中的数据是分若干类的.具体属于哪一类,有多少类,都要视具体情况而定.当然属于哪一类,要再开一个数组来储存.所以,种类并查集一般有两个数组 ...
- hdu 1182 A Bug's Life(简单种类并查集)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1829 题意:就是给你m条关系a与b有性关系,问这些关系中是否有同性恋 这是一道简单的种类并查集,而且也 ...
- A Bug's Life(种类并查集)(也是可以用dfs做)
http://acm.hdu.edu.cn/showproblem.php?pid=1829 A Bug's Life Time Limit:5000MS Memory Limit:327 ...
- POJ2492:A Bug's Life(种类并查集)
A Bug's Life Time Limit: 10000MS Memory Limit: 65536K Total Submissions: 45757 Accepted: 14757 题 ...
随机推荐
- c# 使用 Newtonsoft.Json 序列化json字符串以及,反序列化对象
1. 序列化 对象 /** 使用 Newtonsoft.Json 序列化对象 **/ [WebMethod] public String getPersonInfos() { // 初始化数据 Lis ...
- 《CSS3揭秘》上(边框,投影,渐变,条纹效果,蚂蚁行军)
最近看了<CSS3揭秘>一书,里面真的是干货满满呀,现将常用到的一些技巧归纳总结,便于日后用到查找.不得不感叹学无止境哦~ 1.边框与背景 半透明边框 .demo{ background- ...
- iframe在移动端的缩放
工作中碰到个奇怪的问题,折腾了大半天,终于算是解决了,这里把分析思路和解决办法记录下. 项目是做响应式的公司官网,前期的静态图页面切完后就提交给后台作为模板使用了,我也就基本退出项目. 在后端落地时发 ...
- java三元运算符
由?:符号表示的,具体的含义其实就和if-else结构的含义差不多,这种运算符会将某个条件作两种处理,如果满足条件的话就执行第一个结果,如果不满足的话就执行另外一个结果,例如: Int A,B,C; ...
- baidu地图api , 地理位置转 经纬度接口
更多可以查看 http://lbsyun.baidu.com/index.php?title=webapi/guide/webservice-geocoding 地理编码示例: 以下是关于地理编 ...
- (PowerShell) 重命名文件
Get-ChildItem -Path C:\temp\test -Filter *.txt | Rename-Item -NewName {$_.Basename.Replace("Old ...
- CentOS随笔 - 2.CentOS7安装ftp支持(vsftpd)
前言 转帖请注明出处: http://www.cnblogs.com/Troy-Lv5/ 在前一篇文章中介绍了在虚拟机中安装CentOS7, 接下来就要进行配置了, 第一个就是安装ftp支持. 要不然 ...
- Sql server字段排序,如果字段是字符型的数字
SELECT id , rownum FROM test ))),) ASC 1.如果想要根据字符型的数字排序,不建议转换成数字类型在order by,因为数据存在字符时,可能转换失败. 2.使用RI ...
- c# datarow[] 转换成 datatable, List<T> 转datatable
c# datarow[] 转换成 datatable, List<T> 转datatable DdataRow[]转成Datatable private DataTable ToDat ...
- 获取当前网页的的url
如果测试的url地址是http://www.test.com/testweb/default.aspx, 结果如下: Request.ApplicationPath: / ...