POJ2492 A Bug's Life —— 种类并查集
题目链接:http://poj.org/problem?id=2492
Time Limit: 10000MS | Memory Limit: 65536K | |
Total Submissions: 39415 | Accepted: 12835 |
Description
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
Output
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
Source
#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 —— 种类并查集的更多相关文章
- 【POJ】2492 A bug's life ——种类并查集
A Bug's Life Time Limit: 10000MS Memory Limit: 65536K Total Submissions: 28211 Accepted: 9177 De ...
- poj2492 A Bug's Life【并查集】
Background Professor Hopper is researching the sexual behavior of a rare species of bugs. He assume ...
- hdoj 1829 A bug's life 种类并查集
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1829 并查集的一个应用,就是检测是否存在矛盾,就是两个不该相交的集合有了交集.本题就是这样,一种虫子有 ...
- HDU 1829 A Bug's Life(种类并查集)
思路:见代码吧. #include <stdio.h> #include <string.h> #include <set> #include <vector ...
- hdu1829A Bug's Life(种类并查集)
传送门 关键在于到根节点的距离,如果两个点到根节点的距离相等,那么他们性别肯定就一样(因为前面如果没有特殊情况,两个点就是一男一女的).一旦遇到性别一样的,就说明找到了可疑的 #include< ...
- POJ2492:A Bug's Life(种类并查集)
A Bug's Life Time Limit: 10000MS Memory Limit: 65536K Total Submissions: 45757 Accepted: 14757 题 ...
- 【进阶——种类并查集】hdu 1829 A Bug's Life (基础种类并查集)TUD Programming Contest 2005, Darmstadt, Germany
先说说种类并查集吧. 种类并查集是并查集的一种.但是,种类并查集中的数据是分若干类的.具体属于哪一类,有多少类,都要视具体情况而定.当然属于哪一类,要再开一个数组来储存.所以,种类并查集一般有两个数组 ...
- A Bug's Life(种类并查集)(也是可以用dfs做)
http://acm.hdu.edu.cn/showproblem.php?pid=1829 A Bug's Life Time Limit:5000MS Memory Limit:327 ...
- 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 ...
随机推荐
- Xamarin.Forms特殊的视图BoxView
Xamarin.Forms特殊的视图BoxView BoxView是Xamarin.Forms比较特殊的视图.该视图构建非常简单,其作用也很单一.它的作用就是构成一个特定颜色的色块.在界面设计中, ...
- 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 ...
- Linux有问必答:如何检查Linux的内存使用状况
-1. /proc/meminfo11% -2. atop20% -3. free29% -4. GNOME System Monitor35% -5. htop41% -6. KDE System ...
- Gvim 和 Opencv编译
好奇看了一下Gvim编译器:在官网上也有介绍,github上有源码,安装在电脑上,感觉需求不大,记那些命令也太多了.然后编译opencv过程中cmake成功了,但是VS下编译报了很多错,准备不搞这些了 ...
- Android自己定义实现循环滚轮控件WheelView
首先呈上效果图 watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/d ...
- NYOJ 38 布线问题_(解法1 Kruskal算法)
时间限制:1000 ms | 内存限制:65535 KB 难度:4 描写叙述 南阳理工学院要进行用电线路改造.如今校长要求设计师设计出一种布线方式,该布线方式须要满足下面条件: 1.把全部的楼都供 ...
- hdu杭电1856 More is better【并查集】
Problem Description Mr Wang wants some boys to help him with a project. Because the project is rathe ...
- OA权限树搭建 代码
<ul id="tree"> <s:iterator value="#application.topPrivilegeList"> &l ...
- python正则方法
通过正则替换字符串 res=re.sub(正则,newString,srcString)//返回替换后的字符串 res,m=res.subn(正则,newString,srcString)//返回替换 ...
- android相关文件夹的存取方式与函数解析---全
因为排版问题.转为markdown编辑: http://blog.csdn.net/self_study/article/details/58587412