POJ2492(种类并查集)_猪猪奋斗记的博客-程序员宅基地

技术标签: 并查集  

题目链接:http://poj.org/problem?id=2492

A Bug's Life
Time Limit: 10000MS   Memory Limit: 65536K
Total Submissions: 27388   Accepted: 8907

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!
题目意思就是 给定n只虫子 不同性别的可以在一起 相同性别的不能在一起
给你m对虫子 判断中间有没有同性别在一起的;
我们把同性的放到一个集合里 如果一个集合里出现了异性 则说明存在同性恋在一起
假设 x 为一种性别 x+n为与其相反的性别  
若a,b为同性 的  我们则可以把判断 (a,b+n)  (b,a+n)为异性反之亦然;
代码如下:
#include <iostream>
#include <cstring>
#include <cstdio>
using namespace std;
const int maxn = 5000;
int par[maxn];
void init(int n){
   for(int i=0;i<=n;i++)
      par[i]=i;
}
int find(int x){
    if(x!=par[x])
        par[x]=find(par[x]);
    return par[x];
}
void Union(int a,int b){
    int x=find(a);
    int y=find(b);
    if(x!=y)
       par[x]=y;
}
bool judge(int x,int y){//判断是否为同性,异性为真,同性为假
   x=find(x);
   y=find(y);
   if(x!=y)
    return true;
   return false;
}
int main()
{
    int t,n,m,cas=1;
    scanf("%d",&t);
    for(int i=1;i<=t;i++){
        scanf("%d%d",&n,&m);
        init(n*2);
        bool l=1;
        int x,y;
        while(m--){
            scanf("%d%d",&x,&y);
            if(judge(x,y)||judge(x+n,y+n)){
                Union(x,y+n);//把同性的加到一个集合里
                Union(x+n,y);
            }
            else
                l=0;
        }
        if(i!=1)
            puts("");
        printf("Scenario #%d:\n",cas++);
        if(l)
            printf("No suspicious bugs found!\n");
        else
            printf("Suspicious bugs found!\n");
    }
    return 0;
}


版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/u013790563/article/details/30255265

智能推荐

雷达原理笔记之恒虚警概率检测_雷达检测概率_在路上-正出发的博客-程序员宅基地

雷达原理笔记——恒虚警概率检测——南京理工大学许志勇老师的《雷达原理课程》浅析恒虚警概率检测技术是雷达设计过程中经常涉及到的问题。由于噪声的存在,雷达在探测目标时不可避免地会出现虚警情况。而这种虚警概率的高低则是反应雷达探测性能的重要指标。“恒虚警检测”顾名思义就是在保证虚警概率一定的情况下,尽可能高的提高发现概率。上图是,雷达设计过程经常用到的一个概率分布图。一般来说,噪声都是服从0均值的高斯分布,其包络服从瑞利分布。目标和噪声的包络服从莱斯分布(Rice分布)或者广义瑞利分布。横坐标是对噪声

上传三合一:拖拽上传、上传文件、上传文件夹,一次搞定!_呀呀夫斯基的博客-程序员宅基地

拖拽上传、上传文件、上传文件夹,三合一!1 拖拽上传在 drop 事件中通过 e.dataTransfer.items 递归获取拖拽的文件(夹)。具体看这篇文章:拖拽本地文件夹到浏览器中,展示所有文件结构2 上传文件代码:&lt;template&gt; &lt;section&gt; &lt;!-- 添加 multiple 属性,可以同时选择多个文件 --&gt; ...

python爬虫做灰产_越来越多的网站具有反爬虫特性,有的用图片隐藏关键数据_weixin_39627661的博客-程序员宅基地

基于CNN的验证码图片识别简介本项目采用alexnet模型和letnet模型,可根据实际需要选择(在train_model.py中的train函数修改即可)95.5%作者有话说不知不觉这个git库伴随我从16到到20年,带给我自己最棒的一段人生旅程,整理了这份文档,希望任何想学习图片识别,玩玩卷积神经网络的同学可以最便捷的上手体验。请谨慎使用技术,仅支持学习,不支持任何黑灰产相关可参看:https...

