-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathisBipartite.cpp
More file actions
73 lines (61 loc) · 1.28 KB
/
isBipartite.cpp
File metadata and controls
73 lines (61 loc) · 1.28 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
71
72
73
#include<bits/stdc++.h>
using namespace std;
vector<int>G[100];
bool isBipartite(int n,int s)
{
bool visited[n+1];
bool color[n+1];
for(int i=1;i<=n;i++)
{
visited[i]=false;
color[i]=false;
}
visited[s]=true;
color[s]=true;
queue<int>Q;
Q.push(s);
while(!Q.empty())
{
int a=Q.front();
Q.pop();
for(int i=0;i<G[a].size();i++)
{
int b=G[a][i];
if(!visited[b])
{
visited[b]=false;
color[b]=!color[a];
Q.push(s);
}
else if(color[a]==color[b])
{
return false;
}
}
}
return true;
}
int main()
{
int nodes,edges,a,b;
printf("Enter the number of nodes:");
scanf("%d",&nodes);
printf ("Enter the number of edges:");
scanf("%d",& edges);
printf("Enter edges:\n");
for(int i=1;i<=edges;i++)
{
scanf("%d%d",&a,&b);
G[a].push_back(b);
G[b].push_back(a);
}
if(isBipartite(nodes,1))
{
printf("The graph is bipartite\n");
}
else
{
printf("The graph is not bipartite\n");
}
return 0;
}