forked from smitbose/CodeIIEST
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcoloring.cpp
More file actions
64 lines (59 loc) · 978 Bytes
/
coloring.cpp
File metadata and controls
64 lines (59 loc) · 978 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
59
60
61
62
63
64
#include <cstdio>
#include <iostream>
#include <vector>
#include <queue>
using namespace std;
int main()
{
vector<int> v[100005];
int color[100005];
for(int i=0;i<100005;i++)
{
v[i].clear();
color[i] = -1;
}
cout<<"Enter the number of vertices in the graph: ";
int n,e;
cin>>n;
cout<<"Enter the number of edges in the graph: ";
cin>>e;
for(int i=0;i<e;i++)
{
cout<<"Enter an edge: ";
int x,y;
cin>>x;
cin>>y;
v[x].push_back(y);
v[y].push_back(x);
}
queue<int> q;
q.push(1);
color[1] = 1;
while(!q.empty())
{
int u = q.front();
q.pop();
int s = v[u].size();
for(int j=0;j<s;j++)
{
int w = v[u][j];
if(color[w] == -1)
{
color[w] = 1-color[u];
q.push(w);
}
else if(color[w] == color[u])
{
cout<<"Bipartite partitioning is not possible"<<endl;
return 0;
}
}
}
cout<<"Coloring of the graph is as follows: "<<endl;
for(int i=1;i<=n;i++)
{
cout<<color[i];
}
cout<<endll;
return 0;
}