D - A Bug's Life 二分图 并查集

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

两种方法

1:二分图的判定 ,如果说其构成的图为二分图   那么没有问题,否则 有问题

#include<iostream>
#include<cstdio>
#include<vector>
#include<queue>
#include<cstring>
using namespace std;
const int N= +;
vector<int >ve[N];
int mark[N];
bool flag=false ;
bool bfs(int x){
queue<int >que;
que.push(x);
mark[x]=;
while(que.size()){
int x1=que.front();
que.pop();
for(int i=;i<ve[x1].size();i++){
int dx=ve[x1][i];
if(mark[x1]==){
if(mark[dx]==) return true;
else if(mark[dx]==){
mark[dx]=;
que.push(dx);
}
}
else {
if(mark[dx]==) return true;
else if(mark[dx]==){
mark[dx]=;
que.push(dx);
}
}
}
}
return false;
}
int main(){
int t,k=;
cin>>t;
while(t--){
flag =false ;
int n,m;
k++;
scanf("%d%d",&n,&m);
memset(mark,,sizeof(mark));
for(int i=;i<=n;i++){
ve[i].clear();
}
for(int i=;i<=m;i++){
int x,y;
scanf("%d%d",&x,&y);
ve[x].push_back(y);
ve[y].push_back(x);
}
for(int i=;i<=n;i++){
if(mark[i]==){
if(bfs(i)){
flag=true;
break;
}
}
}
printf("Scenario #%d:\n",k);
if(flag) puts("Suspicious bugs found!");
else puts("No suspicious bugs found!");
puts("");
}
return ;
}

2并查集

链接”:::https://www.cnblogs.com/geloutingyu/p/6117262.html

A Bug's Life POJ 2492的更多相关文章

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

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

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

    链接: http://poj.org/problem?id=2492 http://acm.hust.edu.cn/vjudge/contest/view.action?cid=82830#probl ...

  3. A Bug’s Life POJ - 2492(种类并查集)

    题目链接 每次给出两个昆虫的关系(异性关系),然后发现这些条件中是否有悖论 就比如说第一组数据 1 2 2 3 1 3 1和2是异性,2和3是异性,然后说1和3是异性就显然不对了. 我们同样可以思考一 ...

  4. A Bug's Life POJ - 2492 (种类或带权并查集)

    这个题目的写法有很多,用二分图染色也可以写,思路很好想,这里我们用关于并查集的两种写法来做. 题目大意:输入x,y表示x和y交配,然后判断是否有同性恋. 1 带权并查集: 我们可以用边的权值来表示一种 ...

  5. POJ 2492 并查集扩展(判断同性恋问题)

    G - A Bug's Life Time Limit:10000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u S ...

  6. POJ 2492 并查集应用的扩展

    A Bug's Life Time Limit: 10000MS Memory Limit: 65536K Total Submissions: 28651 Accepted: 9331 Descri ...

  7. 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条臭 ...

  8. POJ 2492 A Bug's Life(并查集)

    http://poj.org/problem?id=2492 题意 :就是给你n条虫子,m对关系,每一对关系的双方都是异性的,让你找出有没有是同性恋的. 思路 :这个题跟POJ1703其实差不多,也是 ...

  9. (简单) POJ 2492 A Bug's Life,二分染色。

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

随机推荐

  1. hdu1175 连连看(bfs疯狂MLE和T,遂考虑dfs+剪枝)

    题目链接:http://icpc.njust.edu.cn/Problem/Hdu/1175/ 题目大意就是给出地图,上面有若干的数,相当于连连看,给了q个查询,问给出的两个位置的数能否在两次转弯以内 ...

  2. python环境变量忘记配置

    Python安装没有勾选配置环境变量安装 解决方法一: 于是,便用安装包卸载后重新安装. 重新安装勾选 安装成功 方法二: 配置环境变量 A.右键点击“我的电脑”,点击“属性”: B.在弹出的界面中点 ...

  3. 【转载】因为我们是OIer

    我们是OIer, 所以我们 不用在跑道上挥汗如雨: 不用在球场上健步如飞: 更不用在没事的时候, 经受非人的体能训练-- 但是, 我们却要把头脑 高速运转, 还要接受一大堆 大学生也只是 " ...

  4. Javascript之网页版待办事项

    本文使用原生JS实现站点 http://www.todolist.cn/ 的基本功能. 其中页面的HTML布局和CSS样式取用原站,JS部分为自己编写. 效果图 完整代码 HTML.JS部分 < ...

  5. 终极解决方案之——Centos7由于误删或更新python导致 No module named yum

    之前由于不懂yum和python之间的关系,因为一直在学python3,看到系统里/usr/lib下的python2我就直接删了,结果... 可能还有人是因为python升级的原因,即系统自带的pyt ...

  6. Centos 8 安装 Consul-Template

    1. 下载安装包( consul-template_0.23.0_linux_amd64.zip 文件 ) 下载地址: https://releases.hashicorp.com/consul-te ...

  7. mybatis 入门基础

    一.Mybatis介绍 MyBatis是一款一流的支持自定义SQL.存储过程和高级映射的持久化框架.MyBatis几乎消除了所有的JDBC代码,也基本不需要手工去设置参数和获取检索结果.MyBatis ...

  8. xmind转为markdown

    先将xmind导出为.opml 将opml导入Typora (需要安装pandoc)

  9. echarts图表x,y轴的设置

    https://www.cnblogs.com/cjh-strive/p/11065005.html xAxis属性代表echarts图表的x轴设置代码如下 xAxis : [ { type : 'c ...

  10. Linux基础:Day05

    iptables ip 的 tables ip的表格: iptables只是netfilter的前端管理工具:netfilter是linux内核提供的数据流量管理模块: iptables/netfil ...