LeetCode 785. 判断二分图 (图论基础之判断二分图)

判断二分图

class Solution {
public:
    int vis[110] = {0},n,flag = 1;
    bool isBipartite(vector<vector<int>>& graph) {
        n = graph.size();
        for(int i=0;i<n;i++){
            if(!flag)
                return false;
            if(!vis[i])    
                dfs(i,1,graph);
        }
        return true;    
    }
    void dfs(int x,int color,vector<vector<int>>& graph){
        if(!flag){
            return;
        }
        vis[x] = color;
        for(int y:graph[x]){
            if(!vis[y]){
                dfs(y,3-color,graph);
            }else if(vis[y]==color){
                flag = 0;
                return ;
            }
        }
    }
};

你可能感兴趣的:(LeetCode,#,LC图论)