forked from vedant781999/Array_operation
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBipartite-Graph.cpp
More file actions
58 lines (55 loc) · 963 Bytes
/
Bipartite-Graph.cpp
File metadata and controls
58 lines (55 loc) · 963 Bytes
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
#include<bits/stdc++.h>
using namespace std;
vector<vector<int>> adj;
vector<bool> vis;
vector<int> col;
bool bipart;
void color(int u,int curr)
{
if(col[u]!=-1 && col[u]!=curr)
{
bipart=false;
return;
}
col[u]=curr;
if(vis[u])
{
return;
}
vis[u]=true;
for(auto i: adj[u])
{
color(i,curr xor 1);
}
}
int main()
{
int n,m;
cin>>n>>m;
bipart = true;
adj = vector<vector<int>>(n);
vis = vector<bool>(n,false);
col = 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(!vis[i])
{
color(i,0);
}
}
if(bipart)
{
cout<<"Graph is Bipartite";
}
else{
cout<<"Graph is not Bipartite";
}
return 0;
}