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. javascript中退出语句break,continue和return 比较

    在 break,continue和return 三个关键字中, break,continue是一起的,return 是函数返回语句,但是返回的同时也将函数停止. 首先:break和continue两个 ...

  2. 使用Indy解决Could not load SSL Library错误

    测试平台:DelphiXE7 + Indy 10.6.0.5169 + Win7 64bit 步骤: 1. SSL下载版本:openssl-1.0.1j-i386-win32 可去http://yun ...

  3. PHPmyadmin Getshell(10.25 第二十七天)

    PHPmyadmin Getshell的方法(1)show global variables like '% secure-file-priv%' 如果该参数设置为空或者指定的文件夹可以利用,然后写木 ...

  4. GNS3 模拟icmp端口不可达

    R1 : conf t int f0/0 no shutdown ip add 192.168.1.1 255.255.255.0 no ip routing end R2 f0/0: conf t ...

  5. 二十六、SAP中通过FORMAT COLOR来设置文字背景颜色

    一.代码如下 二.效果如下

  6. 二、【重点】环境安装:通过淘宝 cnpm 快速安装使用 React,生成项目,运行项目、安装项目

    1.cnpm代替npm 如果你的系统还不支持 Node.js 及 NPM 可以参考我们的 Node.js 教程. 我们建议在 React 中使用 CommonJS 模块系统,比如 browserify ...

  7. file:///D:/Program%20Files/Microsoft%20Visual%20Studio%2011.0/VC/VCWizards/CodeWiz/MFC/Variable/HTML

    title VS2005  VS2008添加变量,添加函数,添加类时弹出 Script Error  解决办法 问现象描述 : 问题大家都清楚了.不赘述 错误提示 :file:///C:/Progra ...

  8. Frequently arduino function

    unctions                                                     功能if(Serial)                           ...

  9. 剑指offer_12.31_Day_1

    不用加减乘除做加法 题目描述 写一个函数,求两个整数之和,要求在函数体内不得使用+.-.*./四则运算符号.   不用四则运算,必然是依靠位运算. 位运算包括,与,或,异或,取反,左移,右移. 分别为 ...

  10. 虚拟机vmware vmnet8 未识别(转)

    原文链接:https://blog.csdn.net/windows_q/article/details/50678646