# 886 Possible Bipartition 可能的二分法

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.

Example:

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 <= 10^4
dislikes[i].length == 2
1 <= dislikes[i][j] <= n
ai < bi
All the pairs of dislikes are unique.

1 <= N <= 2000
0 <= dislikes.length <= 10000
dislikes[i].length == 2
1 <= dislikes[i][j] <= N
dislikes[i][0] < dislikes[i][1]

DFS ➕ 染色

C++:

``````class Solution
{
public:
bool possibleBipartition(int n, vector<vector<int>>& dislikes)
{
vector<vector<int>> graph(n + 1);
for (const auto& d : dislikes)
{
graph[d[0]].emplace_back(d[1]);
graph[d[1]].emplace_back(d[0]);
}
vector<int> color(n + 1,0);
function<bool(int, int)> dfs = [&](int cur_node, int col)
{
if (color[cur_node]) return color[cur_node] == col;
color[cur_node] = col;
for (const auto& next_node : graph[cur_node]) if(!dfs(next_node, -col)) return false;
return true;
};
for (int i = 1; i <= n; i++) if (!color[i] and !dfs(i, 1)) return false;
return true;
}
};
``````

Java:

``````class Solution {
public boolean possibleBipartition(int n, int[][] dislikes) {
List<List<Integer>> graph = new ArrayList<>();
for (int i = 0; i <= n; i++) graph.add(new ArrayList<Integer>());
for (int d[] : dislikes) {
}
int color[] = new int[n + 1];
for (int i = 1; i <= n; i++) if (color[i] == 0 && !dfs(i, 1, color, graph)) return false;
return true;
}

private boolean dfs(int curNode, int col, int[] color, List<List<Integer>> graph) {
if (color[curNode] != 0) return color[curNode] == col;
color[curNode] = col;
for (int nextNode : graph.get(curNode)) if(!dfs(nextNode, -col, color, graph)) return false;
return true;
}
}
``````

Python:

``````class Solution:
def possibleBipartition(self, n: int, dislikes: List[List[int]]) -> bool:
graph, color = defaultdict(list), [0] * (n + 1)
for d in dislikes:
graph[d[0]].append(d[1])
graph[d[1]].append(d[0])

def dfs(cur_node: int, col: int) -> bool:
if color[cur_node]:
return color[cur_node] == col
color[cur_node] = col
for next_node in graph[cur_node]:
if not dfs(next_node, -col):
return False
return True
for i in range(1, n + 1):
if not color[i] and not dfs(i, 1):
return False
return True
``````