A Bug's Life

Time Limit: 15000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)

Total Submission(s): 8528    Accepted Submission(s): 2745

Problem 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.

利用黑白染色,推断是否是二分图。

#include"stdio.h"
#include"string.h"
#include"queue"
using namespace std;
#define N 2005
#define M 1000005
int n,m,t,head[N];
int color[N],flag;
struct node
{
int u,v,next;
}map[2*M];
void add(int u,int v)
{
map[t].u=u;
map[t].v=v;
map[t].next=head[u];
head[u]=t++;
map[t].u=v;
map[t].v=u;
map[t].next=head[v];
head[v]=t++;
}
void find(int u)
{
int i,v;
for(i=head[u];i!=-1;i=map[i].next)
{
v=map[i].v;
if(color[v]==-1)
{
color[v]=color[u]^1;
find(v);
}
else if(color[v]==color[u])
{
flag=1;
return ;
}
}
return ;
}
int main()
{
int i,T,u,v,cnt=1;
scanf("%d",&T);
while(T--)
{
scanf("%d%d",&n,&m);
t=0;
memset(head,-1,sizeof(head));
while(m--)
{
scanf("%d%d",&u,&v);
add(u,v);
}
memset(color,-1,sizeof(color));
flag=0;
for(i=1;i<=n;i++)
{
if(color[i]==-1)
{
color[i]=0;
find(i);
if(flag)
break;
}
}
printf("Scenario #%d:\n",cnt++);
if(flag)
printf("Suspicious bugs found!\n");
else
printf("No suspicious bugs found!\n");
puts("");
}
return 0;
}

带权并查集:

#include"stdio.h"
#include"string.h"
#include"queue"
#include"vector"
#include"stack"
#include"algorithm"
using namespace std;
#define N 2005
#define min(a,b) (a<b?a:b)
int pre[N],gen[N];
int find(int k)
{
if(k==pre[k])
return k;
int t=find(pre[k]); //不能马上更新父节点与根节点同样
gen[k]=gen[k]^gen[pre[k]];//由于该节点的性别和它的父节点相反
return pre[k]=t; //确定该点性别之后才干把该点父节点更新为根节点
}
int Union(int x,int y)
{
int a,b;
a=find(x);
b=find(y);
if(a==b) //x,y的根节点同样
{
if(gen[x]==gen[y]) //推断他们是否同性别
return 1;
return 0;
}
pre[a]=b;
gen[a]=(gen[x]+gen[y]+1)&1;
return 0;
}
int main()
{
int i,u,v,n,cnt=1,T,m;
scanf("%d",&T);
while(T--)
{
scanf("%d%d",&n,&m);
for(i=0;i<=n;i++)
pre[i]=i;
memset(gen,0,sizeof(gen));
int flag=0;
while(m--)
{
scanf("%d%d",&u,&v);
if(flag)
continue;
flag=Union(u,v);
}
printf("Scenario #%d:\n",cnt++);
if(flag)
printf("Suspicious bugs found!\n");
else
printf("No suspicious bugs found!\n");
puts("");
}
return 0;
}

