POJ:2492-Bug's Life(二分图的判定)
Bug’s Life
Time Limit: 10000MS
Memory Limit: 65536K
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.
解题心得:
- 题意就是一个教授研究一种虫子的配对问题,给你每个虫子的配对情况,问你这些虫子是否存在同性配对。
- 其实就是一个二分图的判定,《算法竞赛,训练指南》里面有判定的源代码,这里说两种判定方法
- 第一种就是先将每个虫子的配对对象用邻接表存起来(存双向边),然后用dfs跑,给开头的那个虫子规定一个性别,然后依次跑下去,如果出现性别冲突,那么说明有同性配对的问题。
- 第二种就是使用并查集来做,存单向边,还是初始化第一只虫子的性别,然后开始找,找到的合并在一个并查集里面,但是如果找到两个虫子性别相同配对,这时候有两种情况,第一种是两个虫子在一个并查集里面,这样说明有同性配对的问题,第二种就是两个虫子不在一个并查集里面,这时候要将一个并查集的性别全部翻转,然后将两个并查集合并起来继续向下找。
- 一个坑点,每个例子之间要输出一个空行,输出就输出嘛,你把这个要求写在输入里面搞毛啊,比赛的时候PE。
dfs判定二分图
#include<stdio.h>
#include<vector>
#include<cstring>
using namespace std;
const int maxn = 2010;
vector <int> ve[maxn];
int n,m,sex[maxn],T=1,t;//sex[i]=0代表这个虫子还没有找过,sex[i]=1代表男性,sex[i]=2代表这个虫子是女性
void init()
{
scanf("%d%d",&n,&m);
memset(sex,0,sizeof(sex));
sex[0] = 2;//用于初始化第一个虫子的性别
for(int i=0;i<=n;i++)
ve[i].clear();
while(m--)
{
int a,b;
scanf("%d%d",&a,&b);
ve[a].push_back(b);
ve[b].push_back(a);
}
}
bool dfs(int x,int pre)
{
sex[x] = 3-sex[pre];
for(int i=0;i<ve[x].size();i++)
{
int v = ve[x][i];
if(sex[v] == sex[x])
return false;
if(!sex[v])
if(!dfs(v,x))
return false;
}
return true;
}
void checke()
{
printf("Scenario #%d:\n",T++);
bool flag = true;
for(int i=1;i<=n;i++)
if(!sex[i])
{
flag = dfs(i,0);
if(!flag)
{
printf("Suspicious bugs found!");
return ;
}
}
printf("No suspicious bugs found!");
}
int main()
{
scanf("%d",&t);
while(t--)
{
init();
checke();
if(t != 0)
printf("\n\n");//注意别被坑了
}
}
乱搞,写的并查集
/*比赛时候的智障代码,写得很乱,将就看吧*/
#include<stdio.h>
#include<iostream>
#include<queue>
#include<cstring>
#include<map>
#include<algorithm>
#include<stack>
typedef long long ll;
using namespace std;
const int maxn = 2010;
bool maps[maxn][maxn];
struct NODE
{
int a,b;
}node[maxn*maxn];
int father[maxn];
bool vis[maxn],flag;
int sex[maxn],n,m;
bool cmp(NODE A, NODE B)
{
return A.a < B.a;
}
void init()
{
flag = false;//用于标记是否发生同性冲突
memset(sex,0,sizeof(sex));
memset(vis,0,sizeof(vis));//用来标记这个虫子是否被找过
memset(maps,0,sizeof(maps));
for(int i=1;i<=n;i++)
father[i] = i;
}
int find(int x)
{
if(father[x] == x)
return x;
return father[x] = find(father[x]);
}
void merge(int a,int b)
{
int fa = find(a);
int fb = find(b);
if(fa != fb)
father[fa] = fb;
}
int main()
{
int t = 1;
int T;
scanf("%d",&T);
while(T--)
{
scanf("%d%d",&n,&m);
init();
flag = false;
int cnt = 0;
for(int i=0;i<m;i++)
{
int a,b;
scanf("%d%d",&a,&b);
if(a > b)
swap(a,b);
if(!maps[a][b])//检查重边,不知道有没有用,反正写了
{
maps[a][b] = maps[b][a] = true;
node[cnt].a = a;
node[cnt].b = b;
cnt++;
}
}
sort(node,node+cnt,cmp);
for(int i=0;i<cnt;i++)
{
int a = node[i].a;
int b = node[i].b;
if(!vis[a] && !vis[b])
{
vis[a] = vis[b] = true;
merge(a,b);
sex[a] = 0;
sex[b] = 1;
}
else if(!vis[a] && vis[b])
{
sex[a] = !sex[b];
vis[a] = true;
merge(a,b);
}
else if(!vis[b] && vis[a])
{
sex[b] = !sex[a];
vis[b] = true;
merge(a,b);
}
else if(vis[a] && vis[b])
{
if(sex[a] == sex[b])
{
if(find(a) == find(b))//同一并查集中性别冲突
flag = true;
else
{
for(int i=1;i<=n;i++)
if(find(i) == find(b))//不同并查集中性别冲突,就把其中一个中的性别全部冲突
sex[i] = !sex[i];
merge(a,b);
}
}
}
}
printf("Scenario #%d:\n",t++);
if(flag)
printf("Suspicious bugs found!");
else
printf("No suspicious bugs found!");
if(T!=0)
printf("\n\n");
}
return 0;
}
POJ:2492-Bug's Life(二分图的判定)的更多相关文章
- A Bug's Life POJ - 2492 (带权并查集)
A Bug's Life POJ - 2492 Background Professor Hopper is researching the sexual behavior of a rare spe ...
- A Bug's Life POJ 2492
D - A Bug's Life 二分图 并查集 BackgroundProfessor Hopper is researching the sexual behavior of a rare spe ...
- POJ 2492 并查集扩展(判断同性恋问题)
G - A Bug's Life Time Limit:10000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u S ...
- POJ 2492 并查集应用的扩展
A Bug's Life Time Limit: 10000MS Memory Limit: 65536K Total Submissions: 28651 Accepted: 9331 Descri ...
- 二分图的判定hihocoder1121 and hdu3478
这两个题目都是二分图的判定,用dfs染色比较容易写. 算法流程: 选取一个没有染色的点,然后将这个点染色,那么跟他相连的所有点一定是不同颜色的,所以,如果存在已经染过颜色的,如果和这个颜色相同的话,就 ...
- hdu_2444The Accomodation of Students(二分图的判定和计算)
hdu_2444The Accomodation of Students(二分图的判定和计算) 标签:二分图匹配 题目链接 题意: 问学生是否能分成两部分,每一部分的人都不相认识,如果能分成的话,两两 ...
- 双栈排序(洛谷P1155)二分图的判定+思维贪心
题目:戳这里 题目大意: 给你一个数列,问能否通过两个栈的push与pop把它输出成一个升序序列(每个数只能入队并出队一次) 不能的话输出0,能的话输出操作方法 主要思路: 1.判断是否可以成功输出升 ...
- 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 ...
随机推荐
- linux下使用shell脚本批处理命令
1.新建脚本touch first.sh 2.写入命令vi first.sh #!/bin/bash #publish service and api echo "copy file&quo ...
- 12.Visual Studio 2013中的默认快捷键
这篇大致是IDE的使用技巧,常用的也就那么几个. 我自己用的最多的是注释.取消注释.格式调整.运行测试.开始调试.断开调试.重新开始调试.删除行ctrl+L.保存.全部保存.打开资源管理器.搜索等几个 ...
- AJPFX总结java创建线程的三种方式及其对比
Java中创建线程主要有三种方式: 一.继承Thread类创建线程类 (1)定义Thread类的子类,并重写该类的run方法,该run方法的方法体就代表了线程要完成的任务.因此把run()方法称为执行 ...
- 金三银四面试季节之Java 核心面试技术点 - JVM 小结
原文:https://github.com/linsheng9731/notebook/blob/master/java/JVM.md 描述一下 JVM 的内存区域 程序计数器(PC,Program ...
- Servlet的生命周期以及线程安全问题
一:Servlet生命周期图,以及注意事项 二:代码演示 LifeCycleServlet.java package cn.woo.servlet; import java.io.IOExceptio ...
- JavaWeb前端笔记
day06 回顾: bootstrap: css框架,html/css/js集于一身,ie 6/7/8兼容有问题 开发响应式页面,使用于不同的上网设备 使用步骤: 1.导入bootstrap.css ...
- TFS无法确定工作区解决方案
当TFS遇到无法确定工作区时,删除掉SUO和VSSSCC文件,当然也有可能不是这两个,反正是SLN文件以外的两个文件,毕竟是昨天做的事,有点忘记了.所以,做掉文件之前,要做好备份O(∩_∩)O~
- PHP代码规范的一些总结
世界第一语言在手,辅以前人的最佳实践,天下又算什么. 1.代码是写给小白用的 注释,注释,注释,重要的事情说三遍.我们做的虽然不是拿去卖源码的商业产品,不需要把注释写的多么优美.但也不要太过吝啬,到头 ...
- 初学python,感受和C的不同
从开始看Python到现在也有半个多月了,前后看了Python核心编程和Dive into Python两本书.话说半个月看两本,是个人都知道有多囫囵吞枣,这也是因为我暂时没有需求拿这个做大型开发,主 ...
- SSH中懒加载异常--could not initialize proxy - no Session
SSH进行关联的表进行显示时出现的问题,老是显示你的OGNL表达式错误,但是找了很久确实没错,在网上找了一下,下面的这个方法本人认为是最有效的方法(已经测试可以使用) 在web.xml中加入 程序代码 ...