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 先谈一下二分图相关: 一个图是二分图的充分必要条件: 该图对应无向图的所有回路必定是偶环(构成该 ...
随机推荐
- BZOJ1996: [Hnoi2010]chorus 合唱队 (DP)
就是想水一发 #include <stdio.h> #include <algorithm> #include <iostream> using namespace ...
- jenkins自动部署测试环境
构建脚本如下: echo "当前目录":$(pwd)echo "当前时间":$(date +%Y-%m-%d_%H:%M)find ./ -type f -na ...
- wpf 界面加载 Command
导入 xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity" <i:Interaction. ...
- 新一代 Linux 文件系统 btrfs 简介
https://www.ibm.com/developerworks/cn/linux/l-cn-btrfs/ Btrfs 简介 文件系统似乎是内核中比较稳定的部分,多年来,人们一直使用 ext2/3 ...
- Python isalpha() 方法检测字符串是否只由字母组成。
Python isalpha() 方法检测字符串是否只由字母组成.
- 用TWaver加载大型游戏场景一例
游戏中经常会出现一些大型的户外场景,例如一个小镇.一座古城等.通常这种场景中包含了较多的建筑.道路.桥梁等等元素,其3D模型比较大且复杂.在使用TWaver加载时,可使用一些技巧,让加载速度更快.显示 ...
- xmpp聊天室(5)
聊天室 //初始化聊天室 XMPPJID *roomJID = [XMPPJID jidWithString:ROOM_JID]; xmppRoom = [[XMPPRoom alloc] initW ...
- MyBatis 中传递多个参数的 4 种方式
方式 1 :封装成对象入参 #{对应实体类的属性} //UserMapper.java 接口 /** * 多条件查询:根据用户名称(模糊查询)和用户角色查询用户列表(参数:对象入参) * @para ...
- buf.writeUInt32BE()
buf.writeUInt32BE(value, offset[, noAssert]) buf.writeUInt32LE(value, offset[, noAssert]) value {Num ...
- C语言之自定义__DATE__与__TIME__
/******************************************************************* * > File Name: 05-ymd.c * &g ...