File tree Expand file tree Collapse file tree 1 file changed +38
-0
lines changed Expand file tree Collapse file tree 1 file changed +38
-0
lines changed Original file line number Diff line number Diff line change
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
+ };
You can’t perform that action at this time.
0 commit comments