-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathدورنگی.cpp
More file actions
94 lines (78 loc) · 1.74 KB
/
دورنگی.cpp
File metadata and controls
94 lines (78 loc) · 1.74 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
84
85
86
87
88
89
90
91
92
93
94
#include <iostream>
#include <vector>
using namespace std;
class Node{
public:
Node()=default;
int Color(){return color;}
void Color(int c){color=c;}
void Neighbors(int newN){neighbors.push_back(newN);}
vector<int> neighbors;
private:
int color=0;
};
class Graph{
public:
Graph(int n){
nodes=new Node[n];
size=n;
}
~Graph(){
delete[] nodes;
}
void addNeighbors(int u,int w){
nodes[u].Neighbors(w);
nodes[w].Neighbors(u);
}
bool color(){
for(int i=0;i<size;i++){
bool colorOne=false,colorTwo=false;
for(auto j : nodes[i].neighbors){
if(nodes[j].Color() == 1)
colorOne=true;
else if(nodes[j].Color() == 2)
colorTwo=true;
}
if(colorOne && colorTwo)
return false;
else{
if(!colorOne)
nodes[i].Color(1);
else if(!colorTwo)
nodes[i].Color(2);
}
}
return true;
}
void printNeighbors(int u){
for(auto n : nodes[u].neighbors)
cout << n << ' ';
cout << endl;
}
private:
Node * nodes;
int size;
};
int main()
{
int nodes;
cin >> nodes;
while(nodes){
// cout << "nodes :" << nodes << endl;
Graph g(nodes);
int edges;
cin >> edges;
while(edges){
int u,w;
cin >> u; cin >> w;
g.addNeighbors(u,w);
edges--;
}
if(g.color())
cout << "BICOLORABLE." << endl;
else
cout << "NOT BICOLORABLE." << endl;
cin >> nodes;
}
return 0;
}