传送门: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. EF下泛型分页方法,更新方法

    /// <summary> /// 获取分页的分页集合 /// </summary> /// <typeparam name="S">实体类型& ...

  2. c/s 自动升级(WebService)

    首先声明,本人文笔不好,大家见笑,欢迎高手吐槽. 做c/s开发肯定会遇到的就是自动升级功能,而这实现方式是非常多. 本文使用 webservice的方式来提供升级服务 首先准备服务 为了方便我们专门用 ...

  3. 化茧成蝶,开源NetWorkSocket通讯组件

    前言 前后历时三年,期间大量参考.Net Framework和Asp.net MVC源代码,写写删删再重构,组件如今更新到V1.5.x了.从原来的丑小鸭,变成今天拥有稳定和强大的tcp协议支持基础层, ...

  4. 文本 To 音频

    文本  To  音频 TextToSpeech介绍 TextToSpeech,简称 TTS,是Android 1.6版本中比较重要的新功能.将所指定的文本转成不同语言音频输出.它可以方便的嵌入到游戏或 ...

  5. LiveSDK初始化/登录时失败的解决办法

    环境描述 Windows 8.1+VS 2013 Update3+Live SDK 5.6 Metro风格的程序,集成LIVE认证 问题描述 如下图,提示Null Reference的异常. 解决办法 ...

  6. 【对noip结束后一个月内的总结】

    最近在刷一些树结构,但发现没有一个提纲,觉得有点不知所措,经常学完一个就发现还有比它更好的,而且比较耗时间.于是沙茶准备按顺序刷bzoj的省选题,看看效果怎么样……求大神指教

  7. html 文本超过显示省略号

    display: -webkit-box; -webkit-box-orient: vertical; -webkit-line-clamp: 2;//显示行数 word-break: break-a ...

  8. python3.4 build in functions from 官方文档 翻译中

    2. Built-in Functions https://docs.python.org/3.4/library/functions.html?highlight=file The Python i ...

  9. Eclipse添加代码注释模板

    Eclipse支持我们自定义模板,比如文件的注释,类注释,函数注释等功能.eclipse自身有自带的模板,我们也可以自己定义.一次点击:windows->preference—>java- ...

  10. Yii 字段验证

    关于验证的属性: $enableClientValidation:是否在客户端验证,也即是否生成前端js验证脚本(如果在form中设置了ajax验证,也会生成这个js脚本). $enableAjaxV ...