题意:有编号为1~n的虫子,开始假设这种昆虫是异性恋。然后已知xi 和 yi进行交配,根据已知情况分析能否推理出其中是否有同性恋

这道题和 POJ 1182 食物链 十分相似,不过在更新与父节点关系的时候要简单一些

sex数组保存的是与父节点的性别关系,如果与父节点是同性,则为0,否则是1

每次路径压缩的同时要更新sex[a] = (sex[a] + sex[temp]) % 2;

还有就是如果x 和 y 不在一个集合,两棵树进行合并的时候,考虑x px y py 四者之间的关系,有

parent[px] = py;
sex[px] = - (sex[x] ^ sex[y]);

这个在纸上模拟一下就很容易得出来

在敲代码的过程中还是犯了两个低级的小错误,因为一旦我们发现了存在同性恋,便可以输出结果了,不过要对剩余未读入的数据“忽略”

因为i可能是break出来的,这次循环没有完成,i也没有自增,所以在“忽略”的循环里面,i首先要自增一下

 //#define LOCAL
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std; const int maxn = + ;
int parent[maxn], sex[maxn]; int GetParent(int a)
{
if(parent[a] == a) return a;
int temp = parent[a];
parent[a] = GetParent(parent[a]);
sex[a] = (sex[a] + sex[temp]) % ;
return parent[a];
} int main(void)
{
#ifdef LOCAL
freopen("2492in.txt", "r", stdin);
#endif int T, kase;
scanf("%d", &T);
for(kase = ; kase <= T; ++kase)
{
int n, m, i;
int x, y;
bool flag = false;
scanf("%d%d", &n, &m);
for(int i = ; i <= n; ++i)
{
parent[i] = i;
sex[i] = ;
}
for(i = ; i < m; ++i)
{
scanf("%d%d", &x, &y);
int px = GetParent(x);
int py = GetParent(y);
if(px == py)
{
if(sex[x] == sex[y])
{
flag = true;
break;
}
}
else
{
parent[px] = py;
sex[px] = - (sex[x] ^ sex[y]);
}
}
for(++i; i < m; ++i)
scanf("%d%d", &x, &y);
printf("Scenario #%d:\n%suspicious bugs found!\n\n", kase, flag ? "S" : "No s");
//if(kase < T) printf("\n");
}
return ;
}

代码君

POJ 2492 (简单并查集) A Bug's Life的更多相关文章

  1. POJ 2524 (简单并查集) Ubiquitous Religions

    题意:有编号为1到n的学生,然后有m组调查,每组调查中有a和b,表示该两个学生有同样的宗教信仰,问最多有多少种不同的宗教信仰 简单并查集 //#define LOCAL #include <io ...

  2. poj 2492(关系并查集) 同性恋

    题目;http://poj.org/problem?id=2492 卧槽很前卫的题意啊,感觉节操都碎了, t组测试数据,然后n,m,n条虫子,然后m行,每行两个数代表a和b有性行为(默认既然能这样就代 ...

  3. Poj(2236),简单并查集

    题目链接:http://poj.org/problem?id=2236 思路很简单,傻逼的我输出写成了FALL,然后遍历的时候for循环写错了,还好很快我就Debug出来了. #include < ...

  4. POJ 2236 (简单并查集) Wireless Network

    题意: 有n个电脑坏掉了,分别给出他们的坐标 有两种操作,可以O x表示修好第x台电脑,可以 S x y表示x y是否连通 两台电脑的距离不超过d便可连通,两台电脑是连通的可以直接连通也可以间接通过第 ...

  5. POJ - 2492 种类并查集

    思路:保存每个点与其父节点的关系,注意合并和路径压缩即可. AC代码 #include <cstdio> #include <cmath> #include <cctyp ...

  6. poj 1611 简单并查集的应用

    #include<stdio.h> #define N 31000 int pre[N]; int find(int x) { if(x!=pre[x])     pre[x]=find( ...

  7. poj1611 简单并查集

    The Suspects Time Limit: 1000MS   Memory Limit: 20000K Total Submissions: 32781   Accepted: 15902 De ...

  8. 1213 How Many Tables(简单并查集)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1213 简单并查集,统计单独成树的数量. 代码: #include <stdio.h> #i ...

  9. 【简单并查集】Farm Irrigation

    Farm Irrigation Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 65536/32768K (Java/Other) Tot ...

随机推荐

  1. Unity动态加载和内存管理(三合一)

    原址:http://game.ceeger.com/forum/read.php?tid=4394#info 最近一直在和这些内容纠缠,把心得和大家共享一下: Unity里有两种动态加载机制:一是Re ...

  2. UnityException: Texture is not readable

    原地址:http://blog.csdn.net/emoonight/article/details/18002913 fore you can save or load a Texture, you ...

  3. Long和Date数据类型之间相互转换代码

    static final SimpleDateFormat DATETIME_SEC_STR = new SimpleDateFormat("yyyyMMddHHmmss"); 1 ...

  4. cf div2 237 D

    D. Minesweeper 1D time limit per test 2 seconds memory limit per test 512 megabytes input standard i ...

  5. VS2010 Notes

    1.fatal error LNK1123: 转换到 COFF 期间失败: 文件无效或损坏 VS2010在经历一些更新后,建立Win32 Console Project时会出“error LNK112 ...

  6. POJ 3150 Cellular Automaton(矩阵乘法+二分)

    题目链接 题意 : 给出n个数形成环形,一次转化就是将每一个数前后的d个数字的和对m取余,然后作为这个数,问进行k次转化后,数组变成什么. 思路 :下述来自here 首先来看一下Sample里的第一组 ...

  7. Xamarin for Visual Studio 3.11.666 稳定版 破解补丁 Version 3

    前提概要 1.全新安装请参考 安装 Xamarin for Visual Studio. 2.本次补丁包含: ① Xamarin for Visual Studio 3.11.666 ② Xamari ...

  8. asp.net跳转页面的三种方法比较

    目前,对于学习asp.net的很多朋友来讲,实现跳转页面的方法还不是很了解.本文将为朋友们介绍利用asp.net跳转页面的三种方法,并对其之间的形式进行比较,希望能够对朋友们有所帮助. ASP.NET ...

  9. BZOJ 4199 品酒大会

    以前一直听说什么后缀数组height合并之类的 表示我这种后缀数组都敲不熟的蒟蒻怎么会写 但是做了做觉得还是很简单的嘛 这个题是有两问的,第一问是求LCP>=R的后缀对有多少个 这个就是AHOI ...

  10. C#的控制台程序输出

    1. int nChar; string mystring; Console.WriteLine("{0} {1}",nChar,mystring); 其中{0},{1}为占位符 ...