886. Possible Bipartition

886. Possible Bipartition

https://www.youtube.com/watch?v=VlZiMD7Iby4

https://leetcode.com/problems/possible-bipartition/discuss/159085/java-graph


class Solution {
    public boolean possibleBipartition(int N, int[][] dislikes) {
        // build graph , key : current, value : a list of nei
        // color them 
        int[] visited = new int[N + 1];
        // 0 : not visited, 1: red. 2 : black
        
        //int[][] graph = new int[N + 1][N + 1];
        List<List<Integer>> graph = new ArrayList<>();
        // for(int i = 0; i <= N; i++){
        //     graph.get(i).add(new ArrayList<Integer>());
        // }
        for(int i = 0; i <= N; i++){
            graph.add(new ArrayList<>());
        }
        // fill int the graph with infor from dislikes
        for(int[] pair : dislikes){
            int cur = pair[0];
            int nei = pair[1];
            graph.get(cur).add(nei);
            graph.get(nei).add(cur);
        }
        
        for(int i = 1; i <= N; i++){
            if(visited[i] == 0 && !bfs(graph, i, visited)){
                return false;
            }
        }
        return true;
    }
    private boolean bfs(List<List<Integer>> graph, int i, int[] visited){
        Queue<Integer> queue = new LinkedList<>();
        queue.offer(i);
        visited[i] = 1;
        while(!queue.isEmpty()){
            int current = queue.poll();
            for(int nei : graph.get(current)){
                if(visited[nei] == 0){
                    visited[nei] = visited[current] == 1 ? 2 : 1;
                    queue.offer(nei); // don't forget to offer to the queue 
                }else{
                    if(visited[nei] == visited[current]){
                        return false;
                    }
                }
            }
        }
        return true;
        
    }
}

// dislikes ,means they are neighbors, so use the disklikes to construct the graph, 
// when use List<List<Integer>> as the graph, fill in the empty list first 
// fill in 0,1, 2, .. N times , 
// the list size is N + 1, because, we skip index 0, so its easy to rference it when we want
// to get the neighbors 

 

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

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

 

posted on 2018-08-28 20:47  猪猪&#128055;  阅读(284)  评论(0)    收藏  举报

导航