原题链接在这里: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. JAVA WEB项目目录结构以及web应用部署的根目录,编译路径和项目根目录的区别

    本文链接:https://blog.csdn.net/l00149133/article/details/78984083 web应用部署的根目录,编译路径和项目的根目录有什么区别? 直接上例子: 你 ...

  2. springmvc流程图以及配置

    springmvc:是完成数据的封装和跳转的功能 流程图如下: springmvc的配置流程 1.导入jar包 二.配置servlet文件 init-param的作用是在启动servlet启动时规定其 ...

  3. Java学习:方法的使用与注意事项

    方法的使用与注意事项 定义一个方法的格式:public static void 方法名称(){ 方法体 } 如何调用方法,格式: 方法名称(): 方法名称的命名规则和变量一样,使用小驼峰. 方法体:也 ...

  4. mingw 编译 glfw3 的 helloworld

    glfw3 为基础开发 GUI 似乎是一个不错选项,有很多人尝试这么做了.今天也小试一把. 工具: mingw(不是 mingw-w64),头文件 GLFW/ ,库文件 glfw3.dll 需要注意, ...

  5. ELK学习笔记之ELK6.0 X-pack设置用户名和密码

    $ bin/elasticsearch-plugin install x-pack -> Downloading x-pack from elastic [=================== ...

  6. 【已解決】谷歌浏览器如何清理缓存(cookie)

    清除缓存快捷键 Ctrl+Shift+Delete

  7. 关于C#对Xml数据解析

    首先进行简单说明Xml 与Html  和 XAML数据标签的差别. 1.Xml属于数据文本, 被设计为传输和存储数据,其焦点是数据的内容.它与json格式数据相似,可作为服务数据传输类型. 其中XML ...

  8. selenium自学笔记---ecshop购买脚本 xpath定位元素(下拉框,单选框)

    本机环境:xamppv3.2.1+ecshop3.0  1.元素定位写对,却一直报错,发现是页面元素加载的太慢,所以加上延时 from selenium import webdriverimport ...

  9. pandas-16 pd.merge()的用法

    pandas-16 pd.merge()的用法 使用过sql语言的话,一定对join,left join, right join等非常熟悉,在pandas中,merge的作用也非常类似. 如:pd.m ...

  10. map字典,储存cookie,切换账户,展示购物车不同商品

    1:首页 1,静态html5+css做好基本样式 html5,css,jQery, sass 2,jsonp的方式src引入模拟的json数据//这里用的jsonp方式拿数据,详情有使用ajax 遍历 ...