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 ...
随机推荐
- CodeForces 34B Sale
Sale Time Limit:2000MS Memory Limit:262144KB 64bit IO Format:%I64d & %I64u Submit Status ...
- 【转载】简述Linux的启动过程
原文:简述Linux的启动过程 本文将简单介绍一下Linux的启动过程,希望对那些安装Linux的过程中遇到了问题的朋友有些帮助 声明:本人没用过UEFI模式和GPT分区格式,所有关于这两部分的内容都 ...
- An Example of On-Error Trigger in Oracle Forms
I wrote this trigger around 4 years ago to handle errors in an application based on Oracle Forms 6i. ...
- CHECKBOX_CHECKED built-in in Oracle D2k Forms
CHECKBOX_CHECKED built-in in Oracle D2k Forms DescriptionA call to the CHECKBOX_CHECKED function ret ...
- CSS hack的写法
css hack简单的说就是由于不同的浏览器,比如Internet Explorer 6,Internet Explorer 7,Mozilla Firefox等,对CSS的解析认识不一样,因此会导致 ...
- [SAP ABAP开发技术总结]增强Enhancement
声明:原创作品,转载时请注明文章来自SAP师太技术博客( 博/客/园www.cnblogs.com):www.cnblogs.com/jiangzhengjun,并以超链接形式标明文章原始出处,否则将 ...
- STL区间成员函数及区间算法总结
STL区间成员函数及区间算法总结 在这里总结下可替代循环的区间成员函数和区间算法: 相比单元素遍历操作,使用区间成员函数的优势在于: 1)更少的函数调用 2)更少的元素移动 3)更少的内存分配 在区间 ...
- sqlserver前面加N解释
From : http://lzde360.blog.163.com/blog/static/6780720820111026112033917/ 加上 N 代表存入数据库时以 Unicode 格式存 ...
- FlexSlider插件的详细设置参数 http://www.woothemes.com/flexslider/ -----幻灯片插件
$(window).load(function() { $('.flexslider').flexslider({ namespace: 'flex-', //控件的命名空间,会影响样式前缀 anim ...
- Spring事务配置
Spring中事务的配置学习: 1.心法 Spring配置文件中关于事务配置总是由三个组成部分,分别是DataSource.TransactionManager和代理机制这三部分,无论哪种配置方式,一 ...