原题链接在这里: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. 注解@Slf4j的使用

    注解@Slf4j的使用 声明:如果不想每次都写private  final Logger logger = LoggerFactory.getLogger(当前类名.class); 可以用注解@Slf ...

  2. Vue父组件如何调用子组件(弹出框)中的方法的问题

    如果子组件是一个弹出框,只有在触发某个点击事件时弹出框才能出现(也就是说在父组件中的子组件使用上用了v-if),那在父组件上如果不点击弹出框是不能获取到$ref的. 原因就是:引用指向的是子组件创建的 ...

  3. C++工程师养成 每日一题(vector使用)

    题目: 链接:https://www.nowcoder.com/questionTerminal/6736cc3ffd1444a4a0057dee89be789b?orderByHotValue来源: ...

  4. python docker api

    开启Remote API docker默认是没有开启Remote API的,需要我们手动开启.编辑/lib/systemd/system/docker.service文件, 在文件里的ExecStar ...

  5. ArrayDeque详解

    美人如斯! ArrayDeque是java中对双端队列的线性实现 一.特性 无容量大小限制,容量按需增长: 非线程安全队列,无同步策略,不支持多线程安全访问: 当用作栈时,性能优于Stack,当用于队 ...

  6. python基础2--if,while,for,逻辑运算

    1.1 条件控制语句 1.if - elif - else 2.常用操作运算符 < > >= <=  == != 3.if elif 后面一定要有条件 else后面没有条件 1 ...

  7. 用axios.all处理并发请求

    如果我们需用在两个接口同时完成后,然后在执行一些逻辑,我们可以使用axios.all处理并发请求,如下所示: function getUserAccount() { return axios.get( ...

  8. 2. vue基础-vue-cli(vue脚手架)

    1. 作用 ​ 快速创建一个基于webpack模板的项目 2. 安装工具 安装webpack:使用npm全局安装webpack,打开命令行工具,输入 npm install webpack -g,安装 ...

  9. 从 Vue 的视角学 React(一)—— 项目搭建

    虽然 Vue 在国内一家独大,但在全球范围内,React 依然是最流行的前端框架 最近和一些朋友聊天,发现很多项目都选择了 React 技术栈,而且公司的新项目也决定使用 React 我一直以来都是走 ...

  10. Nginx作为代理服务

    代理服务简介 什么是代理服务 代理-代理办理(代理理财.代理收货.代理购物等等). HTTP请求没有代理服务的模型图 HTTP请求具有代理服务的模型图 代理分类 正向代理 反向代理 正向代理 当局域网 ...