POJ-2492 A Bug's Life(种类并查集)
http://poj.org/problem?id=2492
题意:
给出一个T代表几组数据,给出一个n一个m,代表人的编号由1~n,m条命令,每条命令由两个数值组成,代表这两个人性别不同,问所有命令是否符合逻辑
两种写法:
第一种:带权并查集
#include <stdio.h>
#include <string.h>
#include <iostream>
#include <string>
#include <math.h>
#include <algorithm>
#include <vector>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <sstream>
const int INF=0x3f3f3f3f;
typedef long long LL;
const int mod=1e9+;
const int maxn=1e5+;
using namespace std; int fa[];
int dis[];//表示i和其根结点的关系,1表示异性,0表示同性 void init(int n)
{
for(int i=;i<=n;i++)
dis[i]=,fa[i]=i;
}
int Find(int x)//带权并查集
{
if(x!=fa[x])
{
int t=fa[x];
fa[x]=Find(fa[x]);
dis[x]=(dis[x]+dis[t])%;
}
return fa[x];
} int main()
{
#ifdef DEBUG
freopen("sample.txt","r",stdin);
#endif
// ios_base::sync_with_stdio(false);
// cin.tie(NULL); int T;
scanf("%d",&T);
for(int k=;k<=T;k++)
{
int n,m;
scanf("%d %d",&n,&m);
init(n);
int flag=;//flag=1表示找到了错误
for(int i=;i<=m;i++)
{
int a,b;
scanf("%d %d",&a,&b);
if(flag) continue;
int aa=Find(a);
int bb=Find(b);
if(aa==bb)// 二者的祖先相同
{
if(dis[a]==dis[b]) flag=;//且与祖先的性别关系相同,出现错误
}
else//二者祖先不同
{
fa[bb]=fa[aa];
dis[bb]=(dis[a]+-dis[b])%;
}
}
if(k!=) printf("\n");//格式要求
printf("Scenario #%d:\n",k);
if(flag==) printf("Suspicious bugs found!\n");
else printf("No suspicious bugs found!\n");
} return ;
}
第二种:
fa开两倍空间,fa[i+n]相当于i的对立集合,这种可能耗时会多一点吧
#include <stdio.h>
#include <string.h>
#include <iostream>
#include <string>
#include <math.h>
#include <algorithm>
#include <vector>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <sstream>
const int INF=0x3f3f3f3f;
typedef long long LL;
const int mod=1e9+;
const int maxn=1e5+;
using namespace std; int fa[];
void init(int n)
{
for(int i=;i<=n;i++)
fa[i]=i;
}
int Find(int x)
{
return x==fa[x]? x:fa[x]=Find(fa[x]);
}
void Union(int a,int b)
{
int aa=Find(a);
int bb=Find(b);
if(aa!=bb) fa[aa]=bb;
} int main()
{
#ifdef DEBUG
freopen("sample.txt","r",stdin);
#endif
// ios_base::sync_with_stdio(false);
// cin.tie(NULL); int T;
scanf("%d",&T);
for(int k=;k<=T;k++)
{
int n,m;
scanf("%d %d",&n,&m);
init(*n);
int flag=;//flag=1表示找到了错误
for(int i=;i<=m;i++)
{
int a,b;
scanf("%d %d",&a,&b);
if(flag) continue;
int aa=Find(a);
int bb=Find(b);
if(aa==bb) flag=;// 二者同属一个集合,性别相同
else//二者性别不同,不属于同一个集合
{
Union(a,b+n);//a和b的对立集合同属一个集合
Union(a+n,b);//b和a的对立集合同属一个集合
}
}
if(k!=) printf("\n");//格式要求
printf("Scenario #%d:\n",k);
if(flag==) printf("Suspicious bugs found!\n");
else printf("No suspicious bugs found!\n");
} return ;
}
-
POJ-2492 A Bug's Life(种类并查集)的更多相关文章
- 【POJ】2492 A bug's life ——种类并查集
A Bug's Life Time Limit: 10000MS Memory Limit: 65536K Total Submissions: 28211 Accepted: 9177 De ...
- POJ 2492 A Bug's Life (并查集)
Background Professor Hopper is researching the sexual behavior of a rare species of bugs. He assumes ...
- POJ 2492 A Bug's Life【并查集高级应用+类似食物链】
Background Professor Hopper is researching the sexual behavior of a rare species of bugs. He assumes ...
- POJ 2492 A Bug's Life(并查集)
http://poj.org/problem?id=2492 题意 :就是给你n条虫子,m对关系,每一对关系的双方都是异性的,让你找出有没有是同性恋的. 思路 :这个题跟POJ1703其实差不多,也是 ...
- POJ2492 A Bug's Life —— 种类并查集
题目链接:http://poj.org/problem?id=2492 A Bug's Life Time Limit: 10000MS Memory Limit: 65536K Total Su ...
- POJ 1703 Find them,Catch them ----种类并查集(经典)
http://blog.csdn.net/freezhanacmore/article/details/8774033?reload 这篇讲解非常好,我也是受这篇文章的启发才做出来的. 代码: #i ...
- HDU 1829 A Bug's Life(种类并查集)
思路:见代码吧. #include <stdio.h> #include <string.h> #include <set> #include <vector ...
- hdu1829A Bug's Life(种类并查集)
传送门 关键在于到根节点的距离,如果两个点到根节点的距离相等,那么他们性别肯定就一样(因为前面如果没有特殊情况,两个点就是一男一女的).一旦遇到性别一样的,就说明找到了可疑的 #include< ...
- POJ 1703 Find them, Catch them(种类并查集)
题目链接 这种类型的题目以前见过,今天第一次写,具体过程,还要慢慢理解. #include <cstring> #include <cstdio> #include <s ...
- hdoj 1829 A bug's life 种类并查集
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1829 并查集的一个应用,就是检测是否存在矛盾,就是两个不该相交的集合有了交集.本题就是这样,一种虫子有 ...
随机推荐
- win上java1.7和1.8版本修改环境变量无效.md
网上找了很多办法都没用. 解决办法: 看看自己 "系统环境变量" 中是不是有 "C:\ProgramData\Oracle\Java\javapath" 这项配 ...
- Spring AOP 管理事务
<aop:config proxy-target-class="true"> <aop:pointcut expression="execution(* ...
- Problem C Updating a Dictionary
Problem C Updating a Dictionary In this problem, a dictionary is collection of key-value pairs, ...
- JuJu团队12月1号工作汇报
JuJu团队12月1号工作汇报 JuJu Scrum 团队成员 今日工作 剩余任务 困难 于达 修改generator函数 优化代码 不熟悉julia 婷婷 和金华一起调试main.jl 继 ...
- Java多线程之以7种方式让主线程等待子线程结束
记一次主线程等待子线程结束的多种方法的学习 在学习多线程时,最开始遇到的问题其实是"计算子线程运行时间",写到最后发现本文和标题更为符合,但是仍然基于问题:"在主线程中获 ...
- Golang的标准输入输出
Golang的标准输入输出 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 在很多情况下,我们希望用户通过键盘输入一个数值,存储到某个变量中,然后将该变量的值取出来,进行操作.这时候 ...
- POJ 1458:Common Subsequence
Common Subsequence Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 41957 Accepted: 16 ...
- (转)zookeeper理解
分布式服务框架 Zookeeper -- 管理分布式环境中的数据 Zookeeper 分布式服务框架是 Apache Hadoop 的一个子项目,它主要是用来解决分布式应用中经常遇到的一些数据管理问题 ...
- jQuery省市联动(XML/JSON)
准备: 导包 在src下导入c3p0-config.xml 导入JDBCUtil 创建数据库 新建js文件夹导入jQuery配置文件 NO01:创建city.jsp页面 <%@ page lan ...
- 055-for循环中break的使用
<?php ;;$x++){ //省略表达式2的for循环将是无限循环 echo "$x<br />"; ){ break; //使用if语句控制退出无限循环 } ...