-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathbipartiteGraph.cpp
More file actions
59 lines (49 loc) · 1.15 KB
/
bipartiteGraph.cpp
File metadata and controls
59 lines (49 loc) · 1.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
// find a graph bipartite or not
#include<bits/stdc++.h>
using namespace std;
void bipartite(vector<vector<int>> graph, int V){
queue<int> q;
char color[V]; for(int i = 0; i < V; i++) { color[i] = 'N';} // color array
q.push(0);
color[0] = 'R'; // first vertex
while(!q.empty()){
int frnt = q.front();
char clr = color[frnt];
for(int i = 0; i < graph[frnt].size(); i++){
if(color[graph[frnt][i]] == 'N'){
if(clr == 'R'){ color[graph[frnt][i]] = 'B';}
else{ color[graph[frnt][i]] = 'R'; }
q.push(graph[frnt][i]);
}
}
q.pop();
}
cout<<"color arary"<<endl;
for(int i = 0; i < V; i++){
cout<<color[i]<<" ";
}
int flag = 1;
for(int i = 0; i < V; i++){
for(int j = 0; j < graph[i].size(); j++){
if(color[i] == color[graph[i][j]]){ // adject edge has same color Break
cout<<"\nnot bipartite\n";
flag = 0;
break;
}
}
if(flag == 0){ break;}
}
if(flag ==1){ cout<<"\nYes bipaartite"; }
}
int main(){
int V; cin>>V;
int E; cin>>E;
vector<vector<int> > graph(V);
for(int i = 0; i < E; i++){
int x; cin>>x;
int y; cin>>y;
graph[x-1].push_back(y-1);
graph[y-1].push_back(x-1);
}
bipartite(graph,V);
}