POJ 2492 A Bug's Life
传送门:A Bug's Life
Description
Input
Output
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
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的更多相关文章
- 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条臭 ...
- POJ 2492 A Bug's Life(并查集)
http://poj.org/problem?id=2492 题意 :就是给你n条虫子,m对关系,每一对关系的双方都是异性的,让你找出有没有是同性恋的. 思路 :这个题跟POJ1703其实差不多,也是 ...
- (简单) POJ 2492 A Bug's Life,二分染色。
Description Background Professor Hopper is researching the sexual behavior of a rare species of bugs ...
- POJ 2492 A Bug's Life(带权并查集)
题目链接:http://poj.org/problem?id=2492 题目大意:有n只虫子,m对关系,m行每行有x y两个编号的虫子,告诉你每对x和y都为异性,先说的是对的,如果后面给出关系与前面的 ...
- POJ 2492 A Bug's Life (并查集)
A Bug's Life Time Limit: 10000MS Memory Limit: 65536K Total Submissions: 30130 Accepted: 9869 De ...
- POJ 2492 A Bug's Life (并查集)
Background Professor Hopper is researching the sexual behavior of a rare species of bugs. He assumes ...
- POJ 2492 A Bug's Life【并查集高级应用+类似食物链】
Background Professor Hopper is researching the sexual behavior of a rare species of bugs. He assumes ...
- hdu 1829 &poj 2492 A Bug's Life(推断二分图、带权并查集)
A Bug's Life Time Limit: 15000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) To ...
- poj 2492 A Bug's Life 二分图染色 || 种类并查集
题目链接 题意 有一种\(bug\),所有的交往只在异性间发生.现给出所有的交往列表,问是否有可疑的\(bug\)(进行同性交往). 思路 法一:种类并查集 参考:https://www.2cto.c ...
随机推荐
- lecture12-玻尔兹曼机和受限玻尔兹曼机
这是Hinton的第12课,结合前一课可以知道RBM是来自BM,而BM是来自Hopfield的,因为水平有限,是直译的,虽然有时候会看不懂,但是好歹不会曲解原来的本意,看的话:1.先看ppt:2.通读 ...
- PHP学习资料分享
PHP有着开源.执行效率高.优化代码运行等特性,功能强大,被广泛运用,很多大型企业网站开发的首选,百度.腾讯.新浪.金山等均采用了PHP开发.学好PHP对网络开发人员而言具有很好的发展前景,市场对PH ...
- JavaScript函数劫持
一.为什么我会写这篇文章 这篇文章其实是在一个偶然的机会下发现了居然有JavaScript劫持这种东西,虽然这种东西在平时用的比较少,而且一般实用价值不高,但是在一些特殊的情况下还是要使用到的,所以在 ...
- Oracle中序列(SEQUENCE)的使用一例
曾经在触发器中使用序列(SEQUENCE): create or replace trigger TRI_SUPPLIER before insert on SUPPLIER for each row ...
- 重叠(Overlapping) NAT
当内部网络也使用公网注册地址(或者是外网合法地址)时,如果仍使用标准的静态或者动态NAT转换,则可能使得转换的内网地址与外网中合法地址冲突,使数据包又返回到了本地网络,这肯定是不行的.这时我们就要使用 ...
- java中的static详解
如果一个类成员被声明为static,它就能够在类的任何对象创建之前被访问,而不必引用任何对象.static 成员的最常见的例子是main( ) .因为在程序开始执行时必须调用main() ,所以它被声 ...
- fir2(n,f,m)
编辑 函数fir2用来设计多通带任意响应FIR滤波器,该滤波器的幅频特性由向量对f和m确定,f为归一化频率向量,m为对应频率点上的幅度.当设计的滤波器在频率为π的幅度响应不是0时,滤波器的阶数n为偶数
- app状态监听广播
手机中的应用在安装.更新和卸载时都会发送广播 清单文件 <?xml version="1.0" encoding="utf-8"?> <man ...
- AlertDialog之常见对话框(单选对话框、多选对话框、进度条对话框)
单选对话框,顾名思义就是只能选一项(setSingleChoiceItems(Items,)) public void click(View v){ //创建对话框类 AlertDialog.Buil ...
- 【JavaEE企业应用实战学习记录】servlet3.0上传文件
<%-- Created by IntelliJ IDEA. User: Administrator Date: 2016/10/6 Time: 14:20 To change this tem ...