题目链接: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. Codeforces 620E New Year Tree(线段树+位运算)

    题目链接 New Year Tree 考虑到$ck <= 60$,那么用位运算统计颜色种数 对于每个点,重新标号并算出他对应的进和出的时间,然后区间更新+查询. 用线段树来维护. #includ ...

  2. 矩阵乘法加速fib数列

    考虑矩阵(1,1)(1,0) #include<cstdio> #include<cstring> #include<iostream> using namespa ...

  3. CodeForces - 11D A Simple Task

    Discription Given a simple graph, output the number of simple cycles in it. A simple cycle is a cycl ...

  4. Extjs grid 单元格事件

    celldblclick: function (view, td, cellIndex, record, tr, rowIndex, e, eOpts) { //extjs 4.2下,有时出现,多次不 ...

  5. CreateJS结合Falsh工具生成Canvas动画(加密字符串的由来)

    CreateJS是一款制作Canvas动画的工具. 官网如下: http://www.createjs.com/ http://www.createjs.cc/ 使用CreateJS时最大的疑问就是J ...

  6. 【hibernate】Hibernate中get()和load()的区别

    Hibernate中根据Id单条查询获取对象的方式有两种,分别是get()和load(),来看一下这两种方式的区别. 1. get() 使用get()来根据ID进行单条查询: 1 User user= ...

  7. WindowManager.LayoutParams 详解

    WindowManager.LayoutParams 是 WindowManager 接口的嵌套类:继承于 ViewGroup.LayoutParams .它的内容十分丰富.其实WindowManag ...

  8. 【LeetCode】Word Break 解题报告

    Given a string s and a dictionary of words dict, determine if s can be segmented into a space-separa ...

  9. Linux基础(2)- 用户、群组和权限

    一.用户.群组和权限 1)  新建用户natasha,uid为1100,gid为555,备注信息为“master” 2)  修改natasha用户的家目录为/Natasha 3)  查看用户信息配置文 ...

  10. 我的跟我学Ffmpeg 视频受众有哪些人

    经常有人问我如何学习音视频以及如何学习Ffmpeg,问我有没有比较好的书的书推荐.比较好的音视频以及FFmpeg方面的 书,我了解到的比较全面又能深入浅出的还真没有.很多朋友都推荐雷神的博客,雷神的博 ...