当前位置:网站首页>#886 Possible Bipartition
#886 Possible Bipartition
2022-06-12 20:59: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.
Ideas
Of course he and 785 Very close to , So in the beginning , Just use BFS To judge if you can 2 Color fill .
But I am submission There is another way to use union to search sets , In fact, I don't know what its principle is , I just feel that this way is reasonable , Just record it first , It can be used directly in the future
Code
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;
}
}
Union checking set
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;
}
}
边栏推荐
- UVa11991 Easy Problem from Rujia Liu
- Lake shore PT-100 platinum resistance temperature sensor
- pytorch transforms. Use of lambda
- Data visualization - broken line area chart
- UVa11991 Easy Problem from Rujia Liu
- Social metauniverse: start from redefining yourself
- China hydraulic press market trend report, technical innovation and market forecast
- Lua pattern matching
- What are the barriers that Huawei terminals need to cross if they want to rely on the intelligent turnover of the whole house?
- Design and practice of Hudi bucket index in byte skipping
猜你喜欢

Data visualization - histogram

Lightroom Ambassador series: capturing nostalgia with MEG loeks

多机房动环状态网络触摸屏监控解决方案

leetcode:207. Class Schedule Card

Lake shore PT-100 platinum resistance temperature sensor

没有学历,自学软件测试,找到一份月入过万的测试工作真的有可能吗?

Solve the cvxpy error the solver GLPK_ MI is not installed

机器学习资料汇总

Listener in JSP

跳槽前恶补面试题,金三成功上岸腾讯,拿到30k的测开offer
随机推荐
New product release Junda intelligent integrated environmental monitoring terminal
The salted fish has been transmitted for 5W times, and the latest spring recruit face-to-face test questions of bytes have been leaked
Gather function in pytorch_
Detect current system language
Cv2.lut() (populates the output array with values from the lookup table)
SAP WM preliminary transaction code lx29 - list of fixed storage bins
leetcode:210. 課程錶 II
What are the barriers that Huawei terminals need to cross if they want to rely on the intelligent turnover of the whole house?
解决cvxpy报错The solver GLPK_MI is not installed
Deploy etcd cluster in static pod mode
At the beginning of SAP QM, assign sampling strategy for quantitative characteristics
To understand Devops, you must read these ten books!
[tutorial] detailed explanation of the page rules parameter settings of cloudflare CDN
多机房动环状态网络触摸屏监控解决方案
Library cache lock brought by add trandata
Nexus3搭建本地仓库
Leetcode: 210. Programme II
Integrated monitoring solution for power environment of small and medium-sized computer rooms
初步了解认识正则表达式(Regex)
DFT learning notes