题目连接:https://leetcode-cn.com/problems/is-graph-bipartite/
public class Solution785 {
public boolean isBipartite(int[][] graph) {
int[] color = new int[graph.length];
for(int i=0; i<graph.length; ++i){
if(color[i]==0){
color[i]=1;
if(!dfs(graph,color,i))
return false;
}
}
return true;
}
public boolean dfs(int[][] graph, int[] color, int x){
for(int i:graph[x]){
if(color[i]==color[x])
return false;
else if(color[i]==0){
color[i]=-color[x];
if(!dfs(graph,color,i))
return false;
}
}
return true;
}
}