LeetCode 886. Possible Bipartition
原题链接在这里:https://leetcode.com/problems/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
Note:
- 1 <= N <= 2000
- 0 <= dislikes.length <= 10000
- 1 <= dislikes[i][j] <= N
- dislikes[i][0] < dislikes[i][1]
- There does not exist i != jfor whichdislikes[i] == dislikes[j].
题解:
To determine it is bipartition, we could see if we could color them into 2 different colors.
First use dislikes to construct a graph.
For the node hasn't been color before and it is in the graph, put it as one color 1, then perform BFS.
For cur polled number, if its neighbor has the same color, then return false.
Time Complexity: O(N+E). E = dislikes.length.
Space: O(N).
AC Java:
 class Solution {
     public boolean possibleBipartition(int N, int[][] dislikes) {
         Map<Integer, Set<Integer>> graph = new HashMap<>();
         for(int [] d : dislikes){
             graph.putIfAbsent(d[0], new HashSet<Integer>());
             graph.putIfAbsent(d[1], new HashSet<Integer>());
             graph.get(d[0]).add(d[1]);
             graph.get(d[1]).add(d[0]);
         }
         int [] color = new int[N+1];
         for(int i = 1; i<=N; i++){
             if(color[i]==0 && graph.containsKey(i)){
                 color[i] = 1;
                 LinkedList<Integer> que = new LinkedList<>();
                 que.add(i);
                 while(!que.isEmpty()){
                     int cur = que.poll();
                     for(int nei : graph.get(cur)){
                         if(color[nei] == 0){
                             color[nei] = -color[cur];
                             que.add(nei);
                         }else if(color[nei] == color[cur]){
                             return false;
                         }
                     }
                 }
             }
         }
         return true;
     }
 }
LeetCode 886. Possible Bipartition的更多相关文章
- 【LeetCode】886. Possible Bipartition 解题报告(Python)
		[LeetCode]886. Possible Bipartition 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu ... 
- leetcode 886. 可能的二分法(DFS,染色,种类并查集)
		题目链接 886. 可能的二分法 题意: 给定一组 N 人(编号为 1, 2, ..., N), 我们想把每个人分进任意大小的两组. 每个人都可能不喜欢其他人,那么他们不应该属于同一组. 形式上,如果 ... 
- leetcode 890. Possible Bipartition
		Given a set of N people (numbered 1, 2, ..., N), we would like to split everyone into two groups of ... 
- All LeetCode Questions List 题目汇总
		All LeetCode Questions List(Part of Answers, still updating) 题目汇总及部分答案(持续更新中) Leetcode problems clas ... 
- 算法与数据结构基础 - 深度优先搜索(DFS)
		DFS基础 深度优先搜索(Depth First Search)是一种搜索思路,相比广度优先搜索(BFS),DFS对每一个分枝路径深入到不能再深入为止,其应用于树/图的遍历.嵌套关系处理.回溯等,可以 ... 
- [LeetCode] Possible Bipartition 可能的二分图
		Given a set of N people (numbered 1, 2, ..., N), we would like to split everyone into two groups of ... 
- Leetcode(886)-可能的二分法
		给定一组 N 人(编号为 1, 2, ..., N), 我们想把每个人分进任意大小的两组. 每个人都可能不喜欢其他人,那么他们不应该属于同一组. 形式上,如果 dislikes[i] = [a, b] ... 
- Swift LeetCode 目录 | Catalog
		请点击页面左上角 -> Fork me on Github 或直接访问本项目Github地址:LeetCode Solution by Swift 说明:题目中含有$符号则为付费题目. 如 ... 
- [LeetCode] Is Graph Bipartite? 是二分图么?
		Given an undirected graph, return true if and only if it is bipartite. Recall that a graph is bipart ... 
随机推荐
- Redis Desktop Manager 0.9.3 版本下载
			因为Redis Desktop Manager作者在 0.9.4 版本之后选择对所有的安装包收费,不再提供安装包下载,但是源码依旧公开.链接:https://pan.baidu.com/s/1SXsy ... 
- JAVA并发编程: CAS和AQS
			版权声明:本文为博主原创文章,转载请注明出处 https://blog.csdn.net/u010862794/article/details/72892300 说起JAVA并发编程,就不得不聊 ... 
- 消除VS中动态申请二维数组C6011,C6385,C6386的警告
			动态申请二维数组,无非就是通过指针来实现.@wowpH 过程分三步:1.申请内存,2.使用数组,3.释放内存. 代码如下: /************************************* ... 
- c语言数据结构之线性表的顺序存储结构
			线性表,即线性存储结构,将具有“一对一”关系的数据“线性”地存储到物理空间中,这种存储结构就称为线性存储结构,简称线性表. 注意:使用线性表存储的数据,要求数据类型必须一致,线性表存储的数据,要么全不 ... 
- 『数 变进制状压dp』
			数 Description 给定正整数n,m,问有多少个正整数满足: (1) 不含前导0: (2) 是m的倍数: (3) 可以通过重排列各个数位得到n. \(n\leq10^{20},m\leq100 ... 
- c++打印实心菱形,空心三角形,十字星,空心正方形,实心平行四边形
			今天翻资料的时候,无意间发现了一个文件,是刚接触编程的时候用c++写的一段程序,我称之为"图形打印机",想着把所有图形都打印出来,后来发现其实每种图形的代码都是一个思路,就不想做重 ... 
- django中navie时间和aware时间详解
			navie时间和aware时间: 什么是navie时间?什么是aware时间? navie时间:不知道自己的时间表示的是哪个时区的.也就是不知道自己几斤几两.比较幼稚. aware时间:知道自己的时间 ... 
- spring cloud gateway 深入了解 - Predicate
			文章来源 spring cloud gateway 通过谓词(Predicate)来匹配来自用户的请求 为了方便,使用postman测试不同的谓词的效果 路径谓词(Predicate)—— 最简单的谓 ... 
- 第一个Golang程序
			1. 第一个 HelloGolang 程序 1.1 Go 源程序的基本概念 Go 源程序就是一个特殊格式的文本文件,可以使用任意文本编辑软件做 Go 的开发 Go 程序的 文件扩展名 通常都是 .go ... 
- 爬虫--selenium之 chromedriver与chrome版本映射表(最新至v2.46版本chromedriver)
			本文主要整理了selenium的chromedriver与chrome版本映射表,并且持续更新中..... 1.selenium之 chromedriver与chrome版本映射表(最新至v2.46版 ... 
