A Bug's Life

时间限制:1000 ms  |  内存限制:65535 KB
难度:4
描述
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.

输入
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 10000) 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.
输出
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.
样例输入
2
3 3
1 2
2 3
1 3
4 2
1 2
3 4
样例输出
Scenario #1:
Suspicious bugs found!Scenario #2:
No suspicious bugs found!

解题报告:种类并查集。

code

方法一:把性别相同的虫子放在同一个集合,然后每读入一对虫子号,判断它们在不在同一集合,在则同性别,不在则继续。

#include<iostream>
#include<stdio.h>
#include<queue>
#include<vector>
#include<stack>
#include<cstring>
#include<algorithm>using namespace std;typedef long long ll;
const int MAXN = 10005; /*结点数目上限*/
int pa[MAXN];    /*pa[x]表示x的父节点*/
int rank[MAXN];    /*rank[x]是x的高度的一个上界*/
int gender[MAXN];  // 与i性别相反的虫子号/*创建一个单元集*/
void make_set(int x)
{pa[x] = x;rank[x] = 0;gender[x]=0;
}/*带路径压缩的查找*/
int find_set(int x)
{if(x != pa[x]){pa[x] = find_set(pa[x]);}return pa[x];
}/*按秩合并x,y所在的集合*/
void union_set(int x, int y)
{x = find_set(x);y = find_set(y);if(rank[x] > rank[y])/*让rank比较高的作为父结点*/{pa[y] = x;}else{pa[x] = y;if(rank[x] == rank[y])rank[y]++;}
}int main()
{//  freopen("input.txt","r",stdin);int t,m,n,k=1;scanf("%d",&t);while(t--){scanf("%d%d",&m,&n);for(int i=1;i<=m;i++){ //初始化make_set(i);}int a,b,flag=1;for(int i=0;i<n;i++){scanf("%d%d",&a,&b);if(!flag) //结果出来之后也要把数据读完continue;if(find_set(a)==find_set(b)){  //判断两个虫子在不在同一个集合flag=0;continue;}if(gender[a]==0) gender[a]=b; //把性别相同的虫子放在同一个集合里else union_set(gender[a],b);if(gender[b]==0) gender[b]=a;else union_set(gender[b],a);}if(k!=1)printf("\n");if(flag)printf("Scenario #%d:\nNo suspicious bugs found!\n",k++);elseprintf("Scenario #%d:\nSuspicious bugs found!\n",k++);}return 0;
}

方法二:

#include<cstdio>
#include<iostream>
#include<cstring>
#include<string>
#include<sstream>
#include<algorithm>
#include<math.h>
#include<queue>
#include<stack>
#include<map>
#include<set>
using namespace std;
const int MAXN = 10005; /*结点数目上限*/
int pa[2*MAXN];    /*2种动物*//*创建一个单元集*/
void make_set(int x)
{pa[x] = x;
}/*带路径压缩的查找*/
int find_set(int x)
{if(x != pa[x])pa[x] = find_set(pa[x]);return pa[x];
}/*按秩合并x,y所在的集合*/
void union_set(int x, int y)
{x = find_set(x);y = find_set(y);if(x == y) return;pa[x]=y;
}int main()
{//  freopen("input.txt","r",stdin);int t,n,k,x,y,m=1;scanf("%d",&t);while(t--){/*i~n是男,i+n~i+2*n是女*/for(int i=0;i<2*MAXN;i++)make_set(i);scanf("%d%d",&n,&k);int flag=1;while(k--){scanf("%d%d",&x,&y);if(!flag) continue;if(find_set(x)==find_set(y)||find_set(x+n)==find_set(y+n)){/*是否是同性*/flag=0;continue;}else{union_set(x,y+n);/*男-女*/union_set(x+n,y);/*女-男*/}}printf("Scenario #%d:\n",m++);if(flag) printf("No suspicious bugs found!\n\n");else printf("Suspicious bugs found!\n\n");}
}

