LeetCode 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 its 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 doesnt contain any element twice.

判断二分图,二分图染色的基本做法,DFS加染色

 1 class Solution {
 2 public:
 3     int c=1;
 4     int color[110]={0};
 5     bool isBipartite(vector<vector<int>>& graph) {
 6         for(int i=0; i<graph.size(); i++){
 7             if(color[i]==0){
 8                 if(!DFS(i,c,graph)){
 9                     return false;
10                 }
11             }
12         }
13         return true;
14     }
15     bool DFS(int v,int c,vector<vector<int>>& graph){
16         color[v]=c;
17         for(int i=0; i<graph[v].size(); i++){
18             if(color[graph[v][i]]==c)
19                 return false;
20             if(color[graph[v][i]]==0&&!DFS(graph[v][i],-c,graph))
21                 return false;
22         }
23         return true;
24     }
25 };

相关文章

自1998年我国取消了福利分房的政策后,房地产市场迅速开展蓬...
文章目录获取数据查看数据结构获取数据下载数据可以直接通过...
网上商城系统MySql数据库设计
26个来源的气象数据获取代码
在进入21世纪以来,中国电信业告别了20世纪最后阶段的高速发...