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. Loj_6282. 数列分块入门 6

    Loj_6282 这个题目涉及到了块的重构,这里使用了\(\sqrt{n}\)次插入便重构的方法 讲重复的操作提出来做了函数 #include <iostream> #include &l ...

  2. 软工之词频统计器及基于sketch在大数据下的词频统计设计

    目录 摘要 算法关键 红黑树 稳定排序 代码框架 .h文件: .cpp文件 频率统计器的实现 接口设计与实现 接口设计 核心功能词频统计器流程 效果 单元测试 性能分析 性能分析图 问题发现 解决方案 ...

  3. 安装及使用supervisor

    用途有一个进程需要每时每刻不断的跑,但是这个进程又有可能由于各种原因有可能中断.当进程中断的时候,希望能自动重新启动它.此时,我就需要使用到了Supervisor. 前言supervisor管理的进程 ...

  4. jquery checkbox点选反选

    <script type="text/javascript"> $(function(){ //点选反选 $("#check_all").click ...

  5. Tarjan算法初探(3):求割点与桥以及双连通分量

    接上一节Tarjan算法初探(2):缩点 在此首先提出几个概念: 割点集合:一个无向连通图G 若删除它的一个点集 以及点集中所有点相连的边(任意一端在点集中)后 G中有点之间不再连通则称这个点集是它的 ...

  6. 什么是设计模式?【php】

    原文地址:https://www.cnblogs.com/zhuiluoyu/p/5818974.html 什么是设计模式? 设计模式(Design Pattern)是一套被反复使用.多数人知晓的.经 ...

  7. Linux基础-1.Linux命令及获取帮助

    1.Linux命令的格式 1)了解Linux命令的语法格式: 命令 [选项] [参数] 2)掌握命令格式中命令.选项.参数的具体的含义 a)命令:告诉Linux(UNIX)操作系统做(执行)什么 b) ...

  8. 初识hadoop之分布式文件系统(HDFS)

    Hadoop常用发行版: Apache Hadoop CDH  Cloudera Distributed Hadoop HDP  Hortonworks Data Platfrom 分布式文件系统(H ...

  9. 怎样在windows上定时执行python脚本

    作为一个需要在电脑上工作和学习的人,一件十分困扰我的事情就是怎样不受互联网中其他内容的干扰而专注于自己想要做的事情,有的时候真的是沉浸于微博上的消息,忘了自己本来想要做的事.不过我有一件神器,自己爱豆 ...

  10. PTA(BasicLevel)-1007素数对猜想

    一 问题描述-素数对 让我们定义素数差d​n​​为:d​n​​=p​n+1​​−p​n​​,其中p​i​​是第i个素数.显然有d​1​​=1,且对于n>1有d​n​​是偶数.“素数对猜想”认为“ ...