-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path10004.cpp
More file actions
83 lines (77 loc) · 1.67 KB
/
10004.cpp
File metadata and controls
83 lines (77 loc) · 1.67 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
74
75
76
77
78
79
80
81
82
83
#include<bits/stdc++.h>
using namespace std;
map< int, int > visited;
map<int ,string> color;
void BFS(int s, map< int, vector< int > >G)
{
queue< int >q;
q.push(s);
visited[s] = 0;
color[s] = "r";
while(!q.empty())
{
string x;
int top = q.front();
if(color[top] == "r"){
x = "b";
}
else{
x = "r";
}
for(int i=0; i<(int)G[top].size(); i++)
{
int n = G[top][i];
if(!visited.count(n)) // checks whether n is the valid key or not
{
visited[n] = visited[top] + 1;
color[n] = x;
q.push(n);
}
}
q.pop();
}
}
int main()
{
int totVertice;
int edges;
while( (scanf("%d",&totVertice) == 1) && totVertice != 0 && (scanf("%d",&edges)== 1))
{
map< int,vector< int > >G;
int source; // for this problem code source = 0
for(int i=0; i<edges; i++)
{
int x, y;
scanf("%d %d", &x, &y);
if(i == 0){
source = x;
}
G[x].push_back(y);
G[y].push_back(x);
}
BFS(source,G);
string t1;
int flag = 0;
map< int ,int >:: iterator i;
for(i = visited.begin();i != visited.end();i++){
//cout<< color[i->first]<< " : ";
t1 = color[i->first];
for( int j = 0; j<(int)G[i->first].size() ; j++){
if(t1 == color[G[i->first][j]] ){
flag = 1;
}
//cout<< color[G[i->first][j]] <<' ';
}
//cout<<endl;
}
G.clear();
visited.clear();
color.clear();
if(flag){
cout<<"NOT BICOLORABLE."<<endl;
}else{
cout<<"BICOLORABLE."<<endl;
}
}
return 0;
}