Geeks - Check whether a given graph is Bipartite or not 二分图检查
检查一个图是否是二分图的算法
使用的是宽度搜索:
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 二分图检查的更多相关文章
- 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 ...
- dataStructure@ Check if a directed graph has cycles
#include<iostream> #include<cstdio> #include<cstring> #include<limits> #incl ...
- Codeforces Round #550 (Div. 3) F. Graph Without Long Directed Paths (二分图染色)
题意:有\(n\)个点和\(m\)条无向边,现在让你给你这\(m\)条边赋方向,但是要满足任意一条边的路径都不能大于\(1\),问是否有满足条件的构造方向,如果有,输出一个二进制串,表示所给的边的方向 ...
- LeetCode 785. Is Graph Bipartite?
原题链接在这里:https://leetcode.com/problems/is-graph-bipartite/ 题目: Given an undirected graph, return true ...
- Is Graph Bipartite?
Given an undirected graph, return true if and only if it is bipartite. Recall that a graph is bipart ...
- [LeetCode] Is Graph Bipartite? 是二分图么?
Given an undirected graph, return true if and only if it is bipartite. Recall that a graph is bipart ...
- [Swift]LeetCode785. 判断二分图 | Is Graph Bipartite?
Given an undirected graph, return true if and only if it is bipartite. Recall that a graph is bipart ...
- LeetCode - Is Graph Bipartite?
Given an undirected graph, return true if and only if it is bipartite. Recall that a graph is bipart ...
- [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 ...
随机推荐
- WordPress固定链接设置的几种方法(推荐/%post_id%.html)
传说中,固定链接有SEO功能,今天试了试,现在给大家分享一下: wordpress固定链接设置技巧: 1.不要让日期出现在固定链接里面 这基于两个方面的考虑.一是如果数字出现在固定链接里面,等于提醒搜 ...
- Windows 10 安装 Mongodb
因为新换了Windows 10 电脑,需要在新电脑重新安装所有的软件,包括mongodb 下载文件:首先在mongodb的官方网站上下载最新版本的mongodb安装程序,https://www.mon ...
- Linux下使进程在后台运行
怎么样使程序在后台执行 /////////////////// nohup ./nn > nn.log 2 > &1 & //////////// 方法有很多, ...
- STL模板整理 全排列
概念: 从n个不同元素中任取m(m≤n)个元素,按照一定的顺序排列起来,叫做从n个不同元素中取出m个元素的一个排列.当m=n时所有的排列情况叫全排列.如果这组数有n个,那么全排列数为n!个. 比如a, ...
- Visual Studio找不到adb.exe错误解决
Visual Studio找不到adb.exe错误解决 错误信息:Cannot find adb.exe in specified SDK path.出现这种情况,是因为没有安装Android SDK ...
- nyoj 151 Biorhythms
描述 Some people believe that there are three cycles in a person's life that start the day he or she i ...
- 【BZOJ 2186】 2186: [Sdoi2008]沙拉公主的困惑 (欧拉筛,线性求逆元)
2186: [Sdoi2008]沙拉公主的困惑 Description 大富翁国因为通货膨胀,以及假钞泛滥,政府决定推出一项新的政策:现有钞票编号范围为1到N的阶乘,但是,政府只发行编号与M!互质的钞 ...
- 【UOJ #201】【CTSC 2016】单调上升路径
http://uoj.ac/problem/201 别人都一眼秒的题对我而言怎么那么难qwq 这道题就是要构造一个n*n的邻接矩阵,满足矩阵\(A\)是一个拉丁方阵(也是数独?),\(a_{ij}=a ...
- 【主席树】【最近公共祖先】hdu6162 Ch’s gift
题意:一棵树,每个点有个权值,m次询问,每次给你一条链和两个值a,b,问你这条链上权值在[a,b]之间的权值的和是多少. std竟然是2个log的……完全没必要链剖,每个结点的主席树从其父节点转移过 ...
- 【带权并查集】【离散化】vijos P1112 小胖的奇偶
每个区间拆成r和l-1两个端点,若之内有偶数个1,则这两个端点对应的前缀的奇偶性必须相同,否则必须相反. 于是可以用带权并查集维护,每个结点储存其与其父节点的奇偶性是否相同,并且在路径压缩以及Unio ...