-
Notifications
You must be signed in to change notification settings - Fork 0
/
m-coloring.cpp
63 lines (53 loc) · 1.37 KB
/
m-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
#include <bits/stdc++.h>
using namespace std;
bool isSafe(int u, int c, int n, bool graph[101][101], int color[]){
for(int i = 0; i < n; i++){
if(graph[u][i] == 1 && color[i] == c)
return false;
}
return true;
}
bool helper(int u, int n, int m, bool graph[101][101], int color[]){
if(u == n) return true;
for(int i = 1; i <= m; i++){
if(isSafe(u, i, n, graph, color)){
//color the adjacent node with next color
color[u] = i;
//recur for next node
if(helper(u+1, n, m, graph, color)) return true;
//if this doesn't give answer backtracking
color[u] = 0;
}
}
return false;
}
//Function to determine if graph can be coloured with at most M colours such
//that no two adjacent vertices of graph are coloured with same colour.
bool graphColoring(bool graph[101][101], int m, int v)
{
int color[v] = {};
return helper(0, v, m, graph, color);
}
int main(){
int n, m, e;
cin >> n >> m >> e;
bool graph[101][101];
for(int i = 0; i < e; i++){
int f,s;
cin >> f >> s;
graph[f-1][s-1] = 1;
graph[s-1][f-1] = 1;
}
for(int i=0; i < n; i++){
cout << "{";
for(int j = 0; j < n; j++){
cout << graph[i][j] << ", ";
} cout << "},\n";
}
if(graphColoring(graph, m, n)){
cout << "YES\n";
} else{
cout << "NO\n";
}
return 0;
}