题目链接:http://poj.org/problem?id=2492

A Bug's Life
Time Limit: 10000MS   Memory Limit: 65536K
Total Submissions: 39415   Accepted: 12835

Description

Background 
Professor Hopper is researching the sexual behavior of a rare species of bugs. He assumes that they feature two different genders and that they only interact with bugs of the opposite gender. In his experiment, individual bugs and their interactions were easy to identify, because numbers were printed on their backs. 
Problem 
Given a list of bug interactions, decide whether the experiment supports his assumption of two genders with no homosexual bugs or if it contains some bug interactions that falsify it.

Input

The first line of the input contains the number of scenarios. Each scenario starts with one line giving the number of bugs (at least one, and up to 2000) and the number of interactions (up to 1000000) separated by a single space. In the following lines, each interaction is given in the form of two distinct bug numbers separated by a single space. Bugs are numbered consecutively starting from one.

Output

The output for every scenario is a line containing "Scenario #i:", where i is the number of the scenario starting at 1, followed by one line saying either "No suspicious bugs found!" if the experiment is consistent with his assumption about the bugs' sexual behavior, or "Suspicious bugs found!" if Professor Hopper's assumption is definitely wrong.

Sample Input

2
3 3
1 2
2 3
1 3
4 2
1 2
3 4

Sample Output

Scenario #1:
Suspicious bugs found! Scenario #2:
No suspicious bugs found!

Hint

Huge input,scanf is recommended.

Source

TUD Programming Contest 2005, Darmstadt, Germany
 
 
 
 
题解:
带权并查集。r[]用于表示当前结点与父节点是同性还是异性。
 
 
带权并查集:
 
  带权:r[]数组可以记录当前结点与父节点的关系,可以是大小关系, 可以是逻辑关系(如此题)。对于相同的集合,由于在这棵树中,每个结点与父节点的关系已经确定,那么每个节点与集合中的其他结点的关系也可以一路推导出来。对于两个不同的集合,如果知道一对位于不同集合的结点的关系,那么这两个集合所有的结点之间的关系也可以推导出来了,即两个集合可以合并为一个集合。  
 
  路径压缩:对于被find()函数访问过的结点x, 它们的fa[x]都会直接指向根节点,同时需要更新r[x]数组(一路叠加)。问:那么对于被访问过的结点x的子树怎么办呢,不会被落下吗?答:结点x的子树的fa[]指针没有改变,仍然是指着x,即x的子树一直跟着x。
 
  合并:对于两个不同的集合,由于在对u、v调用find()函数时,u和v都分别指向了各自的根节点(路径压缩)。设fu为u所在集合的根节点(也是u的父节点), fv也如此,所以u和fu的关系即为r[u]、v和fv的关系即为r[v],且又知道u和v的关系, 那么就可以直接推出fu和fv的关系,这样就可以实现两个集合的合并。
 
 
 
代码如下:
 #include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <vector>
#include <queue>
#include <stack>
#include <map>
#include <string>
#include <set>
#define ms(a,b) memset((a),(b),sizeof((a)))
using namespace std;
typedef long long LL;
const double EPS = 1e-;
const int INF = 2e9;
const LL LNF = 2e18;
const int MAXN = 2e3+; int n, m;
int fa[MAXN], r[MAXN]; int find(int x)
{
if(fa[x]==-) return x;
int pre = find(fa[x]);
r[x] = (r[x]+r[fa[x]])%;
return fa[x] = pre;
} bool Union(int u, int v)
{
int fu = find(u);
int fv = find(v);
if(fu==fv)
return r[u]==r[v]; fa[fu] = fv;
r[fu] = (-r[u]++r[v])%;
return false;
} int main()
{
int T;
scanf("%d", &T);
for(int kase = ; kase<=T; kase++)
{
scanf("%d%d", &n, &m);
memset(fa, -, sizeof(fa));
memset(r, , sizeof(r)); bool flag = true;
for(int i = ; i<=m; i++)
{
int u, v;
scanf("%d%d", &u, &v);
if(Union(u, v))
flag = false;
} printf("Scenario #%d:\n", kase);
printf("%s\n\n", flag?"No suspicious bugs found!":"Suspicious bugs found!");
}
}

代码二:

 #include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <vector>
