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(种类并查集)的更多相关文章

  1. 【POJ】2492 A bug's life ——种类并查集

    A Bug's Life Time Limit: 10000MS   Memory Limit: 65536K Total Submissions: 28211   Accepted: 9177 De ...

  2. POJ 2492 A Bug's Life (并查集)

    Background Professor Hopper is researching the sexual behavior of a rare species of bugs. He assumes ...

  3. POJ 2492 A Bug's Life【并查集高级应用+类似食物链】

    Background Professor Hopper is researching the sexual behavior of a rare species of bugs. He assumes ...

  4. POJ 2492 A Bug's Life(并查集)

    http://poj.org/problem?id=2492 题意 :就是给你n条虫子,m对关系,每一对关系的双方都是异性的,让你找出有没有是同性恋的. 思路 :这个题跟POJ1703其实差不多,也是 ...

  5. POJ2492 A Bug's Life —— 种类并查集

    题目链接:http://poj.org/problem?id=2492 A Bug's Life Time Limit: 10000MS   Memory Limit: 65536K Total Su ...

  6. POJ 1703 Find them,Catch them ----种类并查集(经典)

    http://blog.csdn.net/freezhanacmore/article/details/8774033?reload  这篇讲解非常好,我也是受这篇文章的启发才做出来的. 代码: #i ...

  7. HDU 1829 A Bug's Life(种类并查集)

    思路:见代码吧. #include <stdio.h> #include <string.h> #include <set> #include <vector ...

  8. hdu1829A Bug's Life(种类并查集)

    传送门 关键在于到根节点的距离,如果两个点到根节点的距离相等,那么他们性别肯定就一样(因为前面如果没有特殊情况,两个点就是一男一女的).一旦遇到性别一样的,就说明找到了可疑的 #include< ...

  9. POJ 1703 Find them, Catch them(种类并查集)

    题目链接 这种类型的题目以前见过,今天第一次写,具体过程,还要慢慢理解. #include <cstring> #include <cstdio> #include <s ...

  10. hdoj 1829 A bug's life 种类并查集

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1829 并查集的一个应用,就是检测是否存在矛盾,就是两个不该相交的集合有了交集.本题就是这样,一种虫子有 ...

随机推荐

  1. 用ftp命令实现主机文件批量更新

    我们的主机环境是windows 2003,平时程序员访问都喜欢用远程桌面.简单快捷直观.不过我比较喜欢在本地用vim和命令行,这样编辑修改不需要受网络影响. 这种情况下,我本地调试的程序,要经常更新到 ...

  2. 数据库建模工具pd的使用

  3. P1002 写出这个数(Basic Level)

    转跳点:

  4. C语言备忘录——取余与取模

    前几天,一个小姐姐问我取余和取模有什么区别,我当时第一反应就是二者是一样的,但是小姐姐咬死说不一样.我去百度了一下还真的不一样.脑壳疼,我当初误导了多少人.所以为了帮助我记忆也为了帮助预防我误人子弟 ...

  5. Python基础笔记:input()输入与数据类型转换

    input就是个万能输入,不过input输入的元素都是以str形式保存的,如果要他作为一个整数的话,就需要进行数据类型转换. input的使用 name=input('please input you ...

  6. linux中实用的小工具lrzsz

    使用xshell或者其他ssh工具连接上服务器后我们需要向服务器中上传或者下载文件 这时候就用到了lrzsz可以代替其他的ftp软件 yun install - y lrzsz 安装 rz 上传 sz ...

  7. 062-PHP函数按值传参,交换数值函数

    <?php function swap($x,$y){ //定义交换数值函数 $temp=$x; $x=$y; $y=$temp; } $m=5; $n=15; echo "交换前:& ...

  8. C#使用throw和throw ex的区别(转)

    使用throw和throw ex的区别 之前,在使用异常捕获语句try...catch...throw语句时,一直没太留意几种用法的区别,前几天调试程序时无意中了解到几种使用方法是有区别的,网上一查, ...

  9. 线段树&树状数组与离散化的妙用

    牛客2019多校联盟Day7 Fine the median 题意:  每次给数组插入区间[Li,Ri] 内的所有数,每操作一次查询中位数. 遇到这题真的算是巧合,然而就是这种冥冥之中的缘分,给了我线 ...

  10. 批量go get 代码

    批量 go get 代码 Go