Can be converted to a coloring model
DFS + four-color theorem
1 # include <cstdio> 2 # include <memory. h> 3 int N, num; 4 int d [100] [100]; 5 Int C [100]; 6 7 bool OK (INT step) 8 {9 for (INT I = 0; I <n; I ++) 10 if (d [STEP] [I] = 1 & C [STEP] = C [I]) return false; 11 return true; 12} 13 14 bool DFS (INT num, int step) 15 {16 if (Step> = N) return true; 17 for (INT I = 1; I <= num; I ++) 18 {19 C [STEP] = I; 20 if (OK (STEP) & DFS (Num, step + 1) return true; 21 22 C [STEP] = 0; // restore 23} 24 return false; 25} 26 27 int main () 28 {29 freopen ("input.txt", "r", stdin ); 30 While (scanf ("% d \ n", & N) 31 {32 char ch; 33 memset (D, 0, sizeof (d )); 34 For (INT I = 0; I <n; I ++) 35 {36 scanf ("% C:", & Ch ); 37 while (scanf ("% C", & Ch) & Ch! = '\ N') 38 D [I] [CH-'a'] = d [CH-'a'] [I] = 1; 39} 40 41 memset (C, 0, sizeof (c); 42 int num = 0; 43 for (num = 1; num <4; num ++) 44 If (DFS (Num, 0) break; 45 46 If (num = 1) printf ("1 channel needed. \ n "); 47 else printf (" % d channels needed. \ n ", num); 48 49} 50 return 0; 51}