A Bug's Life

Time Limit: 10000MS   Memory Limit: 65536K
Total Submissions: 45757   Accepted: 14757

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

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!

Hint

Huge input,scanf is recommended.

题意:

给出m对关系,他们是互斥的,问给出的关系是否有矛盾。

题解:

种类并查集,可以用带权并查集解决,这里我就多开了两个集合。

思路可以参见我之前的一篇博客:https://www.cnblogs.com/heyuhhh/p/9980330.html

代码如下:

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
using namespace std; const int N = ;
int t,n,m;
int f[N]; int find(int x){
return f[x]==x ? f[x] :f[x]=find(f[x]);
}
bool same(int x,int y){
return find(x)==find(y);
}
void Union(int x,int y){
int fx=find(x),fy=find(y);
f[fx]=fy;
}
int main(){
scanf("%d",&t);int tot=;
while(t--){
tot++;
bool flag=true;
scanf("%d%d",&n,&m);
for(int i=;i<=N-;i++) f[i]=i;
for(int i=,x,y;i<=m;i++){
scanf("%d%d",&x,&y);
if(!flag) continue ;
int fx=find(x),fy=find(y);
if(!same(x,y) && !same(x+n,y+n)){
Union(x,y+n);Union(x+n,y);
}else flag=false;
}
printf("Scenario #%d:\n",tot);
if(flag) puts("No suspicious bugs found!");
else puts("Suspicious bugs found!");
printf("\n");
}
return ;
}

最新文章

  1. Angular JS学习之指令
  2. iOS中数字的格式化 NSNumberFormatter
  3. CoreAnimation-05-CABasicAnimation
  4. H2的MVStore
  5. powershell ise好字库和diy配色文件分享
  6. JavaWeb笔记——JSTL标签
  7. 数据聚合 &amp; 分组:新一代系统监控的核心功能
  8. oracle稳定执行计划1
  9. VS2015连接Oracle数据库(转)
  10. NET基础课--Linq第二讲
  11. Mysql 锁基础
  12. 什么是Vagrant
  13. html常用代码合集
  14. HDFS的副本存放策略(全)
  15. adb查看安卓设备系统Android版本
  16. JS之JSON.parse和JSON.stringify
  17. maven 常用 Archetypes
  18. TensorFlow入门教程集合
  19. SQL Server数据库定时备份解决方案
  20. 使用Word 进行UTF8 以及字符串编码的转换操作

热门文章

  1. CAS解扰小结
  2. CentOs安装Mysql和配置初始密码
  3. centos7下安装elasticSearch错误总结(单节点模式)
  4. 删除txt文件内容
  5. 「日常训练」「小专题·USACO」 Ski Course Design (1-4)
  6. 第二十一篇 json,picklz,xml模块
  7. 洛谷P1379八数码难题
  8. 问题 B: Prime Number
  9. 21天学习caffe(一)
  10. Week2 Teamework from Z.XML 软件分析与用户需求调查(四)Bing桌面及助手的现状与发展