-
Notifications
You must be signed in to change notification settings - Fork 46
/
Possible Bipartition.cpp
73 lines (64 loc) · 1.84 KB
/
Possible Bipartition.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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
class Solution {
public:
bool possibleBipartition(int N, vector<vector<int>>& dislikes) {
vector<vector<int>> graph( N );
for ( auto x : dislikes ){
x[0]--;x[1]--;
graph[x[0]].push_back(x[1]);
graph[x[1]].push_back(x[0]);
}
queue< int > q;
vector<int> color(N ,-1);
for ( int v = 0; v < N ; v++){
if ( color[v] == -1){
q.push(v);
color[v]=1;
while( !q.empty()){
int top = q.front();
q.pop();
for ( int x : graph[top]){
if ( color[x] == -1){
color[x] = 3 - color[top];
q.push(x);
}
else if ( color[x] == color[top]) return false;
}
}
}
}
return true;
}
};
/*
DFS
*/
class Solution {
public:
int dfs( vector<vector<int>> graph , vector<int> &color , int src ,int col =1){
for ( int x : graph[src]){
if (color[x] == -1){
color[x] = 3 -col;
if (!dfs(graph , color , x , 3 -col ,src)) return 0;
}
else if ( color[x] == col) return 0;
}
return 1;
}
bool possibleBipartition(int N, vector<vector<int>>& dislikes) {
vector<vector<int>> graph( N );
for ( auto x : dislikes ){
x[0]--;x[1]--;
graph[x[0]].push_back(x[1]);
graph[x[1]].push_back(x[0]);
}
bool ok = true;
vector<int> color(N ,-1);
for ( int v = 0; v < N ; v++){
if ( color[v] == -1){
color[v] =1;
ok&=dfs(graph ,color , v);
}
}
return ok;
}
};