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 并查集的一个应用,就是检测是否存在矛盾,就是两个不该相交的集合有了交集.本题就是这样,一种虫子有 ...
随机推荐
- python爬虫笔记01
1.urllib库中request,parse的学习 1.1 简单的请求页面获取,并下载到本地 request的使用 from urllib import request # 获取此网页的demout ...
- oracle和mysql的一些区别
1.分页查询语句的区别 2.字符串拼接的区别,oracle不支持三个字符串的拼接,mysql支持三个字符串的拼接,在mybatis中,连接的是oracle数据库,字符串的拼接需要如下语句: <s ...
- 重采样Resample 的一些研究记录。
最近项目有需要重采样算法,先找了一下,主流的就是几个开源算法,Speex / Opus / ffmpeg / sox 1.最早的事Speex,算法源自CCRMA(Center for Computer ...
- MongoDB 常用查询语法
一.查询 find方法 db.collection_name.find(); 查询所有的结果: select * from users; db.users.find(); 指定返回那些列(键): se ...
- 045-利用反射机制,简单的实现PHP插件模式
<?php //利用反射机制,简单的实现PHP插件模式 # 假设,我们有一款开源产品,所有开发者都必须在我定制的需求之上,进行二次开发, # 而开发完成后的新模块,就是一个不一样的新插件,可以放 ...
- 吴裕雄--天生自然C++语言学习笔记:C++ 运算符
运算符是一种告诉编译器执行特定的数学或逻辑操作的符号.C++ 内置了丰富的运算符,并提供了以下类型的运算符: 算术运算符 关系运算符 逻辑运算符 位运算符 赋值运算符 杂项运算符 算术运算符 下表显示 ...
- 吴裕雄 Bootstrap 前端框架开发——Bootstrap 字体图标(Glyphicons):glyphicon glyphicon-tags
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta name ...
- SHELL学习笔记三
SHELL学习笔记一 SHELL学习笔记二 SHELL学习笔记三 for 命令 读取列表中的复杂值 从变量读取列表 从命令读取值 更改字段分隔符 用通配符读取目录 which 使用多个测试命令 unt ...
- UVA - 10570 Meeting with Aliens(外星人聚会)(暴力枚举)
题意:输入1~n的一个排列(3<=n<=500),每次可以交换两个整数.用最少的交换次数把排列变成1~n的一个环状序列. 分析:正序反序皆可.枚举每一个起点,求最少交换次数,取最小值. 求 ...
- Promise 练习
import { ModelClass } from "engine/injector/Injector"; import { Inject } from "core/i ...