886. Possible Bipartition

Given a set of N people (numbered 1, 2, ..., N), we would like to split everyone into two groups of any size.

Each person may dislike some other people, and they should not go into the same group. 

Formally, if dislikes[i] = [a, b], it means it is not allowed to put the people numbered a and b into the same group.

Return true if and only if it is possible to split everyone into two groups in this way.

 

Example 1:

Input: N = 4, dislikes = [[1,2],[1,3],[2,4]]
Output: true
Explanation: group1 [1,4], group2 [2,3]

Example 2:

Input: N = 3, dislikes = [[1,2],[1,3],[2,3]]
Output: false

Example 3:

Input: N = 5, dislikes = [[1,2],[2,3],[3,4],[4,5],[1,5]]
Output: false

 

Constraints:

  • 1 <= N <= 2000
  • 0 <= dislikes.length <= 10000
  • dislikes[i].length == 2
  • 1 <= dislikes[i][j] <= N
  • dislikes[i][0] < dislikes[i][1]
  • There does not exist i != j for which dislikes[i] == dislikes[j].

886. Possible Bipartition
struct Solution;

impl Solution {
    fn possible_bipartition(n: i32, dislikes: Vec<Vec<i32>>) -> bool {
        let n = n as usize;
        let mut graph: Vec<Vec<usize>> = vec![vec![]; n];
        for dislike in dislikes {
            let u = dislike[0] as usize - 1;
            let v = dislike[1] as usize - 1;
            graph[u].push(v);
            graph[v].push(u);
        }
        let mut colors: Vec<i32> = vec![0; n];
        for i in 0..n {
            if colors[i] == 0 && !Self::dfs(i, 1, &mut colors, &graph, n) {
                return false;
            }
        }
        true
    }

    fn dfs(u: usize, color: i32, colors: &mut [i32], graph: &[Vec<usize>], n: usize) -> bool {
        colors[u] = color;
        for &v in &graph[u] {
            if colors[v] == color {
                return false;
            }
            if colors[v] == 0 && !Self::dfs(v, -color, colors, graph, n) {
                return false;
            }
        }
        true
    }
}

#[test]
fn test() {
    let n = 4;
    let dislikes = vec_vec_i32![[1, 2], [1, 3], [2, 4]];
    let res = true;
    assert_eq!(Solution::possible_bipartition(n, dislikes), res);
    let n = 3;
    let dislikes = vec_vec_i32![[1, 2], [1, 3], [2, 3]];
    let res = false;
    assert_eq!(Solution::possible_bipartition(n, dislikes), res);
    let n = 5;
    let dislikes = vec_vec_i32![[1, 2], [2, 3], [3, 4], [4, 5], [1, 5]];
    let res = false;
    assert_eq!(Solution::possible_bipartition(n, dislikes), res);
}