886. Possible Bipartition

Question:

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

Note:

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

Solution:

Imgur

  • Method 1: dfs + bopartite
    • we use the dfs to color the graph.
    • Current node is 1, and we need to color their neighbours to -1, and keep coloring.
    • If one node’s color is filled and different from current color, this question cannot be solved.
       class Solution {
        private Map<Integer, List<Integer>> graph;
        private int[] visited;
        public boolean possibleBipartition(int N, int[][] dislikes) {
            if(N == 0 || dislikes.length == 0) return true;
            graph = new HashMap<>();
            for(int[] dislike : dislikes){
                List<Integer> temp = graph.containsKey(dislike[0]) ? graph.get(dislike[0]): new ArrayList<>();
                temp.add(dislike[1]);
                graph.put(dislike[0], temp);
                temp = graph.containsKey(dislike[1]) ? graph.get(dislike[1]): new ArrayList<>();
                temp.add(dislike[0]);
                graph.put(dislike[1], temp);
            }
            visited = new int[N + 1];
            for(int i = 0; i < N; i++){
                if(visited[i] == 0){
                    if(!dfs(i, 1)) return false;
                }
            }
            return true;
        }
        private boolean dfs(int node, int color){
            if(visited[node] != 0 && visited[node] != color) return false;
            if(visited[node] == color) return true;
            visited[node] = color;
            List<Integer> neighbours = graph.get(node);
            if(neighbours == null) return true;
            for(Integer neighbour : neighbours){
                if(!dfs(neighbour, -color)) return false;
            }
            return true;
        }
        }
      
  • Method 2: bfs
    • For all initialized node, we set it to 1 and add to queue.
    • for all neighbours, we check their neighbours color, if any one of them equals current one, return false.
    • Otherwise, we set it to -currentColor and save it to the queue.
    • Finally return true;
        class Solution {
        public boolean possibleBipartition(int N, int[][] dislikes) {
            Map<Integer, List<Integer>> graph = new HashMap<>();
            for(int[] dislike : dislikes){
                List<Integer> temp = graph.containsKey(dislike[0]) ? graph.get(dislike[0]): new ArrayList<>();
                temp.add(dislike[1]);
                graph.put(dislike[0], temp);
                temp = graph.containsKey(dislike[1]) ? graph.get(dislike[1]): new ArrayList<>();
                temp.add(dislike[0]);
                graph.put(dislike[1], temp);
            }
            Queue<Integer> queue = new LinkedList<>();
            int[] visited = new int[N + 1];
            for(int i = 0; i < N; i++){
                if(visited[i] == 0){
                    visited[i] = 1;
                    queue.offer(i);
                    while(!queue.isEmpty()){
                        int cur = queue.poll();
                        List<Integer> temp = graph.get(cur);
                        if(temp == null) continue;
                        for(Integer next : temp){
                            if(visited[next] == visited[cur]) return false;
                            if(visited[next] == 0){
                                visited[next] = -visited[cur];
                                queue.offer(next);
                            }
                        }
                    }
                }
            }
            return true;
        }
        }
      

Reference

  1. 花花酱 LeetCode 886. Possible Bipartition