当前位置:网站首页>#886 Possible Bipartition
#886 Possible Bipartition
2022-06-12 20:57:00 【yoyooyooo】
Description
We want to split a group of n people (labeled from 1 to n) into two groups of any size. Each person may dislike some other people, and they should not go into the same group.
Given the integer n and the array dislikes where dislikes[i] = [ai, bi] indicates that the person labeled ai does not like the person labeled bi, return true if it is possible to split everyone into two groups in this way.
Examples
Example 1:
Input: n = 4, dislikes = [[1,2],[1,3],[2,4]]
Output: true
Explanation: group1 [1,4] and 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
Constraints:
1 <= n <= 2000
0 <= dislikes.length <= 104
dislikes[i].length == 2
1 <= dislikes[i][j] <= n
ai < bi
All the pairs of dislikes are unique.
思路
当然他和785很接近,所以最开始的时候,就用BFS去判断能不能2色填充。
但是我在submission里面看到了另一种用并查集的方式,其实我不知道它的原理是什么,只是感官上觉得这种方式是合理的,就先记录一下,以后可以直接用
代码
BFS
class Solution {
public boolean possibleBipartition(int n, int[][] dislikes) {
List<Set<Integer>> neighbors = new ArrayList<>();
if (dislikes.length == 0)
return true;
for (int i = 0; i < n; i++)
neighbors.add(new HashSet<>());
for (int[] pairs: dislikes) {
neighbors.get(pairs[0] - 1).add(pairs[1] - 1);
neighbors.get(pairs[1] - 1).add(pairs[0] - 1);
}
int[] color = new int[n];
int[] visited = new int[n];
for (int i = 0; i < n ; i++) {
if (visited[i] == 1)
continue;
List<Integer> parent = new ArrayList<>();
parent.add(i);
int currColor = 1;
color[i] = 1;
while(parent.size() != 0) {
List<Integer> children = new ArrayList<>();
for (int start: parent) {
if (visited[start] == 1)
continue;
visited[start] = 1;
for (int end: neighbors.get(start)) {
if (color[end] == currColor)
return false;
if (color[end] == -currColor)
continue;
color[end] = -currColor;
children.add(end);
}
}
currColor = -currColor;
parent = children;
}
}
return true;
}
}
并查集
class Solution {
int[] parent;
public void union(int x, int y) {
int parentX = find(x);
int parentY = find(y);
if (parentX == parentY)
return;
parent[parentX] = parentY;
}
public int find(int x) {
if (x != parent[x])
parent[x] = find(parent[x]);
return parent[x];
}
public boolean possibleBipartition(int n, int[][] dislikes) {
parent = new int[2 * n];
for (int i = 0; i < n * 2; i++) {
parent[i] = i;
}
for (int[] dislike: dislikes) {
union(dislike[0] - 1 + n, dislike[1] - 1);
union(dislike[0] - 1, dislike[1] - 1 + n);
}
for (int i = 0; i < n; i++) {
if (find(i) == find(i + n))
return false;
}
return true;
}
}
边栏推荐
- Scope and scope chain
- Shell language
- A blog written clearly by vit
- Data visualization - Calendar chart
- Leetcode: 210. Programme II
- China hydraulic press market trend report, technical innovation and market forecast
- Scatter in pytorch_ () function
- Successful transition from self-study test halfway, 10K for the first test
- Do we media video, and share the necessary app for friendly new media operation
- torch. clamp_ min_ method
猜你喜欢

阿里前辈给我推荐的软件测试人员必读书籍(附电子书),让我受益匪浅...

Illustrator tutorial, how to recolor artwork in illustrator?

Solution of multi machine room dynamic loop status network touch screen monitoring

解决cvxpy报错The solver GLPK_MI is not installed

Design and practice of Hudi bucket index in byte skipping

Nexus3 build local warehouse

View 的事件分发机制

Introduction to scala basic grammar (III) various operators in Scala

2022年春招,测试工程师全套面试攻略,一篇吃透全部技术栈(全是干货)

My way of programming
随机推荐
Summary of machine learning materials
JS deep and shallow copy
Pytoch distributed training error
Data visualization diagram microblog forwarding diagram
Mxnet record IO details
torch. Finfo function
Zhangqiming, vice director of the United Front Work Department of the CPC Anhui Provincial Committee, led a team to investigate the HoloNet Royal Hefei R & D base
最简单ALV模板
China hydraulic press market trend report, technical innovation and market forecast
new做了哪几件事
leetcode:210. 课程表 II
The required books for software testers (with e-books) recommended by senior Ali have benefited me a lot
MySQL field truncation principle and source code analysis
Composer version degradation
How mysterious is "PIP not an internal or external command, nor a runnable program or batch file"
UVa11991 Easy Problem from Rujia Liu
可测性设计学习笔记
对闭包的理解
Maximize tensorflow* CPU performance (shell)
SAP WM preliminary transaction code lx29 - list of fixed storage bins