[LeetCode] 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 != j
for whichdislikes[i] == dislikes[j]
.
解法一:
- class Solution {
- public:
- bool possibleBipartition(int N, vector<vector<int>>& dislikes) {
- vector<vector<int>> g(N + , vector<int>(N + ));
- for (auto dislike : dislikes) {
- g[dislike[]][dislike[]] = ;
- g[dislike[]][dislike[]] = ;
- }
- vector<int> colors(N + );
- for (int i = ; i <= N; ++i) {
- if (colors[i] == && !helper(g, i, , colors)) return false;
- }
- return true;
- }
- bool helper(vector<vector<int>>& g, int cur, int color, vector<int>& colors) {
- colors[cur] = color;
- for (int i = ; i < g.size(); ++i) {
- if (g[cur][i] == ) {
- if (colors[i] == color) return false;
- if (colors[i] == && !helper(g, i, -color, colors)) return false;
- }
- }
- return true;
- }
- };
- class Solution {
- public:
- bool possibleBipartition(int N, vector<vector<int>>& dislikes) {
- vector<vector<int>> g(N + );
- for (auto dislike : dislikes) {
- g[dislike[]].push_back(dislike[]);
- g[dislike[]].push_back(dislike[]);
- }
- vector<int> colors(N + );
- for (int i = ; i <= N; ++i) {
- if (colors[i] != ) continue;
- colors[i] = ;
- queue<int> q{{i}};
- while (!q.empty()) {
- int t = q.front(); q.pop();
- for (int cur : g[t]) {
- if (colors[cur] == colors[t]) return false;
- if (colors[cur] == ) {
- colors[cur] = -colors[t];
- q.push(cur);
- }
- }
- }
- }
- return true;
- }
- };
- class Solution {
- public:
- bool possibleBipartition(int N, vector<vector<int>>& dislikes) {
- unordered_map<int, vector<int>> g;
- for (auto dislike : dislikes) {
- g[dislike[]].push_back(dislike[]);
- g[dislike[]].push_back(dislike[]);
- }
- vector<int> root(N + );
- for (int i = ; i <= N; ++i) root[i] = i;
- for (int i = ; i <= N; ++i) {
- if (!g.count(i)) continue;
- int x = find(root, i), y = find(root, g[i][]);
- if (x == y) return false;
- for (int j = ; j < g[i].size(); ++j) {
- int parent = find(root, g[i][j]);
- if (x == parent) return false;
- root[parent] = y;
- }
- }
- return true;
- }
- int find(vector<int>& root, int i) {
- return root[i] == i ? i : find(root, root[i]);
- }
- };
Github 同步地址:
类似题目:
https://leetcode.com/problems/possible-bipartition/
https://leetcode.com/problems/possible-bipartition/discuss/159085/java-graph
https://leetcode.com/problems/possible-bipartition/discuss/195303/Java-Union-Find
https://leetcode.com/problems/possible-bipartition/discuss/158957/Java-DFS-solution
[LeetCode] Possible Bipartition 可能的二分图的更多相关文章
- [LeetCode] Is Graph Bipartite? 是二分图么?
Given an undirected graph, return true if and only if it is bipartite. Recall that a graph is bipart ...
- [LeetCode] 785. Is Graph Bipartite? 是二分图么?
Given an undirected graph, return true if and only if it is bipartite. Recall that a graph is bipart ...
- 【LeetCode】886. Possible Bipartition 解题报告(Python)
[LeetCode]886. Possible Bipartition 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu ...
- LeetCode 886. Possible Bipartition
原题链接在这里:https://leetcode.com/problems/possible-bipartition/ 题目: Given a set of N people (numbered 1, ...
- leetcode 890. Possible Bipartition
Given a set of N people (numbered 1, 2, ..., N), we would like to split everyone into two groups of ...
- leetcode.图.785判断二分图-Java
1. 具体题目 给定一个无向图graph,当这个图为二分图时返回true.如果我们能将一个图的节点集合分割成两个独立的子集A和B,并使图中的每一条边的两个节点一个来自A集合,一个来自B集合,我们就将这 ...
- Java实现 LeetCode 785 判断二分图(分析题)
785. 判断二分图 给定一个无向图graph,当这个图为二分图时返回true. 如果我们能将一个图的节点集合分割成两个独立的子集A和B,并使图中的每一条边的两个节点一个来自A集合,一个来自B集合,我 ...
- [leetcode]785. Is Graph Bipartite? [bai'pɑrtait] 判断二分图
Given an undirected graph, return true if and only if it is bipartite. Example 1: Input: [[1,3], [0, ...
- Swift LeetCode 目录 | Catalog
请点击页面左上角 -> Fork me on Github 或直接访问本项目Github地址:LeetCode Solution by Swift 说明:题目中含有$符号则为付费题目. 如 ...
随机推荐
- Babel学习小记
一.babel配置文件中的plugins和presets是什么? 1.首先说说babel是什么,babel是一个JavaScript转码器,帮助我们把浏览器不兼容的ES6语法转换成ES5语法: 2.接 ...
- 使用element-ui遇到的各种小问题
一.Dialog对话框 1.在使用嵌套Dialog的时候,会出现遮罩层在内容的上方这种错乱情况 解决办法:http://element-cn.eleme.io/#/zh-CN/component/di ...
- 规范开发目录 及 webpack多环境打包文件配置
规范开发目录 普通项目 开发目录: ├── project-name ├── README.md ├── .gitignore ├── assets ├── ├── js ├── ├── css ├─ ...
- java接口,接口的特性,接口实现多态,面向接口编程
package cn.zy.cellphone; /**接口是一种引用数据类型.使用interface声明接口,形式 * 形式:public interface 接口名称{} * 接口不能拥有构造方法 ...
- APP测试点注意事项汇总
1.异常测试:包括业务流程的异常情况:业务场景的异常:操作习惯的异常(比如答题过程中会出现声音干扰这样很不友好喔~) 2.网络测试:网络切换的过程中,APP会不会异常:断网情况进行一些操作,APP会不 ...
- Core 2.0使用Nlog记录日志+Mysql
一.先创建一个Core2.0的项目,并在NuGet中引入3个类库文件 MySql.Data.dll NLog.dll NLog.Web.AspNetCore.dll 二.创建一个nlog.config ...
- C#连接MySQL
由于工作需要,从本地Sqlite数据库转为MySql数据库.遇到了一些坑,随后又埋了.记录下过程: 一.安装MySql 首先上官网下载windows版的MySql.解压.详情是参考了几位同鞋的文章: ...
- UOJ#394. 【NOI2018】冒泡排序
原文链接https://www.cnblogs.com/zhouzhendong/p/UOJ394.html 题解 首先我们发现一个数不能既被往左换又被往右换.也就是说不能有任何一个数左边有比他大的, ...
- P1113 杂务 拓扑排序
题目描述 John的农场在给奶牛挤奶前有很多杂务要完成,每一项杂务都需要一定的时间来完成它.比如:他们要将奶牛集合起来,将他们赶进牛棚,为奶牛清洗乳房以及一些其它工作.尽早将所有杂务完成是必要的,因为 ...
- Centos将yum源设置为阿里云的镜像源
第一步:备份原有镜像源 mv /etc/yum.repo.d/Centos-Base.repo /etc/yum.repo.d/Centos-Base.repo.bak 第二步:下载阿里云的镜像源 w ...