[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 说明:题目中含有$符号则为付费题目. 如 ...
随机推荐
- UGUI打字机效果文本组件
using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; ...
- VS2017 性能优化方法
原文地址:https://www.cnblogs.com/mahatmasmile/p/10394168.html 出处1:https://docs.microsoft.com/zh-cn/visua ...
- asp.net core 发布到iis session无法传递的问题
网站是用asp.net core 的Razor Pages开发的,其中用户登录用到了session,调试运行没有问题,但是发布到iis之后出现session无法记录的问题. 我用log记录查看了一下, ...
- 公设基础equals
1# 覆盖equals方法的通用约定 1.自反性(reflexive) 自己跟自己的比较必须返回true 2.对称性(symmetric) x=y那么y=z 3.传递性(transitive) x= ...
- php 常用的知识点归集(下)
24.静态属性与静态方法在类中的使用 需求:在玩CS的时候不停有伙伴加入,那么现在想知道共有多少人在玩,这个时候就可能用静态变量的方法来处理 利用原有的全局变量的方法来解决以上的问题 <?php ...
- [转载 java 技术栈] eclipse 阅读跟踪 Java 源码的几个小技巧!
本文基于Eclipse IDE,我们每天都使用的IDE其实提供了很多强大的功能,掌握它们,往往能够事半功倍. 1.Quick Type Hierarchy 快速查看类继承体系. 快捷键:Ctrl + ...
- linux命令详解之df命令
df命令概述df命令作用是列出文件系统的整体磁盘空间使用情况.可以用来查看磁盘已被使用多少空间和还剩余多少空间. df命令显示系统中包含每个文件名参数的磁盘使用情况,如果没有文件名参数,则显示所有当前 ...
- 探索Java9 模块系统和反应流
Java9 新特性 ,Java 模块化,Java 反应流 Reactive,Jigsaw 模块系统 Java平台模块系统(JPMS)是Java9中的特性,它是Jigsaw项目的产物.简而言之,它以更简 ...
- 记录vue项目上线遇到的一些问题
1. 静态资源路径不对,在开发模式下正常,打包到服务器上的时候,发现静态资源全部请求不到 原因:开发模式下,本地静态服务器直接从项目目录直接起的,跟static是同目录,写绝对路径没问题,直接loca ...
- How Classes are Found
转载自: https://docs.oracle.com/javase/7/docs/technotes/tools/findingclasses.html How Classes are Found ...