题目链接: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. [Bzoj4943][Noi2017]蚯蚓(hash)

    4943: [Noi2017]蚯蚓 Time Limit: 50 Sec  Memory Limit: 512 MBSubmit: 237  Solved: 110[Submit][Status][D ...

  2. C# Ftp Client 基本操作

    C# Ftp Client 上传.下载与删除 简单介绍一下Ftp Client 上传.下载与删除,这是目前比较常用的命令,各个方法其实都差不多,重点是了解Ftp命令协议. 1.建立连接 public ...

  3. MySql----on duplicate key

    mysql on duplicate key 语句是解决key 冲突的问题,同时 ,key 包括 primary key 和 unique key 等

  4. Parameter Binding in ASP.NET Web API #Reprinted

    http://www.asp.net/web-api/overview/formats-and-model-binding/parameter-binding-in-aspnet-web-api

  5. 常见编码bug

    1.result.replace("abc","bcd");错误 改成result= result.relace("abc","b ...

  6. ios 使用keychain具体方法

    Dictionary  写入: if ([self.currentUserAccount length] > 0) {                                Keycha ...

  7. Android ANR原理分析

    一.概述 ANR(Application Not responding),是指应用程序未响应,Android系统对于一些事件需要在一定的时间范围内完成,如果超过预定时间能未能得到有效响应或者响应时间过 ...

  8. UITableViewCell -- 动画

    -(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath: ...

  9. Spring源代码由浅入深系列三 refresh

    Spring中的refresh是一个相当重要的方法. 它完毕IOC的第一个阶段,将xml中的bean转化为beanDefinition.具体说明如上图所看到的. 在上图中,创建obtainFreshB ...

  10. C 标准库 - <math.h>

    C 标准库 - <math.h> 简介 math.h 头文件定义了各种数学函数和一个宏.在这个库中所有可用的功能都带有一个 double 类型的参数,且都返回 double类型的结果. 库 ...