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. rhel install java jdk

    Linux安装JDK完整步骤 1.检查一下系统中的jdk版本 [root@localhost software]# java -version 显示: openjdk version "1. ...

  2. PAT——1054. 求平均值

    本题的基本要求非常简单:给定N个实数,计算它们的平均值.但复杂的是有些输入数据可能是非法的.一个“合法”的输入是[-1000,1000]区间内的实数,并且最多精确到小数点后2位.当你计算平均值的时候, ...

  3. HDU 1069 Monkey and Banana(转换成LIS,做法很值得学习)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1069 Monkey and Banana Time Limit: 2000/1000 MS (Java ...

  4. 鼠标不能用怎么办 USB OPTICAL MOUSE

    刚买的新鼠标,一般鼠标插上去自动安装驱动,然后就可以正常使用了. 如果遇到下面这种情况:"usb optical mouse 找不到驱动程序" 插上以后死活都没作用,然后开始下载一 ...

  5. ArrayList两个对象之间的赋值

    List<String> list1 = new ArrayList<String>(); List<String> list2 = new ArrayList&l ...

  6. Xcode12 libstdc-.6.0.9.tbd问题

    https://github.com/Kila2/libstdc-.6.0.9.tbd # libstdc-.6.0.9.tbd libstdc++.6.0.9.tbd # for device pu ...

  7. JAVA中基本类型和字符串之间的转换

    一.基本类型转换成字符串有三种方法: int c = 10; 1.使用包转类的toString()方法 String str1 = Integer.toString(c); 2.使用String类的v ...

  8. 在TextBrowser显示中,如何让最新的数据永远出现在第一行或者是在窗口的最后显示信息

    这是第一行,但是随着数据的增多,最新的数据就会在末尾显示,然后就看不到了.可以用 main_ui->ReceiveDatatextBrowser->insertPlainText(strD ...

  9. 【2017 ICPC亚洲区域赛北京站 J】Pangu and Stones(区间dp)

    In Chinese mythology, Pangu is the first living being and the creator of the sky and the earth. He w ...

  10. macOS,安装+配置+激活:MySQL8.0 + Navicat Premium12

    作者的电脑是10.13.3,些许配置偏差请自行略过 本文是学习探讨途径,请勿滥用,后果自负 MySQL8.0 篇章 官网http://www.mysql.com/downloads/ 下载即可,无需激 ...