-
Notifications
You must be signed in to change notification settings - Fork 0
/
bipartite_graph.cpp
54 lines (43 loc) · 946 Bytes
/
bipartite_graph.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
#include<bits/stdc++.h>
using namespace std;
vector<vector<int>> adj;
vector<bool> visited;
vector<int> colors;
bool bipart;
void setColor(int u, int current) {
if(colors[u] != -1 && colors[u] != current) {
bipart = false;
return;
}
colors[u] = current;
if(visited[u])
return;
visited[u] = true;
for(auto i: adj[u]) {
setColor(i, current xor 1);
}
}
int main() {
bipart = true;
int n, m;
cin>>n>>m;
adj = vector<vector<int>>(n);
visited = vector<bool>(n, false);
colors = vector<int>(n, -1);
for(int i=0;i<m; i++) {
int u, v;
cin>>u>>v;
adj[u].push_back(v);
adj[v].push_back(u);
}
for(int i=0; i<n; i++) {
if(!visited[i]) {
setColor(i, 0);
}
}
if(bipart)
cout<<"Given graph is bipartite";
else
cout<<"Given graph is not bipartite";
return 0;
}