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:

  • graph will have length in range [1, 100].
  • graph[i] will contain integers in range [0, graph.length - 1].
  • graph[i] will not contain i or duplicate values.
  • The graph is undirected: if any element j is in graph[i], then i will be in graph[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?的更多相关文章

  1. 【LeetCode】785. Is Graph Bipartite? 解题报告(Python)

    [LeetCode]785. Is Graph Bipartite? 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu. ...

  2. [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, ...

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

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

  4. [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 ...

  5. 785. Is Graph Bipartite?从两个集合中取点构图

    [抄题]: Given an undirected graph, return true if and only if it is bipartite. Recall that a graph is  ...

  6. LeetCode 785. Is Graph Bipartite?

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

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

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

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

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

  9. LeetCode - Is Graph Bipartite?

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

随机推荐

  1. backtype.storm.generated.InvalidTopologyException:null问题的解决

    程序启动报错:backtype.storm.generated.InvalidTopologyException:null 问题解决方法: 这个错误一般都是没有定义输出列造成的 检查Spout和Bol ...

  2. 【noip模拟赛 王强的疑惑】 题解

    考试题. 是个DP. 50分可以通过子集枚举+线段覆盖(贪心)完成. 考试没时间写了一个子集枚举30分. #include <cstdio> #include <cstring> ...

  3. 将硬件规定的通信协议用Lua实现(涉及到很多Lua通信的数据转换)

    1:这次处理的是大唐的gps通信协议,先简单介绍一下他规定的通信规则: 信息结构: 传输说明: 信息结构中的各个字节书写时都是以十六进制标识,两位数组成.传输时,SOI和EOI(SOI=7EH,EOI ...

  4. Tarjan算法初探(2):缩点

    接上一节 Tarjan算法初探(1):Tarjan如何求有向图的强连通分量 Tarjan算法一个非常重要的应用就是 在一张题目性质在点上性质能够合并的普通有向图中将整个强连通分量视作一个点来把整张图变 ...

  5. 『ACM C++』 PTA 天梯赛练习集L1 | 025-026

    满课一天,做25的时候还疯狂WA,进度可以说是很慢了 哭泣 ------------------------------------------------L1-025---------------- ...

  6. ajax与jsonp定义及使用方法

    ajax 定义 ajax技术的目的是让javascript发送http请求,与后台通信,获取数据和信息. ajax通信的过程不会影响后续javascript的执行,从而实现异步. 同步和异步 现实生活 ...

  7. php之微型博客的创建

    一,微型博客的开发思路 微型博客的创建,确定无疑我们会用到PHP和mysql之间的增添删改查,首先来看一下思维导图: 搭建好计算机里的apache php 和mysql的联动功能,打开phpmyadm ...

  8. TCP/IP协议的数据传输过程详解——IP与以太网的包收发操作

    MTU:一个网络包的最大长度,以太网中一般是1500字节:(含有头部长度,包括IP头部,TCP头部,不包括MAC头部) MSS:除去头部后,一个网络包所能容纳的TCP的数据的最大长度 下图为TCP/I ...

  9. Python3 break与continue

    Infi-chu: http://www.cnblogs.com/Infi-chu/ break和continue都是中断循环的意思,但是他们的中断后的效果不同. 请看如下两个例子就懂了 ''' 这个 ...

  10. 20155207王雪纯 《Java程序设计》实验一报告

    20155207王雪纯 <Java程序设计>实验一报告 课程:Java程序设计 班级:1552 指导教师:娄嘉鹏 实验日期:2017.04.07 实验名称:Java开发环境的熟悉(Linu ...