[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 <= 20000 <= dislikes.length <= 100001 <= dislikes[i][j] <= Ndislikes[i][0] < dislikes[i][1]- There does not exist
i != jfor 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 说明:题目中含有$符号则为付费题目. 如 ...
随机推荐
- 2018-2019-2-20175235 实验一 《Java开发环境的熟悉》实验报告
实验一 Java开发环境的熟悉 实验内容及要求: 1.使用JDK编写简单的Java程序 2.使用IDEA编辑编译运行调试测试Java程序 实验内容,步骤与心得: (一).Linux命令行下Java程序 ...
- 【转】pyhton之Reportlab模块——生成pdf文件
[转]pyhton之Reportlab模块 reportlab模块是用python语言生成pdf文件的模块 安装:pip install reportlab 模块默认不支持中文,如果使用中文需要注册 ...
- cocos2dx模拟器修改窗口大小
修改模拟器窗口大小SimulatorWin.cpp搜索 frameSize修改frameSize = Size(1920*0.9, 1080*0.9);
- Image Pipeline
Image Pipeline Scrapy 提供了专门下载文件或者图片的Pipeline,下载图片与文件的原理同抓取网页的原理是一样的,所以他们的下载过程支持多线程与异步,十分的高效 Image Pi ...
- UGUI中粒子特效与UI的遮挡问题
问题背景: 在做主线任务时发现完成任务后的特效显示穿透上面的UI层,不美观,策划不乐意了,抓紧解决下 解决思路: 首先讲下影响渲染顺序的因素: 能够影响渲染顺序的因素有:1.Camera Depth ...
- syntax error near unexpected token `do(写的shell脚本出现格式问题)--->1.问题2.展示信息3.解决方案
1问题:Linux和windows下的回车换行符不兼容的问题 [root@node-01 script]# sh start_zk.sh art_zk.sh: line 3: syntax error ...
- 前端angular使用crypto-js进行加密
首先下载大包 npm install crypto-js 然后下载ts版本的包 npm install --save @types/crypto-js 接着在头部导入crypto-js模块 impor ...
- Python学习笔记十一
1. 协程 并发的解决方案: 多进程 多线程 什么叫并发:看起来同时进行 如何实现并发:切换+保存状态 进程线程都是由操作系统调度的 协程:单线程下实现的并发,应用程序级别的切换, ...
- python3+Robot Framework+PyCharm第一个WEB UI自动化用例
这里只是列举一个很简单的例子,简单介绍工具的使用,编写用例之前,做好WEB UI自动化的准备工作,下载好chrome驱动(这里以chrome为例,不同浏览器有对应的驱动),注意驱动和浏览器版本要对应, ...
- EntityFramework+EntityFramework.SqlServerCompact部署网站
1,最好通过Nuget添加引用EntityFramework.SqlServerCompact,省得去手动填写配置文件. 2,部署后遇到如下的问题: 原因是打包后的Bin下面缺少System.Data ...