-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathgraph_coloring.cpp
95 lines (79 loc) · 1.54 KB
/
graph_coloring.cpp
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
95
#include <bits/stdc++.h>
using namespace std;
int m, n;
vector<int> x;
vector<vector<int>> adj;
int counter = 0;
void print(vector<int> &v)
{
// cout << "\n---------------------------------\n";
for (int i = 1; i < v.size(); i++)
{
cout << v[i] << " ";
}
cout << endl;
// cout << "\n----------------------------------\n";
}
void nextvalue(int k)
{
while (1)
{
x[k] = (x[k] + 1) % (m + 1);
if (x[k] == 0)
{
return;
}
int i = 0;
for (i = 0; i < adj[k].size(); i++)
{
if (x[k] == x[adj[k][i]])
{
break;
}
}
if (i == adj[k].size())
{
return;
}
}
}
void mcoloring(int k)
{
while (k <= n)
{
// cout<<"k = "<<k<<endl;
nextvalue(k);
if (x[k] == 0)
{
return;
}
if (k == n)
{
counter++;
print(x);
}
else
{
mcoloring(k + 1);
}
// cout<<"returned for k = "<<k<<endl;
}
}
int main()
{
cin >> m;
int e;
cin >> n >> e;
adj.resize(n + 1, vector<int>());
x.resize(n + 1);
while (e--)
{
int u, v;
cin >> u >> v;
adj[u].push_back(v);
adj[v].push_back(u);
}
mcoloring(1);
cout << "Total number of solutions : " << counter << endl;
return 0;
}