#include <queue>
#include <stack>
#include <map>
#include <string>
#include <set>
#define ms(a,b) memset((a),(b),sizeof((a)))
using namespace std;
typedef long long LL;
const double EPS = 1e-;
const int INF = 2e9;
const LL LNF = 2e18;
const int MAXN = 2e3+; int n, m;
int fa[MAXN], r[MAXN]; int find(int x)
{
if(fa[x]==-) return x;
int pre = find(fa[x]);
r[x] ^= r[fa[x]];
return fa[x] = pre;
} bool Union(int u, int v)
{
int fu = find(u);
int fv = find(v);
if(fu==fv)
return r[u]==r[v]; fa[fu] = fv;
r[fu] = r[u]^^r[v];
return false;
} int main()
{
int T;
scanf("%d", &T);
for(int kase = ; kase<=T; kase++)
{
scanf("%d%d", &n, &m);
memset(fa, -, sizeof(fa));
memset(r, , sizeof(r)); bool flag = true;
for(int i = ; i<=m; i++)
{
int u, v;
scanf("%d%d", &u, &v);
if(Union(u, v))
flag = false;
} printf("Scenario #%d:\n", kase);
printf("%s\n\n", flag?"No suspicious bugs found!":"Suspicious bugs found!");
}
}

POJ2492 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. poj2492 A Bug's Life【并查集】

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

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

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

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

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

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

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

  6. POJ2492:A Bug's Life(种类并查集)

    A Bug's Life Time Limit: 10000MS   Memory Limit: 65536K Total Submissions: 45757   Accepted: 14757 题 ...

  7. 【进阶——种类并查集】hdu 1829 A Bug's Life (基础种类并查集)TUD Programming Contest 2005, Darmstadt, Germany

    先说说种类并查集吧. 种类并查集是并查集的一种.但是,种类并查集中的数据是分若干类的.具体属于哪一类,有多少类,都要视具体情况而定.当然属于哪一类,要再开一个数组来储存.所以,种类并查集一般有两个数组 ...

  8. A Bug's Life(种类并查集)(也是可以用dfs做)

    http://acm.hdu.edu.cn/showproblem.php?pid=1829   A Bug's Life Time Limit:5000MS     Memory Limit:327 ...

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

    传送门: http://acm.hdu.edu.cn/showproblem.php?pid=1829 A Bug's Life Time Limit: 15000/5000 MS (Java/Oth ...

随机推荐

  1. Xamarin.Forms特殊的视图BoxView

    Xamarin.Forms特殊的视图BoxView   BoxView是Xamarin.Forms比较特殊的视图.该视图构建非常简单,其作用也很单一.它的作用就是构成一个特定颜色的色块.在界面设计中, ...

  2. 1005 Spell It Right

    1005 Spell It Right   Given a non-negative integer N, your task is to compute the sum of all the dig ...

  3. Linux有问必答:如何检查Linux的内存使用状况

    -1. /proc/meminfo11% -2. atop20% -3. free29% -4. GNOME System Monitor35% -5. htop41% -6. KDE System ...

  4. Gvim 和 Opencv编译

    好奇看了一下Gvim编译器:在官网上也有介绍,github上有源码,安装在电脑上,感觉需求不大,记那些命令也太多了.然后编译opencv过程中cmake成功了,但是VS下编译报了很多错,准备不搞这些了 ...

  5. Android自己定义实现循环滚轮控件WheelView

    首先呈上效果图 watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/d ...

  6. NYOJ 38 布线问题_(解法1 Kruskal算法)

    时间限制:1000 ms  |  内存限制:65535 KB 难度:4 描写叙述 南阳理工学院要进行用电线路改造.如今校长要求设计师设计出一种布线方式,该布线方式须要满足下面条件: 1.把全部的楼都供 ...

  7. hdu杭电1856 More is better【并查集】

    Problem Description Mr Wang wants some boys to help him with a project. Because the project is rathe ...

  8. OA权限树搭建 代码

    <ul id="tree"> <s:iterator value="#application.topPrivilegeList"> &l ...

  9. python正则方法

    通过正则替换字符串 res=re.sub(正则,newString,srcString)//返回替换后的字符串 res,m=res.subn(正则,newString,srcString)//返回替换 ...

  10. android相关文件夹的存取方式与函数解析---全

    因为排版问题.转为markdown编辑: http://blog.csdn.net/self_study/article/details/58587412