-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathisBipartite.cpp
More file actions
70 lines (68 loc) · 2.15 KB
/
isBipartite.cpp
File metadata and controls
70 lines (68 loc) · 2.15 KB
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
class Solution {
public:
bool isBipartite(vector<vector<int>>& graph) {
int n = graph.size();
vector<int> color(n, -1);
queue<int> q;
for (int i = 0; i < n; i++) {
if (color[i] != -1) continue;
q.push(i);
color[i] = 0;
while (!q.empty()) {
int node = q.front();
q.pop();
for (int e: graph[node]) {
if (color[e] == -1) {
q.push(e);
color[e] = 1 - color[node];
}
else if (color[e] == color[node])
return false;
}
}
}
return true;
}
};
// Union-Find
class Solution {
public:
int find(vector<pair<int,int>> &parent, int k){
if(parent[k].first == -1 || parent[k].first==k){
parent[k].first = k;
return k;
}
parent[k].first=find(parent,parent[k].first);
return parent[k].first;
}
void unionFind(vector<pair<int,int>> &parent, int x, int y){
int a = find(parent,x);
int b = find(parent,y);
if(a != b){
if(parent[a].second < parent[b].second){
parent[a].first=b;
}else if(parent[a].second > parent[b].second){
parent[b].first=a;
}else{
parent[b].first=a;
parent[a].second += 1;
}
}
}
bool isBipartite(vector<vector<int>>& graph) {
int n =graph.size();
vector<pair<int,int>> parent(n,{-1,0});
for(int i=0;i<n;i++){
for(int j=0;j<graph[i].size();j++){
// node and neighbour shouldn't belong to same set in bipartite
if(find(parent,i) == find(parent,graph[i][j])){
return false;
}
// all node neighbours should belong to one set
unionFind(parent,graph[i][j],graph[i][0]);
}
}
return true;
}
};
// https://leetcode.com/problems/is-graph-bipartite/discuss/1431464/C%2B%2B-Union-find-solution-optimized-with-union-by-rank