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 ...
随机推荐
- 深度学习方法:受限玻尔兹曼机RBM(二)网络模型
欢迎转载,转载请注明:本文出自Bin的专栏blog.csdn.net/xbinworld. 技术交流QQ群:433250724,欢迎对算法.技术.应用感兴趣的同学加入 上解上一篇RBM(一)基本概念, ...
- 微信小程序获取用户信息“授权失败”场景的处理
很多的时候我们在处理小程序功能的时候需要用户获取用户信息,但是呢为了信息安全,用户不授权导致授权失败场景:但是小程序第二次不在启动授权信息弹层,为了用户体验,可以用以下方式处理: function i ...
- 解决错误:此用户名包含无效字符,请输入有效的用户名。wordpress不能注册中文用户名的问题
wordpress在默认情况下不支持中文用户名,就是在后台添加用户的时候,如果用户名包含中文,则显示”错误:此用户名包含无效字符,请输入有效的用户名.”如何解决这个问题呢? 不用插件的话就需要修改一个 ...
- JAVA用POI读取和创建2003和2007版本Excel
1.添加maven依赖 <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi-o ...
- Java学习笔记(三)——静态导入,package-info,Fall-through
[前面的话] 算是真正的放松了好几天时间,没有看任何书,没有任何任务,今天是过完年后的第一天上班时间,我又开始了我的学习之路,感觉还没有老,怎么心态越来越平静了,进入工作状态,就好好努力工作,新的一年 ...
- CentOS7.5右键创建空白文档
首先我们进入centos7桌面 在桌面上右键“打开终端” 在终端我们使用cd命令进入用户目录下的模板文件夹cd ~/模板 ---->中文版cd ~/Templates ---->中 ...
- 转:json注入
现在大部分web采用ajax通信,数据表现为json格式,因此可以尝试进行json注入. json注入:根据实际情况进行注入.有的时候,可能是为了方便,有人会手动拼接下JSON,但是这种随手代码,却可 ...
- 转:Spring学习笔记---Spring Security登录页
转:http://axuebin.com/blog/2016/06/21/spring-security/?utm_source=tuicool&utm_medium=referral. 提示 ...
- 【转载】Banner框架
原文地址:https://github.com/youth5201314/banner 以前banner都自己写,又丑问题又多,在github上找到一个点赞最多的,动画效果那是绚丽多彩啊,好东东当然要 ...
- 36、Flask实战第36天:客户端权限验证
编辑cms_base.html <li><a href="#">{{ g.cms_user.username }}<span>[超级管理员]&l ...