A Bug's Life相关推荐

  1. spring boot 文件上传工具类(bug 已修改)

    以前的文件上传都是之前前辈写的,现在自己来写一个,大家可以看看,有什么问题可以在评论中提出来. 写的这个文件上传是在spring boot 2.0中测试的,测试了,可以正常上传,下面贴代码 第一步:引 ...

  2. 微信无法连接服务器501,微信成语猜猜看第501关BUG出现全是英文怎么过解决方法...

    近日因为跳一跳许多微信小程序游戏异常火爆,其中就包括了成语猜猜看游戏,但是很多小伙伴向小编反应游戏之中出现了BUG,那么微信成语猜猜看BUG怎么办呢?为了帮助各位小伙伴,小编特意带来了成语猜猜看BUG ...

  3. vscode 格式化某一段代码_VSCode格式化代码功能失效的bug解决方法

    VSCode格式化代码功能失效的bug解决方法 前不久我装上了 黑苹果,那么为了快速转移开发环境,我使用了VSCode(Visual Studio Code下面简称VSCode)的插件 Setting ...

  4. 禅道设置bug模板_一款热度很高的项目管理和bug工具,免费使用,可在公司推广哦...

    以前在公司会用到各种bug管理工具,但使用最顺手的感觉还是禅道,主要是它除了能满足我的日常工作之外,用户体验上也做的不错 .前段时间领导碰巧看到了工具,觉得使用它管理项目应该不错,打算在全公司推广,让 ...

  5. 困扰一周的奇葩bug:重复相似代码多,导致单片机程序跑飞

    今天是个好日子,困扰一周的bug终于解决了,迫不及待将这个奇葩问题分享给各位朋友~ 硬件环境: 国产MCU:华大HC32L130 问题描述: 最近做一款基于Modbus协议的三通道温度采集模块,程序设 ...

  6. java string 占位符_驳《阿里「Java开发手册」中的1个bug》?

    前两天写了一篇关于<阿里Java开发手册中的 1 个bug>的文章,评论区有点炸锅了,基本分为两派,支持老王的和质疑老王的. 首先来说,无论是那一方,我都真诚的感谢你们.特别是「二师兄」, ...

  7. 开发人员绩效考核中有效bug数的统计

    我们都知道,开发人员的考核中,bug这块占了一定的比重,那么我们在统计每个开发人员的bug数时,显然要做到有效,不能把缺陷管理系统上的bug不经过处理,就直接进行统计. 如何统计有效bug数呢? 我们 ...

  8. 一个GDIPlus的Bug -- OutofMemory异常

    今天发现 framework2.0中的一个GDIPlus的Bug: 在Form的OnPaint事件里面写如下代码: private void Form1_Paint(object sender, Pa ...

  9. 从难免的线上bug说起代码的思考

    经常是某司线上又出bug了,然后是给公司造成多少损失,追根究底总是可以找到一些原因,诸如:写代码逻辑考虑不全面,或者代码有硬伤,也有测试不充分,甚至不测试都有,也有是运维的问题等等. 我对测试部专业, ...

  10. Visual Studio2005奇怪的bug及解决【月儿原创】

    Visual Studio2005查看设计器打开失败的bug及解决 作者:清清月儿 主页:http://blog.csdn.net/21aspnet/           时间:2007.3.23 在 ...

最新文章

  1. linux PROC文件系统详解
  2. undefined reference to `cv::VideoCapture::VideoCapture()'
  3. 前端学习(2798):实现图片预览的效果
  4. linux下删除cmake_ubuntu 16.04 + cmake 升级
  5. java gridout_java 布局管理器GridLayout
  6. 业界资讯:adobe creative suite 5.5你准备好了吗?
  7. 如何使用go读写excel
  8. 如何在内存中存储有序数据?
  9. linux mysql 密码策略_Linux Ubuntu 14 MySQL 密码策略(复杂度)和审计插件
  10. 状态转移矩阵 matlab,状态转移矩阵计算.PPT
  11. 在浏览器输入地址回车后,发生了什么!
  12. 相干信号与非相干信号
  13. NP问题真的很难理解
  14. error怎么开机 fan_电脑开机后显示CPU Fan Error错误提示怎么办?分享六种解决方法...
  15. J-link无法下载固件问题
  16. 【CSS】来自新时代的CSS
  17. 阿噗啊噗服务器维护,这些App我能笑一年!阿噗整理的20个奇葩App,没玩过你就OUT了!...
  18. SM2SM4实现字符串和文件的数字信封(不限制文件大小)
  19. c#线程学习之ManualResetEvent和AutoResetEvent的区别!
  20. 李祥对数值策划的定义

热门文章

  1. bootstrap4 知识总结
  2. k近邻算法python解读_Python3《机器学习实战》学习笔记(一):k-近邻算法(史诗级干货长文)...
  3. 东华大学java_东华大学继续教育学院 2020年春季学期 远程学历教育《Java程序设计》期末大作业...
  4. “+智能”与混合云成为战略主线 郑叶来如何解码华为云2018年终总结...
  5. JAVA多线程终止线程、退出线程、Interrupt()方法、苦逼的搬砖工
  6. 人机博弈之(一)------博弈介绍
  7. Oracle报“无监听程序”解决方案
  8. sharepoint主题样式定制
  9. C++学习(一五一)hwnd hdc hglrc
  10. 学生免费领取阿里云ECS云服务器并使用全过程(部署个人博客项目)