当前位置:网站首页>#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;
}
}
边栏推荐
- 居家办公期间如何提升沟通效率|社区征文
- Maximize tensorflow* CPU performance (shell)
- MySQL + PostgreSQL batch insert update insertorupdate
- Nexus3 build local warehouse
- String Basics
- Research Report on hydraulic injection machine industry - market status analysis and development prospect forecast
- Unauthorized rce in VMware vCenter
- Before job hopping, Jin San made up the interview questions. Jin San successfully landed at Tencent and got a 30K test offer
- 逐向双碳:东数西算中的绿色需求与竞争焦点
- Solve the cvxpy error the solver GLPK_ MI is not installed
猜你喜欢

Scope and scope chain

测试人如何规划自己的未来?才能实现入行2年达到25k?

Design and practice of Hudi bucket index in byte skipping

View 的事件分发机制

In the spring recruitment of 2022, the test engineer will have a full set of interview strategies to thoroughly understand all the technical stacks (all dry goods)

What are the disadvantages of bone conduction earphones? Analysis of advantages and disadvantages of bone conduction earphones

跳槽前恶补面试题,金三成功上岸腾讯,拿到30k的测开offer

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

New product release Junda intelligent integrated environmental monitoring terminal

Data visualization - biaxial comparison effect
随机推荐
中小型机房动力环境综合监控解决方案
remote: Support for password authentication was removed on August 13, 2021
Market trend report, technical innovation and market forecast of hydraulic torque wrench in China
Design and practice of Hudi bucket index in byte skipping
How do testers plan for their future? To achieve 25K in 2 years?
What are the disadvantages of bone conduction earphones? Analysis of advantages and disadvantages of bone conduction earphones
My way of programming
Listener in JSP
To understand Devops, you must read these ten books!
设计规则检查约束(set_max_transition、set_max_capacitance)
How mysterious is "PIP not an internal or external command, nor a runnable program or batch file"
Allegro Xile technology, a developer of distributed cloud services, received millions of dollars of angel round financing and was independently invested by Yaotu capital
Event distribution mechanism of view
Product Manager: "click here to jump to any page I want to jump" -- decoupling efficiency improving artifact "unified hop routing"
Lake shore PT-100 platinum resistance temperature sensor
new做了哪几件事
GPU giant NVIDIA suffered a "devastating" network attack, and the number one malware shut down its botnet infrastructure | global network security hotspot on February 28
How to download putty using alicloud image?
leetcode:207. 课程表
Maximize tensorflow* CPU performance (shell)