-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPossible Bipartition(BFS).cpp
44 lines (42 loc) · 1.51 KB
/
Possible Bipartition(BFS).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
class Solution {
private:
bool bfs(vector<vector<int>>&graph, vector<int>&group , int start){
queue<int>q;
q.push(start);
group[start] = 1; // group 1
while(!q.empty()){
int curr = q.front();
q.pop();
for(auto&newnode : graph[curr]){
if(group[newnode] == 0){
if(group[curr] == 1){
group[newnode] = 2;
}else{
group[newnode] = 1;
}
q.push(newnode);
}else if(group[newnode] == group[curr]){
return false;
}
}
}
return true;
}
public:
bool possibleBipartition(int n, vector<vector<int>>& dislikes) {
vector<vector<int>>graph(n + 1);
vector<int>group(n + 1, 0);
for(int i=0; i<dislikes.size(); i++){
graph[dislikes[i][0]].push_back(dislikes[i][1]);
graph[dislikes[i][1]].push_back(dislikes[i][0]);
}
for(int i=1; i<=n;i++){
if(group[i] == 0){
if((bfs(graph,group, i )) == false){
return false;
}
}
}
return true ;
}
};