检查一个图是否是二分图的算法

使用的是宽度搜索:

1 初始化一个颜色记录数组

2 利用queue宽度遍历图

3 从随意源点出发。染色0。 或1

4 遍历这点的邻接点。假设没有染色就染色与这个源点相反的颜色,假设已经染色而且和源点的值相反。那么就是合法点,假设是同样的颜色。那么就不能是二分图

 

參考:http://www.geeksforgeeks.org/bipartite-graph/

#include <stdio.h>
#include <iostream>
#include <queue>
using namespace std; class CheckwhetheragivengraphisBipartiteornot
{
const static int V = 4;
bool isBipartite(int G[][V], int src)
{
int colors[V];
fill(colors, colors+V, -1); colors[src] = 1; queue<int> qu;
qu.push(src);
while (qu.size())
{
int u = qu.front();
qu.pop(); for (int v = 0; v < V; v++)
{
if (G[u][v] && colors[v] == -1)
{
colors[v] = 1 - colors[u];
qu.push(v);
}
else if (G[u][v] && colors[v] == colors[u]) return false;
}
}
return true;
}
public:
CheckwhetheragivengraphisBipartiteornot()
{
int G[][V] =
{
{0, 1, 0, 1},
{1, 0, 1, 0},
{0, 1, 0, 1},
{1, 0, 1, 0}
}; isBipartite(G, 0) ? cout << "Yes" : cout << "No";
}
};

Geeks - Check whether a given graph is Bipartite or not 二分图检查的更多相关文章

  1. dataStructure@ Check whether a given graph is Bipartite or not

    Check whether a given graph is Bipartite or not A Bipartite Graph is a graph whose vertices can be d ...

  2. dataStructure@ Check if a directed graph has cycles

    #include<iostream> #include<cstdio> #include<cstring> #include<limits> #incl ...

  3. Codeforces Round #550 (Div. 3) F. Graph Without Long Directed Paths (二分图染色)

    题意:有\(n\)个点和\(m\)条无向边,现在让你给你这\(m\)条边赋方向,但是要满足任意一条边的路径都不能大于\(1\),问是否有满足条件的构造方向,如果有,输出一个二进制串,表示所给的边的方向 ...

  4. LeetCode 785. Is Graph Bipartite?

    原题链接在这里:https://leetcode.com/problems/is-graph-bipartite/ 题目: Given an undirected graph, return true ...

  5. Is Graph Bipartite?

    Given an undirected graph, return true if and only if it is bipartite. Recall that a graph is bipart ...

  6. [LeetCode] Is Graph Bipartite? 是二分图么?

    Given an undirected graph, return true if and only if it is bipartite. Recall that a graph is bipart ...

  7. [Swift]LeetCode785. 判断二分图 | Is Graph Bipartite?

    Given an undirected graph, return true if and only if it is bipartite. Recall that a graph is bipart ...

  8. LeetCode - Is Graph Bipartite?

    Given an undirected graph, return true if and only if it is bipartite. Recall that a graph is bipart ...

  9. [LeetCode] 785. Is Graph Bipartite?_Medium tag: DFS, BFS

    Given an undirected graph, return true if and only if it is bipartite. Recall that a graph is bipart ...

随机推荐

  1. MySQL中的内连接、外连接、交叉连接

    内连接(INNER JOIN):   分为三种 等值连接.自然连接.不等连接        外连接(OUTER JOIN):   左外连接(LEFT OUTER JOIN或LEFT JOIN)   右 ...

  2. sql server 完整备份、差异备份、事务日志备份

    一. 理解: 完整备份为基础, 完整备份可以实物回滚还原,但是由于完整备份文件过大,对硬盘空间比较浪费这是就需要差异备份 或者 事务日志备份. 差异备份还原时,只能还原到备份的那个点, 日志备份还原时 ...

  3. nginx 开启 gzip

    gzip on; gzip_min_length 1k; gzip_buffers 4 16k; gzip_comp_level 2; gzip_types text/plain applicatio ...

  4. LintCode 13. Implement strStr()

    LintCode 13. Implement strStr() 题目描述 对于一个给定的 source 字符串和一个 target 字符串,你应该在 source 字符串中找出 target 字符串出 ...

  5. Java并发编程实战笔记

    如果当多个线程访问同一个可变的状态变量时没有使用合适的同步,那么程序就会出现错误.有三种方式可以修复这个问题: i.不在线程之间共享该状态变量 ii.将状态变量修改为不可变的变量 iii.在访问状态变 ...

  6. 静态call 动态call LINK

    COBOL的调用可以是静态调用(Static Call),这时,被调用的子程序必须与调用程序一起链接(link-edited)起来形成一个完整的装载模块(Load module),但子程序依然可以单独 ...

  7. Android apk去广告

    韩梦飞沙 yue31313 韩亚飞 han_meng_fei_sha  313134555@qq.com 下载地址: [北方网通]    [电信网通] [下载说明] 1 点击上面的地址,打开下载页面 ...

  8. [Codeforces 1053B] Vasya and Good Sequences

    Link: Codeforces 1053B 传送门 Solution: 其实就是暴力 观察需要满足的条件: 1.个数和为偶数 2.最大个数不大于其它所有个数的和 如果只有第一个条件记录前缀和的奇偶性 ...

  9. 【并查集】星球大战starwar

    BZOJ1015: [JSOI2008]星球大战starwar Time Limit: 3 Sec  Memory Limit: 162 MBSubmit: 6407  Solved: 2973[Su ...

  10. python3-开发进阶补充Django中的文件的上传

    PS:这段时间有点不在状态,刚刚找回那个状态,那么我们继续曾经的梦想 今天我们来补充一下文件的上传的几种方式: 首先我们先补充的一个知识点: 一.请求头ContentType: ContentType ...