A Bug's Life POJ - 2492

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.
 
题意:有一些群体的昆虫,两两一对,每一对都是在一起的,问给出的里面有没有同性恋的昆虫
题解:带权并查集,寻找与根节点的状态是否是一样的(是同性恋还是异性恋)
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring>
#include<sstream>
#include<cmath>
#include<stack>
#include<map>
#include<cstdlib>
#include<vector>
#include<string>
#include<queue>
using namespace std; #define ll long long
#define llu unsigned long long
#define INF 0x3f3f3f3f
const double PI = acos(-1.0);
const int maxn = 5e4+;
const int mod = 1e9+; struct node
{
int pre;
int relation; //0:同 1:异
}p[maxn]; int find(int x)
{
int temp;
if(x == p[x].pre)
return x;
temp = p[x].pre;
p[x].pre = find(temp);
p[x].relation = (p[x].relation + p[temp].relation + ) % ;
return p[x].pre;
}
void combine(int x,int y)
{
int xx = find(x);
int yy = find(y);
if(xx != yy)
{
p[xx].pre = yy;
p[xx].relation = (p[y].relation - p[x].relation) % ;
}
}
int main()
{
int t;
scanf("%d",&t);
int ca=;
while(t--)
{
int n,m;
int flag = ;
scanf("%d%d",&n,&m);
for(int i=;i<=n;i++)
{
p[i].pre = i;
p[i].relation = ;
}
for(int i=;i<m;i++)
{
int a,b;
scanf("%d %d",&a,&b);
if(find(a) == find(b))
{
if(p[a].relation == p[b].relation)
flag = ;
}
else
combine(a,b);
}
if(flag)
printf("Scenario #%d:\nSuspicious bugs found!\n\n",ca++);
else
printf("Scenario #%d:\nNo suspicious bugs found!\n\n",ca++);
}
}

A Bug's Life POJ - 2492 (带权并查集)的更多相关文章

  1. poj 1182 (带权并查集)

    食物链 Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 71361   Accepted: 21131 Description ...

  2. A Bug’s Life POJ - 2492(种类并查集)

    题目链接 每次给出两个昆虫的关系(异性关系),然后发现这些条件中是否有悖论 就比如说第一组数据 1 2 2 3 1 3 1和2是异性,2和3是异性,然后说1和3是异性就显然不对了. 我们同样可以思考一 ...

  3. poj 1733(带权并查集+离散化)

    题目链接:http://poj.org/problem?id=1733 思路:这题一看就想到要用并查集做了,不过一看数据这么大,感觉有点棘手,其实,我们仔细一想可以发现,我们需要记录的是出现过的节点到 ...

  4. POJ 1703 带权并查集

    直接解释输入了: 第一行cases. 然后是n和m代表有n个人,m个操作 给你两个空的集合 每个操作后面跟着俩数 D操作是说这俩数不在一个集合里. A操作问这俩数什么关系 不能确定:输出Not sur ...

  5. Navigation Nightmare POJ - 1984 带权并查集

    #include<iostream> #include<cmath> #include<algorithm> using namespace std; ; // 东 ...

  6. Parity game POJ - 1733 带权并查集

    #include<iostream> #include<algorithm> #include<cstdio> using namespace std; <& ...

  7. hdu 1829 &amp;poj 2492 A Bug&#39;s Life(推断二分图、带权并查集)

    A Bug's Life Time Limit: 15000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) To ...

  8. poj 2492 A Bug's Life【带权并查集】

    就是给一个无向图判是否有奇环 用带权并查集来做,边权1表示连接的两个节点异性,否则同性,在%2意义下进行加法运算即可,最后判相同的时候也要%2,因为可能有负数 #include<iostream ...

  9. POJ 2492 A Bug's Life 带权并查集

    题意: 思路: mod2 意义下的带权并查集 如果两只虫子是异性恋,它们的距离应该是1. 如果两只虫子相恋且距离为零,则它们是同性恋. (出题人好猥琐啊) 注意: 不能输入一半就break出来.... ...

随机推荐

  1. Oracle的表创建和事务管理

    Oracle的表创建和事务管理 - CURD,根据查询结果创建新表 - 事务管理 - 什么是事务 ,为什么要用事务 - SQL99事务隔离级别 - Oracle事务隔离级别 - 事务回滚 - 隐式回滚 ...

  2. Python3基础(3)集合、文件操作、字符转编码、函数、全局/局部变量、递归、函数式编程、高阶函数

    ---------------个人学习笔记--------------- ----------------本文作者吴疆-------------- ------点击此处链接至博客园原文------ 1 ...

  3. Python3基础(2)模块、数据类型及运算、进制、列表、元组、字符串操作、字典

    ---------------个人学习笔记--------------- ----------------本文作者吴疆-------------- ------点击此处链接至博客园原文------ 1 ...

  4. ACdream 1431——Sum vs Product——————【dfs+剪枝】

    Sum vs Product Time Limit: 2000/1000MS (Java/Others)    Memory Limit: 128000/64000KB (Java/Others) S ...

  5. 工作经验(Unity篇)

    我的工作是C++开发,主要是做底层,其中绝大部分是给Unity调用的,以下是我的脚印,希望不会重蹈覆辙 Unity具有强大的跨平台性,但是使用到库文件不尽相同,例如Android中就使用so库文件,W ...

  6. C#对INI文件读写

    C#本身没有对INI格式文件的操作类,可以自定义一个IniFile类进行INI文件读写. using System; using System.Collections.Generic; using S ...

  7. springboot mybatis 自动生成代码(maven+IntelliJ IDEA)

    1.在pom文件中加入需要的依赖(mybatis-generator-core) 和 插件(mybatis-generator-maven-plugin) <dependency> < ...

  8. centos执行apt-get提示不存在

    在centos下用yum install xxx yum和apt-get的区别 一般来说著名的linux系统基本上分两大类: 1.RedHat系列:Redhat.Centos.Fedora等 2.De ...

  9. eclipse之插件添加

    在ftp.properties文件中,中文出现十六进制显示情况,如下: 解决该问题只需要在eclipse中下载一个插件即可解决, 步骤: help  ---> install new softw ...

  10. java集合杂谈

    一:java集合框架如下图所示: 大致说明:看上面的框架图,先抓住它的主干,即Collection和Map. 1.Collection是一个接口,是高度抽象出来的集合,它包含了集合的基本操作和属性.C ...