传送门: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. JavaScript文件加载器LABjs API详解

    在<高性能JavaScript>一书中提到了LABjs这个用来加载JavaScript文件的类库,LABjs是Loading And Blocking JavaScript的缩写,顾名思义 ...

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

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

  3. C# 反射范范的理解下

    程序进行时引入程序集.动态的调用方法属性事件. Assembly类. type类.

  4. 5-touch 命令总结

  5. android开发------编写用户界面之相对布局

    今天要说的是RelativeLayout.RelativeLayout相对于LinearLayout的主要不同点在于它需要一个参照物. 我们先来看一下官方对这个布局的解释: RelativeLayou ...

  6. canvas边界与摩擦力

    处理物体超出画布时的三种基本状态,复位,移除,反弹 (1)检测是否越界的核心算法 if( object.x - object.width / 2 > right || object.x + ob ...

  7. Android NestedScrolling嵌套滑动机制

    Android NestedScrolling嵌套滑动机制 最近项目要用到官网的下拉刷新SwipeRefreshLayout,它是个容器,包裹各种控件实现下拉,不像以前自己要实现事件的拦截,都是通过对 ...

  8. JS实时定位

    <!DOCTYPE html><html lang="en" xmlns="http://www.w3.org/1999/xhtml"> ...

  9. HttpServletRequest 转换成MultipartHttpServletRequest

    //转换 HttpServletRequestMultipartHttpServletRequest mulReq=(MultipartHttpServletRequest)request;//获取上 ...

  10. 天朝git的使用

    开源中国社区 官方网站 https://git.oschina.net/ 开源中国社区成立于2008年8月,其目的是为中国的IT技术人员提供一个全面的.快捷更新的用来检索开源软件以及交流使用开源经验的 ...