A Bug's Life POJ 2492
D - 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.
Input
Output
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的更多相关文章
- 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
链接: http://poj.org/problem?id=2492 http://acm.hust.edu.cn/vjudge/contest/view.action?cid=82830#probl ...
- A Bug’s Life POJ - 2492(种类并查集)
题目链接 每次给出两个昆虫的关系(异性关系),然后发现这些条件中是否有悖论 就比如说第一组数据 1 2 2 3 1 3 1和2是异性,2和3是异性,然后说1和3是异性就显然不对了. 我们同样可以思考一 ...
- A Bug's Life POJ - 2492 (种类或带权并查集)
这个题目的写法有很多,用二分图染色也可以写,思路很好想,这里我们用关于并查集的两种写法来做. 题目大意:输入x,y表示x和y交配,然后判断是否有同性恋. 1 带权并查集: 我们可以用边的权值来表示一种 ...
- 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 ...
- 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条臭 ...
- POJ 2492 A Bug's Life(并查集)
http://poj.org/problem?id=2492 题意 :就是给你n条虫子,m对关系,每一对关系的双方都是异性的,让你找出有没有是同性恋的. 思路 :这个题跟POJ1703其实差不多,也是 ...
- (简单) POJ 2492 A Bug's Life,二分染色。
Description Background Professor Hopper is researching the sexual behavior of a rare species of bugs ...
随机推荐
- 四则运算APP版
(一)四则运算APP版 这这个Demo的目的就是强化一下本周学习的Android的Jetpack里的新内容,接下来我将通过这个Demo来展示我所学到的新知识. 先列出新学到的知识:ViewModel, ...
- codeforces 1236 A. Bad Ugly Numbers
A. Bad Ugly Numbers time limit per test 1 second memory limit per test 256 megabytes input standard ...
- MySQL优化之慢查询日志
慢查询日志概述 所谓慢查询日志,就是用于记录MySQL中响应时间超过设定阈值的SQL语句,通过打开慢查询开关,MySQL会将大于阈值的SQL记录在日志中,以便于分析性能. 慢查询日志选项默认是关闭的, ...
- linux bash吧,还有啥Bourne Again Shell
linux bash吧,还有啥Bourne Again Shell bash吧,还有啥Bourne Again Shell 头部要写#!/bin/bash set -x #open script de ...
- TCP、UDP服务器模型 在网络程序里面,通常都是一
TCP.UDP服务器模型 在网络程序里面,通常都是一个服务器处理多个客户机,为了出个多个客户机的请求,服务器端的程序有不同的处理方式. 目前最常用的服务器模型: 循环服务器:循环服务器在同一时刻只能响 ...
- matplotlib命令与格式:参数配置文件与参数配置
转自 https://my.oschina.net/swuly302/blog/94805 自定义matplotlib Created Saturday 08 December 2012 5.1 ma ...
- python3.4连接mysql数据库的方法
python3.4连接mysql数据库的方法 发布时间:2014-08-04编辑:www.jbxue.com 本文介绍了python3.4连接mysql数据库的方法,在python3.4中不能用mys ...
- TensorFlow系列专题(五):BP算法原理
欢迎大家关注我们的网站和系列教程:http://www.tensorflownews.com/ ,学习更多的机器学习.深度学习的知识! 一.反向传播算法简介 二.前馈计算的过程 第一层隐藏层的计算 第 ...
- (C++ Error: Incompatible types in assignment of ‘char*’ to ‘char [2])
in C++, cannot assign a pointer to an array. c++中char*与char[]不是一种类型,但是在C里面可以,所以尽量使用C++中的类,如string, v ...
- jq日历一周为单位轮播
简单效果图: 代码如下: <!doctype html> <html lang="en"> <head> <meta charset=&q ...