给一个无向图,判断是否为二分图 是否有奇环。用染色法解决,也可以用并查集解决,时间复杂度是相同的,但是并查集用的内存少。

这里给出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 判断二分图的更多相关文章

  1. HDU 4751 Divide Groups (2013南京网络赛1004题,判断二分图)

    Divide Groups Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Tot ...

  2. Java实现 LeetCode 785 判断二分图(分析题)

    785. 判断二分图 给定一个无向图graph,当这个图为二分图时返回true. 如果我们能将一个图的节点集合分割成两个独立的子集A和B,并使图中的每一条边的两个节点一个来自A集合,一个来自B集合,我 ...

  3. (hdu)2444 The Accomodation of Students 判断二分图+最大匹配数

    题目链接:http://acm.split.hdu.edu.cn/showproblem.php?pid=2444 Problem Description There are a group of s ...

  4. The Accomodation of Students(判断二分图以及求二分图最大匹配)

    The Accomodation of Students Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d &a ...

  5. 二分图学习——基础dfs判断二分图

    #include <iostream> #include <cstdio> #include <string.h> #include <vector> ...

  6. The Accomodation of Students HDU - 2444(判断二分图 + 二分匹配)

    The Accomodation of Students Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K ( ...

  7. poj2492 A Bug's Life(带权并查集)

    题目链接 http://poj.org/problem?id=2492 题意 虫子有两种性别,有n只虫子,编号1~n,输入m组数据,每组数据包含a.b两只虫子,表示a.b为不同性别的虫子,根据输入的m ...

  8. hdu 2444 The Accomodation of Students 判断二分图+二分匹配

    The Accomodation of Students Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K ( ...

  9. 【交叉染色法判断二分图】Claw Decomposition UVA - 11396

    题目链接:https://cn.vjudge.net/contest/209473#problem/C 先谈一下二分图相关: 一个图是二分图的充分必要条件: 该图对应无向图的所有回路必定是偶环(构成该 ...

随机推荐

  1. HTML `capture` 属性

    file 类型的 <input> 除了调起系统的文件选择框外,还可通过指定 capture 属性来现场拍照或录制.配合 accept 属性,可实现更加便捷的文件获取. 比如想要录制一段视频 ...

  2. 模板—splay

    #include<iostream> #include<cstdio> #define cin(x) scanf("%d",&x) using na ...

  3. 学习笔记——网络编程3(基于TCP协议的网络编程)

    TCP协议基础 IP协议是Internet上使用的一个关键协议,它的全称是Internet Protocol,即Internet协议,通常简称IP协议.   使用ServerSocket创建TCP服务 ...

  4. NOIP2016 DAY1 T2天天爱跑步

    传送门 题目描述 小c同学认为跑步非常有趣,于是决定制作一款叫做<天天爱跑步>的游戏.«天天爱跑步»是一个养成类游戏,需要玩家每天按时上线,完成打卡任务. 这个游戏的地图可以看作一一棵包含 ...

  5. Python使用Flask框架,结合Highchart处理jsonl数据

    1.html代码 <!DOCTYPE html> <html lang="en"> <head> <meta charset=" ...

  6. CSC

    CSC CSC Table of Contents 1. account 2. Contacts 3. <国家公派留学人员预订回国机票说明> 4. 回国手续 4.1. 申办及开具<留 ...

  7. Keil uVision “已停止工作”

    之前一直受这个问题的困扰,但是因为只是下载程序,下载镜像文件完事就算了.随便keil挂掉. 今天要调试程序,发现开启调试keil就挂掉了,烦. 解决办法参见: http://218.244.144.1 ...

  8. A - Restaurant

    UVA 1468 Description   Mr. Kim is planning to open a new restaurant. His city is laid out as a grid ...

  9. redis学习——系统管理

    Redis系统管理 实验简介 上一节实验讲述了Redis的基本数据类型,本实验继续讲解Redis相关命令及管理操作. 在Redis中,命令大小写不敏感. 一.适合全体类型的常用命令 启动redis服务 ...

  10. HDU 1540 区间合并线段树

    题目大意: 就是给定一堆位置,进行删除还原,最后找到 t 位置上的最大连续位置 #include <cstdio> #include <cstring> #include &l ...