POJ2492 A Bug's Life 判断二分图
给一个无向图,判断是否为二分图 是否有奇环。用染色法解决,也可以用并查集解决,时间复杂度是相同的,但是并查集用的内存少。
这里给出Bfs染色的代码
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cmath>
#include<cstring>
#include<vector>
#include<queue>
using namespace std;
const int maxn=2000+1,maxm=1e+6;
int color[maxn];
vector<int>adj[maxn]; bool isbipart(int s,vector<int>connect[])
{
color[s]=1;
queue<int>q;
q.push(s);
while(!q.empty())
{
int now=q.front();q.pop();
for(int i=0;i<connect[now].size();i++)
{
int next=connect[now][i];
if(color[next]==0)
{
if(color[now]==1)color[next]=2;
else color[next]=1;
q.push(next);
continue;
}
if(color[next]==color[now])return false;
}
}
return true;
}
int main()
{freopen("t.txt","r",stdin);
int T;scanf("%d",&T);
int tt=T;
while(T--)
{
int n,m;
scanf("%d%d",&n,&m);
for(int i=1;i<=n;i++)
{color[i]=0;adj[i].clear();}
int a,b;
for(int i=0;i<m;i++)
{
scanf("%d%d",&a,&b);
adj[a].push_back(b);
adj[b].push_back(a);
}
bool ans=true;
for(int i=1;i<=n&&ans;i++)
{
if(color[i]==0)ans=isbipart(i,adj);
}
if(ans)printf("Scenario #%d:\nNo suspicious bugs found!\n",tt-T);
else printf("Scenario #%d:\nSuspicious bugs found!\n",tt-T);
if(T>0)printf("\n");
}
return 0;
}
POJ2492 A Bug's Life 判断二分图的更多相关文章
- HDU 4751 Divide Groups (2013南京网络赛1004题,判断二分图)
Divide Groups Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Tot ...
- Java实现 LeetCode 785 判断二分图(分析题)
785. 判断二分图 给定一个无向图graph,当这个图为二分图时返回true. 如果我们能将一个图的节点集合分割成两个独立的子集A和B,并使图中的每一条边的两个节点一个来自A集合,一个来自B集合,我 ...
- (hdu)2444 The Accomodation of Students 判断二分图+最大匹配数
题目链接:http://acm.split.hdu.edu.cn/showproblem.php?pid=2444 Problem Description There are a group of s ...
- The Accomodation of Students(判断二分图以及求二分图最大匹配)
The Accomodation of Students Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d &a ...
- 二分图学习——基础dfs判断二分图
#include <iostream> #include <cstdio> #include <string.h> #include <vector> ...
- The Accomodation of Students HDU - 2444(判断二分图 + 二分匹配)
The Accomodation of Students Time Limit: 5000/1000 MS (Java/Others) Memory Limit: 32768/32768 K ( ...
- poj2492 A Bug's Life(带权并查集)
题目链接 http://poj.org/problem?id=2492 题意 虫子有两种性别,有n只虫子,编号1~n,输入m组数据,每组数据包含a.b两只虫子,表示a.b为不同性别的虫子,根据输入的m ...
- hdu 2444 The Accomodation of Students 判断二分图+二分匹配
The Accomodation of Students Time Limit: 5000/1000 MS (Java/Others) Memory Limit: 32768/32768 K ( ...
- 【交叉染色法判断二分图】Claw Decomposition UVA - 11396
题目链接:https://cn.vjudge.net/contest/209473#problem/C 先谈一下二分图相关: 一个图是二分图的充分必要条件: 该图对应无向图的所有回路必定是偶环(构成该 ...
随机推荐
- [转]Js获取当前日期时间及其它操作
转载自:http://www.cnblogs.com/carekee/articles/1678041.html Js获取当前日期时间及其它操作 var myDate = new Date();myD ...
- 13Microsoft SQL Server SQL 高级事务,锁,游标,分区
Microsoft SQL Server SQL高级事务,锁,游标,分区 通过采用事务和锁机制,解决了数据库系统的并发性问题. 9.1数据库事务 (1)BEGIN TRANSACTION语句定义事务的 ...
- 【Redis】二、Redis高级特性
(三) Redis高级特性 前面我们介绍了Redis的五种基本的数据类型,灵活运用这五种数据类型是使用Redis的基础,除此之外,Redis还有一些特性,掌握这些特性能对Redis有进一步的了解, ...
- Spring事物不回滚
今天发现个自己的bug,仔细排查后,发现根本原因我在service方法中抛出的异常被控制层的方法捕获了,所以后台页面也只是出现个错误提示,而数据却没有回滚. 解决方式:对自己抛出的异常使用try ca ...
- 当ECharts碰到TWaver
百度公司的ECharts发展迅速,已经成为HTML5 Chart的佼佼者,这让大家骄傲:中国人终于也有世界级的开源通用UI产品了.正如其网站所说,它是百度的,是中国的,也是世界的.想想那些年,我们追逐 ...
- vue父子通信的基本使用
项目中没怎么用过父子通信,很多页面都是路由切换实现的,后来做首页的时候发现首页的路径没法配置,我强行在原先的首页上写了个子组件,通过判断路径使其强行跳转实现的 这个时候跳转页面的时候就要使用到了父子间 ...
- CCF201612-2 工资计算 java(100分)
试题编号: 201612-2 试题名称: 工资计算 时间限制: 1.0s 内存限制: 256.0MB 问题描述: 问题描述 小明的公司每个月给小明发工资,而小明拿到的工资为交完个人所得税之后的工资.假 ...
- 【codeforces 3C】Tic-tac-toe
[链接] 我是链接,点我呀:) [题意] 题意 [题解] 写一个函数判断当前局面是否有人赢. 然后枚举上一个人的棋子下在哪个地方. 然后把他撤回 看看撤回前是不是没人赢然后没撤回之前是不是有人赢了. ...
- 使用Mybatis进行连表查询、left join---https://blog.csdn.net/jinzhencs/article/details/51980518
使用Mybatis进行连表查询.left join https://blog.csdn.net/jinzhencs/article/details/51980518
- [luoguP2854] [USACO06DEC]牛的过山车Cow Roller Coaster(DP + sort)
传送门 先按照起点 sort 一遍. 这样每一个点的只由前面的点决定. f[i][j] 表示终点为 i,花费 j 的最优解 状态转移就是一个01背包. ——代码 #include <cstdio ...