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. JavaScript(9)--- 跨域

    JavaScript(9)--- 跨域 一.跨域原理(同源策略) 在项目搭建的初期,因为现在项目基本上都是前后端分离,所以不可避免地会遇到跨域问题,而造成跨域的罪魁祸首就是浏览器的同源策略.所以要解决 ...

  2. Javascript之封装运动函数

    @ 目录 阶段一.仅适用单位带px属性的匀速运动 阶段二.可适用单位不带px属性(如opacity)的匀速运动 阶段三.适用于多元素单一属性的匀速运动 阶段四.适用于多元素单一属性的匀速或缓冲运动 阶 ...

  3. Git 处理换行符的配置方法

    core.autocrlf If you're programming on Windows and working with people who are not (or vice-versa), ...

  4. 利用sqlmap进行Access和Mysql注入

    sqlmap将检测结果保存到C:\Users\Administrator.sqlmap\output (windows) linux:(/root/.sqlmap/output) Access注入 1 ...

  5. C 2010年笔试题

    1 有一个函数, 写一段程序,输入的值,输出的值. #include <stdio.h> void main() { int x,y; printf("输入x:"); ...

  6. Linux - Ubuntu18.04下更改apt源为阿里云源

    进入apt目录,备份原来的源地址 cd /etc/apt mv ./source.list ./source.list.bak 修改源文件source.list vim source.list 更换阿 ...

  7. 201771010108韩腊梅《面向对象程序设计(java)》第一周学习总结

    第一部分:课程准备部分 填写课程学习 平台注册账号, 平台名称 注册账号 博客园:www.cnblogs.com 木子林 程序设计评测:https://pintia.cn/ 19362039991@q ...

  8. ELK数据批量导入

                                                                            数据批量导入 • 使用 _bulk 批量导入数据 – 批 ...

  9. Hadoop安装教程_伪分布式

    文章更新于:2020-04-09 注1:hadoop 的安装及单机配置参见:Hadoop安装教程_单机(含Java.ssh安装配置) 注2:hadoop 的完全分布式配置参见:Hadoop安装教程_分 ...

  10. es实现mysql的like查询

    es版本6.8 因为阿里云的dts同步最高支持es版本就是6.8 构建索引 PUT /z_test/ { "mappings": { "doc": { &quo ...