传送门: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. 读“日请求亿级的QQ会员AMS平台PHP7升级实践”博客心得笔记

    PHP7版本尚未普及,对于前辈们为了性能提升有勇气探索新技术敢于尝螃蟹的精神十分敬佩,倍受鼓舞. PHP7升级面临的风险和挑战 对于一个已经现网在线的大型公共Web服务来说,基础公共软件升级,通常是一 ...

  2. 用canvas画“哆啦A梦”时钟

    前言:今天看完了Js书的canvas画布那张,好开心~又是心爱的canvas~欧耶~ 之前看到有人建议我画蓝胖子,对哦,我怎么把童年最喜欢的蓝胖子忘了,为了表达我对蓝胖子的歉意,所以今天画了会动的he ...

  3. 「拉勾网」薪资调查的小爬虫,并将抓取结果保存到excel中

    学习Python也有一段时间了,各种理论知识大体上也算略知一二了,今天就进入实战演练:通过Python来编写一个拉勾网薪资调查的小爬虫. 第一步:分析网站的请求过程 我们在查看拉勾网上的招聘信息的时候 ...

  4. PHP 依赖注入,从此不再考虑加载顺序

    说这个话题之前先讲一个比较高端的思想--'依赖倒置原则' "依赖倒置是一种软件设计思想,在传统软件中,上层代码依赖于下层代码,当下层代码有所改动时,上层代码也要相应进行改动,因此维护成本较高 ...

  5. 中晟银泰国际中心酒店式公寓介绍 业主交流QQ群:319843248

    行政区域:中原区 区域板块:西北板块 项目位置:中原路与华山路东北角(中原万达北侧中原西路对面) 建筑类型:高层 物业类别:酒店式公寓 户型面积:公寓35-100平米 开发商:中晟集团 投资商:中晟集 ...

  6. [BZOJ1188][HNOI2007]分裂游戏(博弈论)

    题目:http://www.lydsy.com:808/JudgeOnline/problem.php?id=1188 分析: 设SG[i]表示一个石子在位置i上的SG值 这个很容易暴力求,因为i的后 ...

  7. [转]Windows系统注册表知识完全揭密

    来源:http://www.jb51.net/article/3328.htm Windows注册表是帮助Windows控制硬件.软件.用户环境和Windows界面的一套数据文件,注册表包含在Wind ...

  8. C# Rotating Oval

    This program is used to show how to generate an oval. The moon's orbit around the sun is an oval two ...

  9. linux下PHP7环境搭建

    LAMP环境版本 操作系统:Centos 7 Mysql:5.7.11 Apache:2.4.18 PHP:7.0.4     安装Mysql   下载链接:http://dev.mysql.com/ ...

  10. [LeetCode]ZigZag Conversion

    The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows like ...