LeetCode -- 785. 判断二分图

 深度优先遍历判断二分图

c ++ 
class Solution {

const static int N = 110, M = N * N;
int h[N], e[M], ne[M], idx;
int st[N];

void add(int a, int b) {
    e[idx] = b, ne[idx] = h[a], h[a] = idx ++ ;
}

public:
    bool isBipartite(vector<vector<int>>& graph) {
        int n = graph.size();
        memset(h, -1, sizeof h);
        
        for(int i = 0; i < n; i ++ ) {
            for(auto it : graph[i]) {
                add(i, it);
            }
        }

        function<bool(int, int)> dfs = [&](int u, int col) -> bool {
            st[u] = col;
            for(int i = h[u]; ~i; i = ne[i]) {
                int j = e[i];
                if(!st[j]) {
                    if(!dfs(j, 3 - col)) return false;
                } else if(st[j] == col) return false;
            }
            return true;
        };

        for(int i = 0; i <= n; i ++ ) {
            if(!st[i]) {
                if(!dfs(i, 1)) return false;
            }
        }
        return true;
    }
};
java
class Solution {

    int N = 110, M = N * N;

    int[] h = new int[N], e = new int[M], ne = new int[M];
    int idx;
    int[] color = new int[N];

    void add(int a, int b) {
        e[idx] = b;
        ne[idx] = h[a];
        h[a] = idx ++ ;
    }

    boolean dfs(int u, int c) {
        color[u] = c;
        for(int i = h[u]; i != -1; i = ne[i]) {
            int j = e[i];
            if(color[j] == 0) {
                if(!dfs(j, 3 - c)) return false;
            } else if(color[j] == c) return false;
        }
        return true;
    }

    public boolean isBipartite(int[][] graph) {
        Arrays.fill(h, -1);
        for(int i = 0; i < graph.length; i ++ ) {
            for(int j = 0; j < graph[i].length; j ++ ) {
                add(i, graph[i][j]);
            }
        }

        for(int i = 0; i < graph.length; i ++ ) {
            if(color[i] == 0) {
                if(!dfs(i, 1)) return false;
            }
        }
        return true;
    }
}
python
class Solution:
    def isBipartite(self, graph: List[List[int]]) -> bool:
        n = len(graph)
        color = [0] * n
        flag = True

        def dfs(u: int, c: int) -> bool:
            global flag
            color[u] = c
            for it in graph[u]:
                if color[it] == 0:
                    if not dfs(it, 3 - c):
                        return False
                elif color[it] == c:
                    return False
            return True
        
        for i in range(n):
            if color[i] == 0:
                if not dfs(i, 1):
                    return False

        return True
                
golang
var color []int

func dfs(u int, c int, graph [][]int) bool {
    color[u] = c
    for _, it := range graph[u] {
        if color[it] == 0 {
            if !dfs(it, 3 - c, graph) {
                return false
            }
        } else if(color[it] == c) {
            return false
        }
    }
    return true
}

func isBipartite(graph [][]int) bool {
    color = make([]int, 110)
    n := len(graph)
    for i := 0; i < n; i ++ {
        fmt.Println(color[i])
        if color[i] == 0 {
            if !dfs(i, 1, graph) {
                return false
            }
        }
    }
    return true 
}
js
/**
 * @param {number[][]} graph
 * @return {boolean}
 */
var isBipartite = function(graph) {
    let len = graph.length;
    let color = new Array(110).fill(0);

    let dfs = function (u, c) {
        color[u] = c;
        for(let i = 0; i < graph[u].length; i ++ ) {
            let j = graph[u][i];
            if(color[j] === 0) {
                if(!dfs(j, 3 - c)) return false;
            } else if(color[j] === c) return false;
        }
        return true;
    }

    for(let i = 0; i < len; i ++ ) {
        if(color[i] === 0) {
            if(!dfs(i, 1)) return false;
        }
    }
    return true;
};

 

posted @ 2023-07-06 17:24  深渊之巅  阅读(7)  评论(0编辑  收藏  举报