-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSolution9.java
More file actions
81 lines (74 loc) · 2.29 KB
/
Solution9.java
File metadata and controls
81 lines (74 loc) · 2.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
/**
* @description:
*
* 给定一组 n 人(编号为 1, 2, ..., n), 我们想把每个人分进任意大小的两组。每个人都可能不喜欢其他人,那么他们不应该属于同一组。
*
* 给定整数 n 和数组 dislikes ,其中 dislikes[i] = [ai, bi] ,表示不允许将编号为 ai 和 bi的人归入同一组。当可以用这种方法将所有人分进两组时,返回 true;否则返回 false。
*
*
*
* 示例 1:
*
* 输入:n = 4, dislikes = [[1,2],[1,3],[2,4]]
* 输出:true
* 解释:group1 [1,4], group2 [2,3]
* 示例 2:
*
* 输入:n = 3, dislikes = [[1,2],[1,3],[2,3]]
* 输出:false
* 示例 3:
*
* 输入:n = 5, dislikes = [[1,2],[2,3],[3,4],[4,5],[1,5]]
* 输出:false
*
*
* 提示:
*
* 1 <= n <= 2000
* 0 <= dislikes.length <= 104
* dislikes[i].length == 2
* 1 <= dislikes[i][j] <= n
* ai < bi
* dislikes 中每一组都 不同
*
*/
class Solution9 {
public boolean possibleBipartition(int n, int[][] dislikes) {
List<Integer>[] graph = new List[n + 1];
for (int i = 0; i <= n; i++) {
graph[i] = new ArrayList<>();
}
// Build the graph
for (int[] pair : dislikes) {
graph[pair[0]].add(pair[1]);
graph[pair[1]].add(pair[0]);
}
// -1 means uncolored, 0 and 1 are the two colors
int[] colors = new int[n + 1];
Arrays.fill(colors, -1);
for (int i = 1; i <= n; i++) {
if (colors[i] == -1) { // Not visited
if (!dfs(i, 0, colors, graph)) {
return false;
}
}
}
return true;
}
private boolean dfs(int node, int color, int[] colors, List<Integer>[] graph) {
colors[node] = color;
for (int neighbor : graph[node]) {
if (colors[neighbor] == -1) { // Not colored yet
if (!dfs(neighbor, 1 - color, colors, graph)) {
return false;
}
} else if (colors[neighbor] == color) { // Same color as current node
return false;
}
}
return true;
}
}