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 先谈一下二分图相关: 一个图是二分图的充分必要条件: 该图对应无向图的所有回路必定是偶环(构成该 ...
随机推荐
- Linux---文件目录管理
1. Linux文件目录架构 Linux的目录结构与win的目录有很大不同,首先,没有盘符的概念:然后Linux使用斜杠/标识目录,Linux首先建立一个根目录,然后将其他文件系统挂载到这个目录下. ...
- MySQL练习题及答案(复习)
新建一个叫做 review 的数据库,将测试数据脚本导进去.(可以使用Navicat查询功能) /* Navicat MySQL Data Transfer Source Server : DB So ...
- linux-vmstat-显示虚拟内存状态
推荐:更多linux 性能监测与优化 关注:linux命令大全 vmstat命令的含义为显示虚拟内存状态(“Viryual Memor Statics”),但是它可以报告关于进程.内存.I/O等系统整 ...
- CCF201609-2 火车购票 java(100分)
试题编号: 201609-2 试题名称: 火车购票 时间限制: 1.0s 内存限制: 256.0MB 问题描述: 问题描述 请实现一个铁路购票系统的简单座位分配算法,来处理一节车厢的座位分配. 假设一 ...
- 第十一节:Web爬虫之数据存储(数据更新、删除、查询)
接着上一节的内容 5.MySQL数据更新 结果是将id=3的name进行更新操作,结果如下: 6.MySQL数据去重及更新 结果是判断数据是否有重复的,如果有重复的将不再存储,若没有重复的就更新数据进 ...
- 聊聊餐饮: 2016年,是我做生意9年来,最差的1年 by某老板
晚上忙完事,在小区里点了个菜. 今年在这个小店点菜,基本没有等过. 比较好奇,就问了下老板,最近怎么没人. 经常在这个店吃饭,老板就和我多聊了几句. 2016年,是我做生意9年来,最差的1年.还好 ...
- redis 指定端口 启动
由于资源紧张 需要多创建一个redis实例 重新copy一份实例,然后修改redis.conf文件, 找到port 6379 处修改 端口号 6380 通过下面命令启动就好了 src/redis-se ...
- noip模拟赛 蒜头君的排序
分析:其实就是求m个区间的逆序对个数,题目真的是明摆着让我们用莫队算法,套用树状数组就可以了. 具体怎么转移呢?如果移动R,那么对区间[l,r]有影响的是R左边的元素,我们只需要看有多少在R左边比a[ ...
- hdu 1867 kmp匹配
#include<stdio.h> #include<string.h> #define N 100100 void getnext(int next[],char s[]) ...
- centos7 mysql安装与用户设置
1.环境:Centos 7.0 64位2.mysql版本:5.73.安装:https://dev.mysql.com/doc/refman/5.7/en/installing.html3.1.创建my ...