并查集--poj 2492

时间:2022-05-07 03:38:47
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.
 
题目大意:有N个虫子,M个关系,判断这些虫子的关系(即性取向)是否正确
      按照样例输出。
解题思路:很明显用并查集比较好处理
     处理时候有一个就是保存深度的数组ra[Maxn],0表示虫子是同性,1表示虫子是异性
 
下面就闲话少叙,代码走起
 
/*之前一直没搞懂为什么ra这个数组要取余运算原来是用0
表示同类,1表示不同类,所以下面的21还有30行会取模*/
#include<iostream>
#include<cstdio>
using namespace std;
const int Maxn=2005;
int pa[Maxn],ra[Maxn];//用来存储父节点还有关系
void init(int n)
{
for(int i=1;i<=n;i++)
{
pa[i]=i;
ra[i]=0;
}
}
int find_set(int x)
{
int t=pa[x];
if(pa[x]==x) return x;
pa[x]=find_set(t);
ra[x]=(ra[x]+ra[t])%2;
return pa[x];
}
void union_set(int x,int y)
{
int fx=find_set(x);
int fy=find_set(y);
if(fx==fy) return;
pa[fx]=fy;
ra[fx]=(ra[x]+1+ra[y])%2;
}
int main()
{
int T;
int num,ncase;
int x,y;
scanf("%d",&T);
// {
for(int i=1;i<=T;i++)
{
bool flag=true;
scanf("%d%d",&num,&ncase);
init(num);
while(ncase--)
{
scanf("%d%d",&x,&y);
if(find_set(x)==find_set(y))
{
if(ra[x]==ra[y])
{
flag=false;
continue;
}
}
else
{
union_set(x,y);
}
}
printf("Scenario #%d:\n", i);
if(flag) printf("No suspicious bugs found!\n");
else printf("Suspicious bugs found!\n");
printf("\n");
}
// }
return 0;
}

并查集--poj 2492的更多相关文章

  1. hdu - 1829 A Bug&&num;39&semi;s Life &lpar;并查集&rpar;&amp&semi;&amp&semi;poj - 2492 A Bug&&num;39&semi;s Life &amp&semi;&amp&semi; poj 1703 Find them&comma; Catch them

    http://acm.hdu.edu.cn/showproblem.php?pid=1829 http://poj.org/problem?id=2492 臭虫有两种性别,并且只有异性相吸,给定n条臭 ...

  2. &lbrack;并查集&rsqb; POJ 1703 Find them&comma; Catch them

    Find them, Catch them Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 43132   Accepted: ...

  3. &lbrack;并查集&rsqb; POJ 2236 Wireless Network

    Wireless Network Time Limit: 10000MS   Memory Limit: 65536K Total Submissions: 25022   Accepted: 103 ...

  4. &lbrack;并查集&rsqb; POJ 1182 食物链

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

  5. &lbrack;并查集&rsqb; POJ 1611 The Suspects

    The Suspects Time Limit: 1000MS   Memory Limit: 20000K Total Submissions: 35206   Accepted: 17097 De ...

  6. &lbrack; An Ac a Day &Hat;&lowbar;&Hat; &rsqb; &lbrack;kuangbin带你飞&rsqb;专题五 并查集 POJ 2236 Wireless Network

    题意: 一次地震震坏了所有网点 现在开始修复它们 有N个点 距离为d的网点可以进行通信 O p   代表p点已经修复 S p q 代表询问p q之间是否能够通信 思路: 基础并查集 每次修复一个点重新 ...

  7. 并查集 POJ 1988

    #include <cstdio> #define N 30010 int pa[N]; //parent int siz_tree[N]; //size of tree int d[N] ...

  8. 【转】并查集&amp&semi;MST题集

    转自:http://blog.csdn.net/shahdza/article/details/7779230 [HDU]1213 How Many Tables 基础并查集★1272 小希的迷宫 基 ...

  9. POJ 2492 并查集扩展(判断同性恋问题)

    G - A Bug's Life Time Limit:10000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u S ...

随机推荐

  1. Java对象的XML序列化&lpar;转&rpar;

    转自:http://westlifesz.javaeye.com/blog/48618 java.io.Serializable引发的问题——什么是序列化?在什么情况下将类序列化?  序列化就是一种用 ...

  2. sed 4个功能

    [root@lanny test]# cat test.txt test liyao lanny 经典博文: http://oldboy.blog.51cto.com/2561410/949365 h ...

  3. libyuv 编译for ios

    这里有编译好的库 https://bintray.com/yarr/ios/libyuv-ios# lipo -info libyuv.a  Architectures in the fat file ...

  4. &lowbar;&lowbar;stdcall 与 &lowbar;&lowbar;cdecl

    (1) _stdcall调用 _stdcall是Pascal程序的缺省调用方式,参数采用从右到左的压栈方式,被调函数自身在返回前清空堆栈. WIN32 Api都采用_stdcall调用方式,这样的宏定 ...

  5. Asp&period;NET 之 路径浅析

    比如你的工程是Web(url是:http://localhost/web/default.aspx) Request.ApplicationPath 就是/Web 如果是站点就直接返回"/& ...

  6. xrdp远程 &amp&semi; watchdog 启用与测试 &amp&semi; WebRTC

    sudo apt-get install xrdp sudo apt-get install vnc4server tightvncserver echo "xfce4-session&qu ...

  7. c&num; 设计模式&lpar;一&rpar; 工厂模式

    源代码在github上面,需要的自己进行下载:https://github.com/yuzhoukamen/UnikmDesignPattern.git 工厂模式(Factory Pattern)是最 ...

  8. Linux DTS&lpar;Device Tree Source&rpar;设备树详解之二&lpar;dts匹配及发挥作用的流程篇&rpar;【转】

    转自:https://blog.csdn.net/radianceblau/article/details/74722395 版权声明:本文为博主原创文章,未经博主允许不得转载.如本文对您有帮助,欢迎 ...

  9. linux下文件共享的几种常用方式

    1. python方式,做一个简单的服务器.默认是开启8000端口. > python -m SimpleHTTPServer 执行命令后,在浏览器上输入该机器IP+8000端口即可 2. sc ...

  10. Eclipce 配置javaEE

    Eclipse 安装JavaEE插件   Oxygen版Eclipse 导入项目会自动安装你项目需要的一些插件,但是有时候会安装失败,需要手动安装: 这里以Dynamic Web Project项目为 ...