hdu 1829 &amp;poj 2492 A Bug&#39;s Life(推断二分图、带权并查集)的更多相关文章

  1. POJ 2912 Rochambeau(难,好题,枚举+带权并查集)

    下面的是从该网站上copy过来的,稍微改了一点,给出链接:http://hi.baidu.com/nondes/item/26dd0f1a02b1e0ef5f53b1c7 题意:有N个人玩剪刀石头布, ...

  2. A Bug's Life POJ - 2492 (带权并查集)

    A Bug's Life POJ - 2492 Background Professor Hopper is researching the sexual behavior of a rare spe ...

  3. HDU 1829 A Bug's Life 【带权并查集/补集法/向量法】

    Background Professor Hopper is researching the sexual behavior of a rare species of bugs. He assumes ...

  4. hdu 1829-A Bug's LIfe(简单带权并查集)

    题意:Bug有两种性别,异性之间才交往, 让你根据数据判断是否存在同性恋,输入有 t 组数据,每组数据给出bug数量n, 和关系数m, 以下m行给出相交往的一对Bug编号 a, b.只需要判断有没有, ...

  5. poj2492 A Bug's Life(带权并查集)

    题目链接 http://poj.org/problem?id=2492 题意 虫子有两种性别,有n只虫子,编号1~n,输入m组数据,每组数据包含a.b两只虫子,表示a.b为不同性别的虫子,根据输入的m ...

  6. 【poj 1984】&【bzoj 3362】Navigation Nightmare(图论--带权并查集)

    题意:平面上给出N个点,知道M个关于点X在点Y的正东/西/南/北方向的距离.问在刚给出一定关系之后其中2点的曼哈顿距离((x1,y1)与(x2,y2):l x1-x2 l+l y1-y2 l),未知则 ...

  7. 【poj 1988】Cube Stacking(图论--带权并查集)

    题意:有N个方块,M个操作{"C x":查询方块x上的方块数:"M x y":移动方块x所在的整个方块堆到方块y所在的整个方块堆之上}.输出相应的答案. 解法: ...

  8. POJ 1733 Parity game(离散化+带权并查集)

    离散化+带权并查集 题意:长度为n的0和1组成的字符串,然后问第L和R位置之间有奇数个1还是偶数个1. 根据这些回答, 判断第几个是错误(和之前有矛盾)的. 思路:此题同HDU 3038 差不多,询问 ...

  9. hdu 5441 Travel 离线带权并查集

    Travel Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=5441 De ...

随机推荐

  1. 【Android】 HttpClient 发送REST请求

    直接po代码吧,第一个是一个枚举类型的类,是四种rest http请求,get/post/put/delete: public enum HttpRequestMethod { HttpGet { @ ...

  2. [BZOJ5462][APIO2018]新家(线段树+堆)

    其实这个题第一反应一定是线段树分治,但是这样反而更难考虑了(实际上是可做的但很难想到),可见即使看上去最贴切的算法也未必能有效果. 考虑这个DS题,没有什么模型的转化,可能用到的无非就是线段树.平衡树 ...

  3. 【树链剖分】【线段树】bzoj3626 [LNOI2014]LCA

    引用题解: http://blog.csdn.net/popoqqq/article/details/38823457 题目大意: 给出一个n个节点的有根树(编号为0到n-1,根节点为0).一个点的深 ...

  4. Java高级架构师(一)第02节:分模块、分工程管理

    本节课程的目标在于:利用Maven构建分工程.分模块的空项目. -------- 基本的构建大致相同,有一个强调调点: 在总web的pom里边(architecture01web中),加入要合并的wa ...

  5. ueditor .net配置

    ASP.NET发展时期曾经诞生过 website  webapplication 两类程序. website 目前基本绝迹,就是曾经在App_Code目录中写代码,然后直接发布源码,动态编译的那种,基 ...

  6. c++之list学习

    #define _CRT_SECURE_NO_WARNINGS #include <iostream> #include <list> using namespace std; ...

  7. centos7 ping127.0.0.1不通

    ping 127.0.0.1,localhost和本地ip都不通,所有的配置也是正确的 检查下是否禁止了ping vim /proc/sys/net/ipv4/icmp_echo_ignore_all ...

  8. opengl中VAO,VBO,IBO用法小结(zz) 【转】

    http://cowboy.1988.blog.163.com/blog/static/751057982014380251300/ opengl中VAO,VBO,IBO用法小结 这三个玩意全面取代旧 ...

  9. JNI之——在cmd命令行下编译执行C/C++源文件

    转载请注明出处:http://blog.csdn.net/l1028386804/article/details/46604269 一直用java来敲代码,java配置好jre路径之后.在cmd下编译 ...

  10. Yii2系列教程二:MVC,Forms和Layouts

    上一篇文章我们简单地实现了Yii2框架安装和Hello World,而在这一篇文章当中,我们会带着好奇之心去探索一下在Yii2中的几个重要的元素组成:MVC,Forms和Layouts. 本文的目标是 ...