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 先谈一下二分图相关: 一个图是二分图的充分必要条件: 该图对应无向图的所有回路必定是偶环(构成该 ...
随机推荐
- 脚手架工具搭建VUE应用
首先需要安装node.js,然后安装CLI工具. vue init webpack vue-lesson2 使用element组件的话,需要用到如下命令: cd vue-lesson2 vue add ...
- [USACO15JAN]Grass Cownoisseur
\(tarjan\)缩点+\(DAG\)上最长路. 求一个以\(1\)为起点的最长路和一个以\(1\)为终点的最长路,然后找那个逆行边就行了. 然后这个我\(RE\)了好久,原因是\(vector\) ...
- FTP配置
常用的匿名FTP配置项 anonymous_enable=YES 是否允许匿名用户访问 anon_umask=022 匿名用户所上传文件的权限 ...
- 爬虫实战(二) 用Python爬取网易云歌单
最近,博主喜欢上了听歌,但是又苦于找不到好音乐,于是就打算到网易云的歌单中逛逛 本着 "用技术改变生活" 的想法,于是便想着写一个爬虫爬取网易云的歌单,并按播放量自动进行排序 这篇 ...
- ubuntu14.04 fcitx安装
先卸载ibus sudo apt-get remove ibus (也可尝试不卸载ibus,直接安装fcitx) 添加源 sudo add-apt-repository ppa:fcitx-team/ ...
- ubuntu root用户登陆
sudo vi /etc/lightdm/lightdm.conf (如果没有该文件则创建,内容如下) [SeatDefaults] user-session=ubuntu greeter-ses ...
- 利用ajax全局设置实现拦截器
var token = localStorage.getItem("token"); $.ajaxSetup({ dataType: "json", cache ...
- 04 Python基础数据类型
目录: 1) 整型 2) 为什么使用16进制以及用在哪里 3) 浮点型 4) 字符串型 5) 布尔型 6) 查看数据类型 7) 复数型 8) input() 9) print() 10) % 格式化输 ...
- SQL学习笔记:一些高级语句
现在以MySQL为模板.学习的方法和别的数据库写法上会有不同,但是思路基本一致. 用到的数据库表的格式: +----+--------------+-------------------------- ...
- JS权威指南笔记1
1.JavaScript数据类型可分为两种:原始类型和对象类型.原始类型下又包括数字.字符串和布尔值,以及null和undefined这两个特殊的:对象是属性的集合,且每个属性都有自己的"名 ...