785. Is Graph Bipartite?
Given an undirected graph, return true if and only if it is bipartite.
Recall that a graph is bipartite if we can split it's set of nodes into two independent subsets A and B such that every edge in the graph has one node in A and another node in B.
The graph is given in the following form: graph[i] is a list of indexes j for which the edge between nodes i and j exists. Each node is an integer between 0 and graph.length - 1. There are no self edges or parallel edges: graph[i] does not contain i, and it doesn't contain any element twice.
Example 1:
Input: [[1,3], [0,2], [1,3], [0,2]]
Output: true
Explanation:
The graph looks like this:
0----1
| |
| |
3----2
We can divide the vertices into two groups: {0, 2} and {1, 3}.
Example 2:
Input: [[1,2,3], [0,2], [0,1,3], [0,2]]
Output: false
Explanation:
The graph looks like this:
0----1
| \ |
| \ |
3----2
We cannot find a way to divide the set of nodes into two independent subsets.
Note:
graphwill have length in range[1, 100].graph[i]will contain integers in range[0, graph.length - 1].graph[i]will not containior duplicate values.- The graph is undirected: if any element
jis ingraph[i], theniwill be ingraph[j].
class Solution {
public:
bool dfs(vector<vector<int> >& graph, vector<int>& state, int i, int color){
for (int j = ; j<graph[i].size(); j++){
if (state[graph[i][j]] == ){ //没有遍历到时
state[graph[i][j]] = -color; //标记该节点颜色同时继续搜索
return dfs(graph, state, graph[i][j], -color);
}
else if (state[graph[i][j]] == color){ //邻居节点中与该节点颜色相同则返回false
return false;
}
}
return true;
}
bool isBipartite(vector<vector<int>>& graph) {
int node_num = graph.size();
vector<int> state(node_num,);
int result = true;
for(int i=; i<graph.size(); i++){
if(state[i]== && !dfs(graph, state, i, ))
result = false;
}
return result;
}
};
785. Is Graph Bipartite?的更多相关文章
- 【LeetCode】785. Is Graph Bipartite? 解题报告(Python)
[LeetCode]785. Is Graph Bipartite? 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu. ...
- [leetcode]785. Is Graph Bipartite? [bai'pɑrtait] 判断二分图
Given an undirected graph, return true if and only if it is bipartite. Example 1: Input: [[1,3], [0, ...
- [LeetCode] 785. 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 ...
- 785. Is Graph Bipartite?从两个集合中取点构图
[抄题]: Given an undirected graph, return true if and only if it is bipartite. Recall that a graph is ...
- LeetCode 785. Is Graph Bipartite?
原题链接在这里:https://leetcode.com/problems/is-graph-bipartite/ 题目: Given an undirected graph, return true ...
- [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 ...
随机推荐
- C语言的谜题
本篇文章<C语言的谜题>展示了14个C语言的迷题以及答案,代码应该是足够清楚的,而且我也相信有相当的一些例子可能是我们日常工作可能会见得到的.通过这些迷题,希望你能更了解C语言.如果你不看 ...
- HTML和CSS基础知识
html基本结构<html>内容</html> html开始标记<head>内容</head> html文件头标记<title>内容< ...
- 基于Python在MacOS上安装robotframework-ride
基于Python在MacOS上安装robotframework-ride https://www.jb51.net/article/153665.htm https://www.jianshu.com ...
- weblogic.xml中的虚拟目录的配置
项目中的Ueditor富文本编辑器中上传图片后要能够预览. 如下图: 实现: 配置weblogic的虚拟目录:项目名称是test 如图: 这个表示:所有的访问/uefile/*的路径都会被转发到服务器 ...
- iOS:Masonry约束经验(19-03-21更)
1.label约束: 1).只需约束x.y 点相关就行.宽高 长度相关不用约束,就算用boundingRectWithSize计算出来的,也可能不准. 如:top.bottom二选一,trailing ...
- PyCharm 2018最新激活码通用
通用:Window.Mac.Ubantu都稳定有效,关键是这种激活方式不会产生其他影响 缺点:需要修改hosts文件 **1.修改hosts文件**将 0.0.0.0 account.jetbrain ...
- 【MySQL】基本语句
##mac启动mysql的命令语句## /usr/local/mysql/bin/mysql -u root -p ##然后输入密码 查看当前存在数据库 show databases; 创建数据库 c ...
- js基础(闭包实例)
1,常用发送短信的闭包实现: function sms() { var count = 60; return { start: function() { if(count == 0) { count ...
- 记录一下idea的破解方法
刚把idea升级到最新版,发现要重新激活,网上查了有改host的方法可行,只是有点麻烦.无意中发现一个方法,如图所示 输入 http://idea.java.sx/ 即可,亲测可用.如果资金 ...
- echarts动态加载数据无法更新series 无法更新图表
最近遇到一个Echarts图表无法动态更新数据的问题 最初我在option中设置series的值为一个数组,想着通过修改数组来动态更新图表,但是没变 化,后来发觉是因为图表数据会和之前的合并 看官方的 ...