-
Notifications
You must be signed in to change notification settings - Fork 6
/
m-colouring.cpp
56 lines (47 loc) · 1.03 KB
/
m-colouring.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
/**
* Name : m-Coloring problem
* this variation is to print maximum node with a spacific colour here 2
*/
///ncol is number of colours
vector<int>ed[MX];
int col[MX];
int n,m,tmp[MX],in,ncol=2;
bool safe(int pos,int cl){
int x;
for(int i=0;i<ed[pos].size();i++){
/// here restricted adjecent colour is black||1
x = ed[pos][i];
if(col[x]==cl&&cl==1) return 0;
}
return 1;
}
void backtrack(int pos ,int cnt){
if(pos>n){
if(cnt>in){
in = 0;
for(int i=1;i<=n;i++){
if(col[i]==1) tmp[++in] = i;
}
}
return;
}
int i,cn=0;
///trying all colour in a spacific node
for(i=1;i<=ncol;i++){
if(i==1) cn = 1;
else cn = 0;
if(safe(pos,i)){
col[pos] = i;
backtrack(pos+1,cnt+cn);
col[pos] = 0;
}
}
}
void free(){
in = 0;
for(int i=0;i<105;i++){
col[i] = tmp[i] = 0;
ed[i].clear();
}
}
///calling with : backtrack(0,0);