HDU 1829 A Bug's Life 【带权并查集/补集法/向量法】
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.
InputThe 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.OutputThe 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.
#include<cstdio>
#include<string>
#include<cstdlib>
#include<cmath>
#include<iostream>
#include<cstring>
#include<set>
#include<queue>
#include<algorithm>
#include<vector>
#include<map>
#include<cctype>
#include<stack>
#include<sstream>
#include<list>
#include<assert.h>
#include<bitset>
#include<numeric>
#define debug() puts("++++")
#define gcd(a,b) __gcd(a,b)
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define fi first
#define se second
#define pb push_back
#define sqr(x) ((x)*(x))
#define ms(a,b) memset(a,b,sizeof(a))
#define sz size()
#define be begin()
#define pu push_up
#define pd push_down
#define cl clear()
#define lowbit(x) -x&x
#define all 1,n,1
#define rep(i,x,n) for(int i=(x); i<(n); i++)
#define in freopen("in.in","r",stdin)
#define out freopen("out.out","w",stdout)
using namespace std;
typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int,int> P;
const int INF = 0x3f3f3f3f;
const LL LNF = 1e18;
const int maxn = +;
const int maxm = 1e6 + ;
const double PI = acos(-1.0);
const double eps = 1e-;
const int dx[] = {-,,,,,,-,-};
const int dy[] = {,,,-,,-,,-};
int dir[][] = {{,},{,-},{-,},{,}};
const int mon[] = {, , , , , , , , , , , , };
const int monn[] = {, , , , , , , , , , , , };
int t,n,m,d,x,y;
int fa[maxn*];
inline int read()
{
int sum=;
char ch=getchar();
while(ch>''||ch<'') ch=getchar();
while(ch>=''&&ch<='') sum=sum*+ch-,ch=getchar();
return sum;
}
int Find(int x)
{
return fa[x]==x ? x:fa[x]=Find(fa[x]);
}
void join(int x,int y)
{
int fx=Find(x);
int fy=Find(y);
if(fx!=fy) fa[fx]=fy;
}
bool same(int x,int y)
{
return Find(x)==Find(y);
}
int main()
{
int cas=;
scanf("%d",&t);
while(t--)
{
int f=;
n=read(),m=read();
int ans=;
for(int i=;i<=*n;i++) fa[i]=i;
for(int i=;i<=m;i++)
{
scanf("%d%d",&x,&y);
if(x>n||y>n){f=;continue;}
if(same(x,y) || same(x+n,y+n))
f=;
else
{
join(x+n,y);
join(x,y+n);
}
}
printf("Scenario #%d:\n",cas++);
if(f) printf("Suspicious bugs found!\n"); //1 3
else printf("No suspicious bugs found!\n");
cout<<endl;
}
return ;
} /*
【题意】
略 【类型】
带权并查集 【分析】
首先考虑如何用并查集解决问题:我们可以设置数组[1,2n],其中[a]表示男,[a+n]表示女
从头扫描每一对关系,将a和b+n,a+n和b合并(两人异性才可以合并在一个圈子)
合并之前检查a和b,a+n和b+n是否在同一集合,如果是,则为同性恋 【时间复杂度&&优化】 【trick】 【数据】
*/
HDU 1829 A Bug's Life 【带权并查集/补集法/向量法】的更多相关文章
- POJ2492 A Bug's Life 带权并查集
分析:所谓带权并查集,就是比朴素的并查集多了一个数组,记录一些东西,例如到根的距离,或者和根的关系等 这个题,权数组为relation 代表的关系 1 和父节点不同性别,0,和父节点同性别 并查集一 ...
- HDU 5176 The Experience of Love 带权并查集
The Experience of Love Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/O ...
- POJ 2492 A Bug's Life 带权并查集
题意: 思路: mod2 意义下的带权并查集 如果两只虫子是异性恋,它们的距离应该是1. 如果两只虫子相恋且距离为零,则它们是同性恋. (出题人好猥琐啊) 注意: 不能输入一半就break出来.... ...
- POJ 1182 食物链 【带权并查集/补集法】
动物王国中有三类动物A,B,C,这三类动物的食物链构成了有趣的环形.A吃B, B吃C,C吃A. 现有N个动物,以1-N编号.每个动物都是A,B,C中的一种,但是我们并不知道它到底是哪一种.有人用两种说 ...
- hdu 1829 A Bug's Life(分组并查集(偏移量))
A Bug's Life Time Limit: 15000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Tot ...
- HDU 1829 A Bug's Life (种类并查集)
传送门: http://acm.hdu.edu.cn/showproblem.php?pid=1829 A Bug's Life Time Limit: 15000/5000 MS (Java/Oth ...
- Travel(HDU 5441 2015长春区域赛 带权并查集)
Travel Time Limit: 1500/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)Total Su ...
- poj2492A Bug's Life——带权并查集
题目:http://poj.org/problem?id=2492 所有元素加入同一个并查集中,通过其偏移量%2将其分类为同性与异性,据此判断事件. 代码如下: #include<iostrea ...
- POJ 2492 A Bug's Life (带权并查集 && 向量偏移)
题意 : 给你 n 只虫且性别只有公母, 接下来给出 m 个关系, 这 m 个关系中都是代表这两只虫能够交配, 就是默认异性, 问你在给出的关系中有没有与异性交配这一事实相反的, 即同性之间给出了交配 ...
随机推荐
- Linux系统查看系统信息
1. CPU # lscpu # cat /proc/cpuinfo //可以知道每个cpu信息,如每个CPU的型号,主频等 2. 内存 # free -m # cat /proc/meminfo / ...
- .net core Fundamentals
• Application Startup 應用程序啟動 • Middleware 中間件 • Working with Static Files 靜態文件 • Routing 路由 • URL Re ...
- 【51NOD-0】1008 N的阶乘 mod P
[算法]简单数学 [题解]多项式展开:(a*b)%p=(a%p*b%p)%p #include<cstdio> #include<algorithm> #define rep( ...
- Tunnel Warfare(HDU1540+线段树+区间合并)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1540 题目: 题意:总共有n个村庄,有q次操作,每次操作分为摧毁一座村庄,修复一座村庄,和查询与询问的 ...
- css纯样式导航
<style>.dropdown { position: relative; display: inline-block;} .dropdown-content { di ...
- 简述--构建React项目的几种方式
前言: 构建React项目的几种方式: 构建:create-react-app 快速脚手架 构建:generator-react-webpack 构建:webpack一步一步构建 1)构建:creat ...
- H5特性 MutationObserver 监听元素 动态改变iframe高度
这些代码要写在iframe页中执行 <script type="text/javascript"> $(function () { // Firefox和Chrome早 ...
- js 基本类型&引用类型
1.基本的数据类型有:undefined,boolean,number,string,null.基本类型的访问是按值访问的,就是说你可以操作保存在变量中的实际的值 基本类型的比较是值的比较.用==比较 ...
- Eureka服务续约(Renew)源码分析
主要对Eureka的Renew(服务续约),从服务提供者发起续约请求开始分析,通过阅读源码和画时序图的方式,展示Eureka服务续约的整个生命周期.服务续约主要是把服务续约的信息更新到自身的Eurek ...
- Development tools[重点]
Development tools yum groupinfo "Development tools" Loaded plugins: product-id, security, ...