python读csv文件-python读写csv文件方法详细总结_weixin_37988176的博客-程序员宅基地

python提供了大量的库,可以非常方便的进行各种操作,现在把python中实现读写csv文件的方法使用程序的方式呈现出来。在编写python程序的时候需要csv模块或者pandas模块,其中csv模块使不需要重新下载安装的,pandas模块需要按照对应的python版本安装。在python2环境下安装pandas的方式是:sudo pip install pandas在python3环境下安装p...

matlab数组删除指定行列元素_matlab数组删除元素_zyc滴的博客-程序员宅基地

删除第一列元素a=[1,2,3 4,5,6];a(:,1)=[];a结果a = 2 3 5 6删除第一行元素a=[1,2,3 4,5,6];a(1,:)=[];a单独删除一个元素a=[1,2,3,4,5,6];a(1)=[];a

Eclipse导入项目后,所有jsp文件报错_是华仔呀的博客-程序员宅基地

报错如上图。解决:项目右键-&amp;gt;Build Path-&amp;gt;Configure Build Path&amp;nbsp;-&amp;gt;&amp;nbsp; &amp;nbsp;-&amp;gt; OK

随便推点

【VBA(三):操作工作表】【Worksheets工作表对象+Application主程序对象+小结及练习】_LinGavinQ的博客-程序员宅基地

本章主要内容:Worksheets工作表对象,Application主程序对象,小结及练习。

@JsonFormat和@DataFormat的使用_hello_cmy的博客-程序员宅基地

注解@JsonFormat主要是后台到前台的时间格式的转换 注解@DataFormat主要是前后到后台的时间格式的转换@DateTimeFormat 和 @JsonFormat 可将日期信息在JSON格式和java.util.Date对象之间转换@DateTimeFormat此为Spring框架提供的注解,将JSON格式的日期信息信息解析转换并绑定到Date对象中,该注...

Unity3D-微信跳一跳(一)_unity3d跳一跳小游戏_广信IT001的博客-程序员宅基地

一、设置开始游戏的场景、UI与玩家操作的对象。voidStart(){CurrentStage = Stage;//相机的位置减去人物的位置,获得他们的相对距离CameraRelativePosition = Camera.main.transform.position - transform.position;StageInitPos = Stage.transform.localPosition;StageInitScale = Stage.transform.loca.

conda的安装和使用_idea conda_Childs丶的博客-程序员宅基地

目录概念阐述conda下载与安装conda的使用集成conda到IDEA中一、概念阐述pip、conda和anaconda的解释和区别:pip在python的世界里也浸淫多年了,我们早已习惯有 pip ,easy_install 和virtualenv的世界,但是这些工具没有解决我们所有的需求哦。这其中主要的问题是他们全部都集中解决关于python相关问题而忽略了非py...

redis通用key操作命令(总)_古月的三个锦囊的博客-程序员宅基地

redis默认是开启了16个数据库,在配置文件中可以修改,编号从0到15,默认选择的是0号数据库,通过使用select index命令来更改数据库1.keys pattern 命令–>获取key值 在redis里,允许模糊查询key 有3个通配符 *, ? ,[] *: 通配任意多个字符 ?: 通配单个字符 []: 通配括号内的某1个字符redis

windows改变docker 镜像储存位置_勤奋的Alivon的博客-程序员宅基地

windows10 改变docker 镜像存储位置windows 10下面承载docker 默认的虚拟机时hyper-v,默认使用的镜像是MobyLinuxVM。首先你运行过docker for windows。开启了hyper-v。windows管理工具开启hyper-v管理器,也可以再控制面板=&gt;管理工具找到中间虚拟机框右击设置更改虚拟磁盘位置到你想要更改的位置右键点击...

推荐文章

热门文章

相关标签