-
Notifications
You must be signed in to change notification settings - Fork 3.7k
/
bipartite.cpp
46 lines (46 loc) · 1.17 KB
/
bipartite.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
#include<iostream>
#include<vector>
#include<queue>
int main()
{
long int vertex,edge,u,v,c,flag=0;
// taking n ->number of vertex and m -> number of edges
std::cin>>vertex>>edge;
// adj is 2-D vector to store edges between vertex
std::vector<std::vector<long int > >adj(vertex,std::vector<long int>());
// color is use to store color of vertex
std::vector<long int> color(vertex,-1);
std::queue<long int>q;
//for number of edges times
for(int i=0;i<edge;i++)
{
std::cin>>u>>v;
adj[u-1].push_back(v-1);
adj[v-1].push_back(u-1);
}
u=0;
color[u]=0;
q.push(u);
while(!q.empty())
{
c=q.front();
q.pop();
for(auto i=adj[c].begin();i!=adj[c].end();i++)
{
// updating color of vertex
if(color[*i]==-1 )
{
q.push(*i);
color[*i]=1-color[c];
}
else if(color[*i]==color[c]) // checking for bipartite graph
{
flag=1;
}
}
}
if(flag==0)
std::cout<<"Graph is bipartite";
else
std::cout<<"Graph is not bipartite";
}