【Leetcode】886. Possible Bipartition

题目地址:

https://leetcode.com/problems/possible-bipartition/

给定一个无向图的一系列边,要求每个边的端点都不能涂上相同颜色,问是否存在涂色方法。题目保证不存在平行边和环。

本质上是判断图是否是二分图。

法1:DFS。从每个未访问的顶点开始做DFS,同时给每个顶点染色,遍历下一个顶点的时候则染成不一样的颜色。当发现本顶点和下一个顶点颜色相同的时候返回false。代码如下:

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class Solution {
    public boolean possibleBipartition(int N, int[][] dislikes) {
        if (dislikes == null || dislikes.length == 0) {
            return true;
        }
        // 以邻接表建图
        List<List<Integer>> graph = buildGraph(N, dislikes);
        // 开一个数组表示每个顶点的颜色。-1表示未访问,0和1分别表示两种颜色
        int[] colors = new int[N];
        Arrays.fill(colors, -1);
        
        for (int i = 0; i < N; i++) {
        	// 如果未访问则对其访问
            if (colors[i] == -1) {
                if (!dfs(i, graph, colors, 0)) {
                    return false;
                }
            }
        }
        
        return true;
    }
    
    // 从cur开始做DFS,并将cur染色为color
    private boolean dfs(int cur, List<List<Integer>> graph, int[] colors, int color) {
        colors[cur] = color;
        for (int next : graph.get(cur)) {
        	// 如果发现某个邻居的颜色和自己一样,则返回false
            if (colors[next] == color) {
                return false;
            }
            // 若发现next遍历后会发现某两个邻居染色一样,也返回false
            if (colors[next] == -1 && !dfs(next, graph, colors, color ^ 1)) {
                return false;
            }
        }
        
        // 遍历完后发现没问题,返回true
        return true;
    }
    
    private List<List<Integer>> buildGraph(int N, int[][] dislikes) {
        List<List<Integer>> graph = new ArrayList<>(N);
        for (int i = 0; i < N; i++) {
            graph.add(new ArrayList<>());
        }
        
        for (int[] dislike : dislikes) {
            graph.get(dislike[0] - 1).add(dislike[1] - 1);
            graph.get(dislike[1] - 1).add(dislike[0] - 1);
        }
        
        return graph;
    }
}

时空复杂度 O ( V + E ) O(V+E) O(V+E)

法2:BFS。BFS天然很适合判断二分图,因为在BFS的时候可以对访问的顶点进行分层,很显然相邻的两层的顶点就涂不同的颜色。如果发现某个顶点之前涂过,但在接下来的遍历中需要涂一个不同的颜色的时候,就返回false。代码如下:

import java.util.*;

public class Solution {
    public boolean possibleBipartition(int N, int[][] dislikes) {
        if (dislikes == null || dislikes.length == 0) {
            return true;
        }
        
        List<List<Integer>> graph = buildGraph(N, dislikes);
        int[] colors = new int[N];
        Arrays.fill(colors, -1);
        
        for (int i = 0; i < N; i++) {
            if (colors[i] == -1) {
                if (!bfs(i, graph, colors)) {
                    return false;
                }
            }
        }
        
        return true;
    }
    
    private boolean bfs(int cur, List<List<Integer>> graph, int[] colors) {
        int color = 0;
        colors[cur] = color;
        
        Queue<Integer> queue = new ArrayDeque<>();
        queue.offer(cur);
        while (!queue.isEmpty()) {
        	// color代表将要入队的元素需要涂的颜色
            color ^= 1;
            int size = queue.size();
            for (int i = 0; i < size; i++) {
                int head = queue.poll();
                for (int next : graph.get(head)) {
                	// 如果接下来要入队的顶点的颜色已经涂好,并且和head一样,那就返回false
                    if (colors[next] == (color ^ 1)) {
                        return false;
                    }
                    // 如果未访问,则对其进行涂色并入队
                    if (colors[next] == -1) {
                        colors[next] = color;
                        queue.offer(next);
                    }
                }
            }
        }
        
        return true;
    }
    
    private List<List<Integer>> buildGraph(int N, int[][] dislikes) {
        List<List<Integer>> graph = new ArrayList<>(N);
        for (int i = 0; i < N; i++) {
            graph.add(new ArrayList<>());
        }
        
        for (int[] dislike : dislikes) {
            graph.get(dislike[0] - 1).add(dislike[1] - 1);
            graph.get(dislike[1] - 1).add(dislike[0] - 1);
        }
        
        return graph;
    }
}

时空复杂度一样。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值