原题链接在这里: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. 1 <= N <= 2000
  2. 0 <= dislikes.length <= 10000
  3. 1 <= dislikes[i][j] <= N
  4. dislikes[i][0] < dislikes[i][1]
  5. There does not exist i != j for which dislikes[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;
}
}

类似Is Graph Bipartite?.

LeetCode 886. Possible Bipartition的更多相关文章

  1. 【LeetCode】886. Possible Bipartition 解题报告(Python)

    [LeetCode]886. Possible Bipartition 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu ...

  2. leetcode 886. 可能的二分法(DFS,染色,种类并查集)

    题目链接 886. 可能的二分法 题意: 给定一组 N 人(编号为 1, 2, ..., N), 我们想把每个人分进任意大小的两组. 每个人都可能不喜欢其他人,那么他们不应该属于同一组. 形式上,如果 ...

  3. leetcode 890. Possible Bipartition

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

  4. All LeetCode Questions List 题目汇总

    All LeetCode Questions List(Part of Answers, still updating) 题目汇总及部分答案(持续更新中) Leetcode problems clas ...

  5. 算法与数据结构基础 - 深度优先搜索(DFS)

    DFS基础 深度优先搜索(Depth First Search)是一种搜索思路,相比广度优先搜索(BFS),DFS对每一个分枝路径深入到不能再深入为止,其应用于树/图的遍历.嵌套关系处理.回溯等,可以 ...

  6. [LeetCode] Possible Bipartition 可能的二分图

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

  7. Leetcode(886)-可能的二分法

    给定一组 N 人(编号为 1, 2, ..., N), 我们想把每个人分进任意大小的两组. 每个人都可能不喜欢其他人,那么他们不应该属于同一组. 形式上,如果 dislikes[i] = [a, b] ...

  8. Swift LeetCode 目录 | Catalog

    请点击页面左上角 -> Fork me on Github 或直接访问本项目Github地址:LeetCode Solution by Swift    说明:题目中含有$符号则为付费题目. 如 ...

  9. [LeetCode] Is Graph Bipartite? 是二分图么?

    Given an undirected graph, return true if and only if it is bipartite. Recall that a graph is bipart ...

随机推荐

  1. ubuntu docker 下mongodb集群和分片

    首先我们计划启动了三个mongo服务:master,salve,arbiter 1.准备工作 新建文件夹如图(每个文件夹下面有db和configdb文件夹): 生成认证文件并修改权限 openssl ...

  2. Python格式化输出——format用法示例

    format OR % 提到Python中的格式化输出方法,一般来说有以下两种方式: print('hello %s' % 'world') # hello world print('hello {} ...

  3. Tomcat 路由请求的实现 Mapper

    在分析 Tomcat 实现之前,首先看一下 Servlet 规范是如何规定容器怎么把请求映射到一个 servlet.本文首发于(微信公众号:顿悟源码) 1. 使用 URL 路径 收到客户端请求后,容器 ...

  4. Linux学习笔记之详解linux软连接和硬链接

    0x00 链接文件 Linux链接分两种,一种被称为硬链接(Hard Link),另一种被称为符号链接(Symbolic Link).默认情况下,ln命令产生硬链接. [软连接] 另外一种连接称之为符 ...

  5. SQL 2008R2问题:用户、组或角色'XXX'在当前数据库中已存在?

    为一个数据库添加一个用户或者映射数据库时,提示以下错误信息: 用户.组或角色 '*****' 在当前数据库中已存在. (Microsoft SQLServer, 错误 : 15023) 问题原因:在还 ...

  6. SecureCRT上传本地文件到linux

    1.使用crt登录到需要操作的linux系统 2.按Alt+P打开sftp传输界面 3.输入pur指令加文件路径,例如:put E://srs-3.0.zip按enter就可以 4.再返回crt界面, ...

  7. C#中如何禁止WindowsMediaPlayer双击全屏显示

    问题描述:在项目中使用WindowsMediaPlayer播放视频时,双击会出现视频全屏的效果,而且视频恢复后会暂停,除非再次双击返回后才能正常播放.那么如何禁止WindowsMediaPlayer的 ...

  8. vuex简单化理解和安装使用

     1.简单化理解 首先你要明白 vuex 的目的 就是为了 集中化的管理项目中 组件所有的 数据状态 (state) 0. 第一步你要明白 , store 的重要性 , store 类似一个中央基站, ...

  9. jQuery选择器与过滤器(二)

    一.jQuery选择器1.基本选择器:所有选择器    *标签选择器    标签名ID选择器    #ID类选择器    .className组合选择器    selector1,selector2 ...

  10. 剑指前端(前端入门笔记系列)——DOM(属性节点)

    DOM(属性节点) 属性节点没有过参加家族关系中,其专用选择器:attributes,返回值为对象的形式,它的键是索引值,也就是用对象模拟了一个伪数组,DOM中选择器返回的都是伪数组(可以使用数组的形 ...