Skip to content

Commit c85b55b

Browse files
authored
Create Day27.cpp
1 parent 17686d4 commit c85b55b

File tree

1 file changed

+38
-0
lines changed

1 file changed

+38
-0
lines changed

Day27.cpp

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/*
2+
Author: Aryan Yadav
3+
Possible Bipartition
4+
5+
Algorithm: DFS
6+
Difficulty: Hard
7+
*/
8+
9+
class Solution {
10+
public:
11+
12+
bool possibleBipartition(int N, vector<vector<int>>& dislikes) {
13+
g = vector<vector<int>>(N);
14+
for( const auto& d:dislikes){
15+
g[d[0] -1].push_back(d[1]-1);
16+
g[d[1] -1].push_back(d[0]-1);
17+
}
18+
colors = vector<int>(N,0);
19+
for(int i=0;i<N;i++){
20+
if(colors[i] == 0 && !dfs(i,1))
21+
return false;
22+
}
23+
return true;
24+
}
25+
private:
26+
vector<vector<int>>g;
27+
vector<int>colors;
28+
bool dfs(int cur, int color){
29+
colors[cur] = color;
30+
for(int nxt:g[cur]){
31+
if (colors[nxt] == color)
32+
return false;
33+
if(colors[nxt] == 0 && !dfs(nxt, -color))
34+
return false;
35+
}
36+
return true;
37+
}
38+
};

0 commit comments

Comments
 (0)