hdu 1829 A Bug's Life(分组并查集(偏移量))
A Bug's Life
Time Limit: 15000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 9204 Accepted Submission(s): 2961
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.
题意:
1喜欢2,2喜欢3,而1又喜欢3,则矛盾。 (排出同性恋)
基础并查集的新应用:
分组并查集(偏移量):开一个并查集,但是要加个偏移向量数组,来记录每个节点距离根节点的距离
代码:
/*带权值的并查集*/
#include<cstdio>
#define maxn 2005
int father[maxn],rank[maxn];
int tt,nn,mm;
void init()
{
for(int i=;i<nn;i++){
father[i]=i;
rank[i]=;
}
} int fin(int x)
{
if(x==father[x]) return x;
int temp=fin(father[x]);
rank[x]=(rank[x]+rank[father[x]])%;
father[x]=temp;
return father[x];
}
int unin(int a,int b)
{
int x=fin(a);
int y=fin(b);
if(x==y)
{
if(rank[a]==rank[b])return ; //说明矛盾
return ;
}
father[x]=y;
rank[x]=(rank[a]+rank[b]+)%;
return ;
}
int main()
{
int a,b;
int tag;
scanf("%d",&tt);
for(int j=;j<=tt;j++)
{
tag=;
scanf("%d%d",&nn,&mm);
init();
for(int i=;i<mm;i++)
{
scanf("%d%d",&a,&b);
tag+=unin(a,b);
}
printf("Scenario #%d:\n",j); if(tag)puts("Suspicious bugs found!");
else puts("No suspicious bugs found!");
puts("");
}
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 并查集的一个应用,就是检测是否存在矛盾,就是两个不该相交的集合有了交集.本题就是这样,一种虫子有 ...
- HDU 1829 分组并查集
题意:有两种性别,每组数据表示是男女朋友,判断输入的几组数据是否有同性恋 思路:http://blog.csdn.net/iaccepted/article/details/24304087 分组并查 ...
- 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/Oth ...
- hdu - 1829 A Bug's Life (并查集)&&poj - 2492 A Bug's Life && poj 1703 Find them, Catch them
http://acm.hdu.edu.cn/showproblem.php?pid=1829 http://poj.org/problem?id=2492 臭虫有两种性别,并且只有异性相吸,给定n条臭 ...
- 【进阶——种类并查集】hdu 1829 A Bug's Life (基础种类并查集)TUD Programming Contest 2005, Darmstadt, Germany
先说说种类并查集吧. 种类并查集是并查集的一种.但是,种类并查集中的数据是分若干类的.具体属于哪一类,有多少类,都要视具体情况而定.当然属于哪一类,要再开一个数组来储存.所以,种类并查集一般有两个数组 ...
- hdu 1829 A Bug's Life(并查集)
A Bu ...
- HDU 1829 - A Bug's Life
Problem Description Background Professor Hopper is researching the sexual behavior of a rare species ...
- HDU 1829 A Bug's Life 【带权并查集/补集法/向量法】
Background Professor Hopper is researching the sexual behavior of a rare species of bugs. He assumes ...
随机推荐
- Cheatsheet: 2015 03.01 ~ 03.31
Web The Architecture of Algolia's Distributed Search Network No promises: asynchronous JavaScript wi ...
- [SAP ABAP开发技术总结]动态修改选择屏幕
声明:原创作品,转载时请注明文章来自SAP师太技术博客( 博/客/园www.cnblogs.com):www.cnblogs.com/jiangzhengjun,并以超链接形式标明文章原始出处,否则将 ...
- linux配置IP地址
1. ifconfig命令临时配置IP地址 ifconfig命令:查看与配置网络状态命令 如: ifconfig eht0 192.168.0.200 netmask 255.255.255.0 # ...
- hdu 4946 Just a Joke(数学+物理)
链接:http://acm.hdu.edu.cn/showproblem.php?pid=4969 Just a Joke Time Limit: 2000/1000 MS (Java/Others) ...
- NoSQL聚合数据模型
NoSQL聚合数据模型 特点 聚合数据模型的特点就是把经常访问的数据放在一起(聚合在一块): 这样带来的好处很明显,对于某个查询请求,能够在与数据库一次交互中将所有数据都取出来: 当然,以这种方式存储 ...
- javascript权威指南笔记--javascript语言核心(三)
1.var用来声明一个或多个变量.全局变量是全局对象的属性,它无法通过delete删除. 如果var语句中的变量没有指定初始化表达式,那么这个变量的初始值为undefined. 变量声明语句会被提前到 ...
- CSS笔记(十)position属性与定位
参考:http://www.w3school.com.cn/cssref/pr_class_position.asp position属性规定了元素的定位类型.任何元素都可定位,其中,绝对或固定元素会 ...
- Scrum Meeting---Seven(2015-11-2)
今日已完成任务和明日要做的任务 姓名 今日已完成任务 今日时间 明日计划完成任务 估计用时 董元财 完成了服务器实现 5h 服务器与客户端连接测试 4h 胡亚坤 客户端与服务器端的通信 2h 客户端与 ...
- javascript中的计时器
javascript中的定时器有两种:一种是一次性定时器,一种是可以持续使用的定时器: 1:一次性定时器setTimeout(a,b):兼容ie的任何版本 该方法接受两个参数,第一个是要执行的代码,第 ...
- mysql概要(二)类型
1.mysql数值型范围 tinyint可选属性 tinyint(N) unsigned zerofill N:表示显示长度,与zerofill配合使用,即长度不够用0填充,并且自动变成无符号的数,仅 ...