传送门:A Bug's Life

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.
写这道题是为了说明一件重要的事:
不能认为Input总是以EOF结尾。
WA到死的写法(注意加粗的两行)
using namespace std;
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<functional>
#include<vector>
#include<map>
#include<queue>
#include<climits>
#define pq priority_queue
#define X first
#define Y second
#define MP make_pair
#define pb push_back
#define Read(x) freopen(x, "r", stdin)
#define scf(x) scanf(x)
#define prf(x) printf(x)
#define set_0(x) memset(x, 0, sizeof(x))
#define set_1(x) memset(x, -1, sizeof(x));
#define rep(i, l, r) for(int i=l; i<r; i++)
typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<pii> vpii; const int MAX_N=2e3+;
int par[MAX_N];
bool rel[MAX_N]; int find(int x){
if(par[x]==x) return x;
int tmp=par[x];
par[x]=find(par[x]);
if(rel[x]) rel[x]=rel[tmp];
else rel[x]=!rel[tmp];
return par[x];
} void unite(int x, int y){
int yy=find(y), xx=find(x);
if(xx==yy) return;
par[xx]=yy;
if(rel[x]==rel[y]) rel[xx]=false;
else rel[xx]=true;
} int main(){
//Read("in");
scanf("%*d");
int N, M, a, b, cs=;
bool ok;
while(~scanf("%d%d", &N, &M)){
for(int i=; i<=N; i++){
par[i]=i;
rel[i]=true; //i与par[i]是否同性
}
ok=true;
while(M--){
scanf("%d%d", &a, &b);
if(!ok) continue;
if(find(a)==find(b)){
if(rel[a]==rel[b]) ok=false;
}
else unite(a, b);
}
if(cs) puts("");
printf("Scenario #%d:\n", ++cs);
puts(ok?"No suspicious bugs found!":"Suspicious bugs found!");
}
return ;
}

AC的姿势:

using namespace std;
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<functional>
#include<vector>
#include<map>
#include<queue>
#include<climits>
#define pq priority_queue
#define X first
#define Y second
#define MP make_pair
#define pb push_back
#define Read(x) freopen(x, "r", stdin)
#define scf(x) scanf(x)
#define prf(x) printf(x)
#define set_0(x) memset(x, 0, sizeof(x))
#define set_1(x) memset(x, -1, sizeof(x));
#define rep(i, l, r) for(int i=l; i<r; i++)
typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<pii> vpii; const int MAX_N=2e3+;
int par[MAX_N];
bool rel[MAX_N]; int find(int x){
if(par[x]==x) return x;
int tmp=par[x];
par[x]=find(par[x]);
if(rel[x]) rel[x]=rel[tmp];
else rel[x]=!rel[tmp];
return par[x];
} void unite(int x, int y){
int yy=find(y), xx=find(x);
if(xx==yy) return;
par[xx]=yy;
if(rel[x]==rel[y]) rel[xx]=false;
else rel[xx]=true;
} int main(){
//Read("in");
int N, M, a, b, cs=, T;
bool ok;
scanf("%d", &T);
while(T--
){
scanf("%d%d", &N, &M);
for(int i=; i<=N; i++){
par[i]=i;
rel[i]=true; //i与par[i]是否同性
}
ok=true;
while(M--){
scanf("%d%d", &a, &b);
if(!ok) continue;
if(find(a)==find(b)){
if(rel[a]==rel[b]) ok=false;
}
else unite(a, b);
}
if(cs) puts("");
printf("Scenario #%d:\n", ++cs);
puts(ok?"No suspicious bugs found!":"Suspicious bugs found!");
}
return ;
}
 

