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. mysql数据库忘记密码时如何修改(二)

    第一步:找到mysql数据库的my.ini配置文件,在[mysqld]下面添加一行代码:skip-grant-tables 第二步:运行services.msc进入服务管理界面,重启mysql服务. ...

  2. 使用Pycharm开发python下django框架项目生成的文件解释

    目录MyDjangoProject下表示工程的全局配置,分别为setttings.py.urls.py和wsgi.py,1.其中setttings.py包括了系统的数据库配置.应用配置和其他配置,2. ...

  3. cairo-dock天气位置代码

    cairo-dock天气位置代码: 城市: 北京CHXX0008哈尔滨CHXX0046长春CHXX0010沈阳CHXX0119大连CHXX0019天津CHXX0133呼和浩特CHXX0249乌鲁木齐C ...

  4. Java项目—嗖嗖移动业务大厅

    嗖嗖移动业务大厅包类(如下图): SosoMgr: package cn.biz; import java.util.Scanner; import cn.common.Common; import ...

  5. 从零开始的全栈工程师——js篇2.4

    条件语句与循环语句 变量提升: 变量提升是浏览器的一个功能,在运行js代码之前,浏览器会给js一个全局作用域叫window ,window分两个模块,一个叫内存模块,一个叫运行模块,内存模块找到当前作 ...

  6. eclipse之插件添加

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

  7. mysql5.6.31安装及配置

    1,下载安装包解压到安装位置.下载地址https://dev.mysql.com/downloads/mysql/5.6.html#downloads 2,修改默认配置文件 在根目录下面有my-def ...

  8. javascript的常用操作(二)

    Undefined 不是 Null 在 JavaScript 中, null 用于对象, undefined 用于变量,属性和方法. 对象只有被定义才有可能为 null,否则为 undefined. ...

  9. 使用JDBC操作SAP云平台上的HANA数据库

    本文假设您对JDBC(Java Database Connectivity)有最基本的了解.您也可以将其同ADBC(ABAP Database Connectivity)做对比,细节请参考我的博客AD ...

  10. javascript:理解DOM事件

    首先,此文不讨论繁琐细节,但是考虑到读者的心灵感受,本着以积极向上的心态,在此还是会列举示例说明. ​标题为理解DOM事件,那么在此拿一个简单的点击事件为例,希望大家看到这个例子后能触类旁通. DOM ...