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!
 /**
题目大意:
判断输入的两个bug(臭虫)是否是同性恋,教授认为他判断的臭虫都是异性的,你的任务就是看教授
的判断是否真确。
①、如果有两个臭虫是同性的,那么输出 Suspicious bugs found! (找到可疑(同性)臭虫)
②、如果 *bug(所有的bug) 都不是同性的,最后输出 no Suspicious bugs found! (没找到可疑(同性)臭虫) 解题算法:并查集 idea:
1、将同性的所有臭虫放在一起。
1.1、如果输入的数据在同一个集合中说明这对臭虫是可疑的
1.2、如果输入的数据不在同一个集合说明它们到现在都还是异性关系
1.2.1、同时,我们假设数据a和数据a+n是异性关系
1.2.1、那么如果a 与 b也是异性关系,就有a+n 与 b是同性(放在同一个集合中) PS:其实,这是一个模板题,只要是这种关系:
<数据只有两种情况、后一个数据的状态会被前面的数据影响、判断新插入的数据是否合法>
**/

核心模板:

 void my_init(int n) // 数据的初始化
{
for(int i = ; i <= n; ++ i)
pre[i] = i;
}
int my_find(int x) // 找到它的根节点
{
int n = x;
while (n != pre[n])
n = pre[n];
int i = x, j;
while (pre[i] != n) //路径压缩
{
j = pre[i];
pre[i] = n;
i = j
}
return n;
}
void my_join(int a, int b) // 不是同一个根节点的数据加入
{
int n1 = my_find(a), n2 = my_find(b);
if (n1 != n2)
pre[n1] = n2;
}
bool my_judge(int a, int b) // 判断是否在同一个集合中
{
int n1 = my_find(a), n2 = my_find(b);
if (n1 != n2) return true;
return false;
}

C/C++代码实现(AC):

 #include <iostream>
#include <algorithm>
#include <cstring>
#include <cstdio> using namespace std; int T, n, m, pre[], flag, cnt; void my_init()
{
int nn = n + n;
for (int i = ; i <= nn; ++ i)
pre[i] = i;
return ;
} int my_find(int x)
{
int n = x;
while (n != pre[n])
n = pre[n];
int i = x, j;
while (pre[i] != n)
{
j = pre[i];
pre[i] = n;
i = j;
}
return n;
} bool judge(int a, int b)
{
int n1 = my_find(a), n2 = my_find(b);
if (n1 != n2) return true;
return false;
} void my_join(int a, int b)
{
int n1 = my_find(a), n2 = my_find(b);
if (n1 != n2) pre[n1] = n2;
return ;
} int main()
{
cnt = ;
scanf("%d", &T);
for(int i = ; i < T; ++ i)
{
flag = ;
scanf("%d%d", &n, &m);
my_init();
while (m --)
{
int a, b;
scanf("%d%d", &a, &b);
if (flag) continue; if (judge(a, b) || judge(a + n, b + n))
{
my_join(a, b + n);
my_join(a + n, b);
}
else
flag = ;
} if (i != ) puts("");
if (flag)
printf("Scenario #%d:\nSuspicious bugs found!\n", cnt ++);
else
printf("Scenario #%d:\nNo suspicious bugs found!\n", cnt ++); }
return ;
}

最新文章

  1. Error:identifer “blockIdx” and __syncthreads() undefined
  2. 遗传算法在JobShop中的应用研究(part 7:整体流程)
  3. Linux系统下设置Tomcat自启动
  4. fifter过滤器
  5. TCP/IP 协议:链路层概述
  6. Spark RDD类源码阅读
  7. android中progress进度条的使用
  8. Marshal.SecureStringToBSTR
  9. Linux--------------更改yum
  10. 上传XML文件字符编码问题
  11. Web 前端利器Emmet 的HTML用法总结
  12. C# 参数按照ASCII码从小到大排序(字典序)
  13. 怎样求FIRST集、FOLLOW集和SELECT集
  14. ROS_Kinetic_15 ROS使用Qt
  15. wxpython多线程通信的应用-实现边录音边绘制音谱图
  16. AliOS-Things ESP8266 编译下载
  17. 后台管理Models
  18. [UGUI]帧动画
  19. 豪斯医生第一季/全集House M.D 1迅雷下载
  20. IE浏览器中的加载项怎么删除

热门文章

  1. fread优化读入
  2. [BZOJ29957] 楼房重建 - 线段树
  3. Windows下Python虚拟环境的配置
  4. 百万年薪python之路 -- 数据库初始
  5. RabbitMQ通过DLX实现消息延迟接收
  6. .net core跨平台应用研究-ubuntu core下配置.net core运行时
  7. Java基础(五)继承和多态
  8. Redis(五)持久化
  9. fenby C语言 P14
  10. anaconda重装jupyter notebook后启动jupyter报错的问题