POJ 2492 A Bug's Life的更多相关文章

  1. hdu - 1829 A Bug's Life (并查集)&&poj - 2492 A Bug's Life && poj 1703 Find them, Catch them

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

  2. POJ 2492 A Bug's Life(并查集)

    http://poj.org/problem?id=2492 题意 :就是给你n条虫子,m对关系,每一对关系的双方都是异性的,让你找出有没有是同性恋的. 思路 :这个题跟POJ1703其实差不多,也是 ...

  3. (简单) POJ 2492 A Bug's Life,二分染色。

    Description Background Professor Hopper is researching the sexual behavior of a rare species of bugs ...

  4. POJ 2492 A Bug's Life(带权并查集)

    题目链接:http://poj.org/problem?id=2492 题目大意:有n只虫子,m对关系,m行每行有x y两个编号的虫子,告诉你每对x和y都为异性,先说的是对的,如果后面给出关系与前面的 ...

  5. POJ 2492 A Bug's Life (并查集)

    A Bug's Life Time Limit: 10000MS   Memory Limit: 65536K Total Submissions: 30130   Accepted: 9869 De ...

  6. POJ 2492 A Bug's Life (并查集)

    Background Professor Hopper is researching the sexual behavior of a rare species of bugs. He assumes ...

  7. POJ 2492 A Bug's Life【并查集高级应用+类似食物链】

    Background Professor Hopper is researching the sexual behavior of a rare species of bugs. He assumes ...

  8. hdu 1829 &amp;poj 2492 A Bug&#39;s Life(推断二分图、带权并查集)

    A Bug's Life Time Limit: 15000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) To ...

  9. poj 2492 A Bug's Life 二分图染色 || 种类并查集

    题目链接 题意 有一种\(bug\),所有的交往只在异性间发生.现给出所有的交往列表,问是否有可疑的\(bug\)(进行同性交往). 思路 法一:种类并查集 参考:https://www.2cto.c ...

随机推荐

  1. centos设置静态IP

    1.编辑网卡文件 vi /etc/sysconfig/network-scripts/ifcfg-eth0 # eth0为网卡编号 设置网卡eth0的IPV4信息,需要注意的是,设置的IPADDR需要 ...

  2. Mininet的内部实现原理简介

    原文发表在我的博客主页,转载请注明出处. 前言 之前模拟仿真网络一直用的是Mininet,包括写了一些关于Mininet安装,和真实网络相连接,Mininet简历拓扑的博客,但是大多数都是局限于具体步 ...

  3. Retro 2013

    现在的team里每个迭代都会做一次retro,回顾这两周的情况,有哪些做得好的地方,有哪些做得不足的地方,并制定出一系列action,以期望在下一个迭代中解决这些问题.我觉得这种形式挺不错.因此今年的 ...

  4. 史密斯(smith)圆图讲解

    不管多么经典的射频教程,为什么都做成黑白的呢?让想理解史密斯原图的同学一脸懵逼. 这是什么东东? 今天解答三个问题: 1.是什么? 2.为什么? 3.干什么? 1.是什么? 该图表是由菲利普·史密斯( ...

  5. java util包概述

    util是utiliy的缩写,意为多用途的,工具性质的包这个包中主要存放了:集合类(如ArrayList,HashMap等),随机数产生类,属性文件读取类,定时器类等类.这些类极大方便了Java编程, ...

  6. Android 全屏显示的方法(不包含状态栏)

    我们都知道在Android中某些功能的实现往往有两种方法:一种是在xml文件中设置相应属性,另一种是用代码实现.同样Android实现全屏显示也可以通过这两种方法实现: 1.在AndroidManif ...

  7. 关于QString中的arg()函数使用方法

    例:正确做法:ui->label->setText(QString("Processingfile%1").arg(index));错误做法: ui->label ...

  8. 【POJ 1556】The Doors 判断线段相交+SPFA

    黑书上的一道例题:如果走最短路则会碰到点,除非中间没有障碍. 这样把能一步走到的点两两连边,然后跑SPFA即可. #include<cmath> #include<cstdio> ...

  9. 基于@Aspect的AOP配置

    1. Spring 除了支持Schema 方式配置 AOP,还支持注解方式:使用 @Aspect 来配置 2. Spring 默认不支持 @Aspect 风格的切面声明,通过如下配置开启@Aspect ...

  10. js-比较两个日期的大小

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> <html> <head ...