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 并查集的一个应用,就是检测是否存在矛盾,就是两个不该相交的集合有了交集.本题就是这样,一种虫子有 ...
随机推荐
- 007、MySQL日期取当前时间,取昨天
#取今天文本格式 SELECT DATE_SUB( curdate( ), INTERVAL DAY ); #取昨天文本格式 SELECT DATE_SUB( curdate( ), INTERVAL ...
- java 根据值获取枚举对象
关键方法: /** * 值映射为枚举 * * @param enumClass 枚举类 * @param value 枚举值 * @param method 取值方法 * @param <E&g ...
- node核心 http模块
node作为服务器更多的是web服务器 1.http模块 首先:http是一个协议.里面有通信机制,状态码一大堆乱七八糟的东西.自己写猴年马月都写不出来,这个对象帮我们集成.直接用 服务器对象: ht ...
- Linux学习总结《shell脚本》知识点关键-用好“过滤器”
- UVA - 1513 Movie collection (树状数组)
题意:有n个影碟,标号为1~n,位置为0~n-1,每次取出一个影碟看完后,将其放在最前面(标号为0处),问每个影碟取出前,其位置之前有多少个影碟. 分析: 1.数组大小开为100000*2,后1000 ...
- C++ Winsock
由于兼容的问题更新下winsock,有较好的移植性:客户端是非阻塞的,服务器是阻塞的! Win32控制台: 数据收发: 服务器向客户端发送一个txt文本内容和一个结构体数据: 服务器代码: #incl ...
- log4j1-x使用
目录 代码 配置 编译 测试 代码: import org.apache.log4j.*; public class Client{ public static void main(String[] ...
- Flink on yarn以及实现jobManager 高可用(HA)
on yarn https://ci.apache.org/projects/flink/flink-docs-release-1.8/ops/deployment/yarn_setup.html f ...
- (22)Canny算法
基础知识,主要是看这个博客:https://blog.csdn.net/qq_41167777/article/details/84863351
- Python 异常处理(Try...Except)
版权所有,未经许可,禁止转载 章节 Python 介绍 Python 开发环境搭建 Python 语法 Python 变量 Python 数值类型 Python 类型转换 Python 字符串(Str ...