[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 说明:题目中含有$符号则为付费题目. 如 ...
随机推荐
- ABP代码生成器与升级到VS2017VSIX
首先,我不是要分享一个代码生成器,而是怎么升级到VS2017,简单介绍下 如何将2015的VSIX项目升级到2017 阳光铭睿 写了一篇<分享一个与ABP配套使用的代码生成器源码>,并在群 ...
- 【JS】VUE学习
VUE的全家桶:vue-cli,vue-router,vue-resource,vuex 环境搭建:https://www.jianshu.com/p/32beaca25c0d 先码在这儿吧. htt ...
- The Quad - Directory Explorer(一款四窗口的文件资源管理器)
官网:http://www.q-dir.com/ 参考这位兄弟的介绍:https://www.cnblogs.com/clso/p/4694486.html 一款四窗口的文件资源管理器.
- composer操作简单解析
1. composer配置中国镜像 #使用命令: composer config -e#修改composer.json 添加如下代码 { "repositories": [ { & ...
- TFS2015创建项目
1,在TFS服务器上的团队项目集合中创建集合 2,创建集合完毕后,在VS2017中选择管理连接,创建对应的管理连接. 3,团队资源管理器中新建团队项目.后续就是下一步,下一步完成.帐号权限 ...
- 高性能HTTP加速器Varnish-3.0.3搭建、配置及优化步骤
经过一天的努力,终于将Varnish缓存服务器部署到线上服务器了.趁着热乎劲儿,赶紧给大家分享一下.Varnish是一个轻量级的Cache和反向代理软件.先进的设计理念和成熟的设计框架是Varnish ...
- 结对开发项目--石家庄地铁web版
一.功能要求 1.数据库设计:将石家庄地铁线路图的各个线路,各个站点,换乘信息等用数据库的形式保存起来,应该保存的信息有 {线路号,线路的各个站名,车站的换乘信息}. 2.站点查询:用户可以输入任一一 ...
- spring-boot-starter-actouator2.1.4与c3p0版本0.9.1.2冲突
报错前的pom文件: <?xml version="1.0" encoding="UTF-8"?><project xmlns="h ...
- linux中sogou输入法崩溃重启
经常在linux中搜狗输入法用着用着就崩溃了,无法输入中文,又不想重启电脑,照着下面在终端输入命令可以重启输入法: 1.先关闭fcitx(小企鹅输入法,提供了良好的中文输入法环境) # killall ...
- python之lambda函数
今天复习python,看见一个关于lambda函数的例子,在python中使用lambda在某些时候十分方便,因为不必为了实现某些简单功能而新建一个函数.但是有这么一个lambda实例令我有些疑惑,现 ...