-
Notifications
You must be signed in to change notification settings - Fork 66
/
Copy pathpossible-partition.cpp
54 lines (38 loc) · 1.8 KB
/
possible-partition.cpp
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
/*
Code by Urjita Sharma
Function for Possible Bipartition
Given a set of N people (numbered 1, 2, ..., N), we would like to split everyone into two groups of any size.
Each person may dislike some other people, and they should not go into the same group.
Formally, if dislikes[i] = [a, b], it means it is not allowed to put the people numbered a and b into the same group.
Return true if and only if it is possible to split everyone into two groups in this way.
*/
bool possibleBipartition(int n, vector<vector<int>>& dislikes) {
vector<vector<int>> graph(n + 1);
for ( auto dislike : dislikes) {
graph[dislike[0]].push_back(dislike[1]);
graph[dislike[1]].push_back(dislike[0]);
}
vector<bool> visited(n + 1, false);
vector<int> groups(n + 1, 0);
for (int i = 1; i <= n; ++i) {
if (!visited[i]) {
groups[i] = 1;
queue<int> to_process;
to_process.push(i);
while (!to_process.empty()) {
auto person = to_process.front();
to_process.pop();
if (visited[person]) continue;
visited[person] = true;
for (auto next_person : graph[person]) {
if (groups[person] == groups[next_person]) {
return false;
}
groups[next_person] = groups[person] == 1 ? 2 : 1;
to_process.push(next_person);
}
}
}
}
return true;
}