【题意】:给出一个平面图,问最少用多少种颜色来染色能够使得图中相邻结点颜色不同。
【题解】:平面图染色,四色定理。
直接dfs即可。
【代码】:
1 #include "iostream"
2 #include "cstdio"
3 #include "cstring"
4 #include "algorithm"
5 #include "vector"
6 #include "queue"
7 #include "cmath"
8 #include "string"
9 #include "cctype"
10 #include "map"
11 #include "iomanip"
12 using namespace std;
13 #define pb push_back
14 #define mp make_pair
15 #define fi first
16 #define se second
17 #define lc(x) (x << 1)
18 #define rc(x) (x << 1 | 1)
19 #define lowbit(x) (x & (-x))
20 #define ll long long
21 #define maxn 30
22 bool maz[maxn][maxn], visit[maxn];
23 int color[maxn];
24 int n;
25 char a, ch;
26
27 int dfs(int u, int cnt) {
28 memset(visit, false, sizeof(visit));
29 if(u == n) return true;
30 for(int i = 0; i < n; i++) {
31 if(color[i] != -1 && maz[u][i]) {
32 visit[color[i]] = true;
33 }
34 }
35 for(int i = 0; i < cnt; i++) {
36 if(!visit[i]) {
37 color[u] = i;
38 if(dfs(u+1, cnt)) return true;
39 }
40 }
41 return false;
42 }
43
44 void solve() {
45 int ans = 5;
46 for(int i = 1; i < 5; i++) {
47 memset(color, -1, sizeof(color));
48 if(dfs(0, i)) {
49 ans = i;
50 break;
51 }
52 }
53 if(ans == 1) printf("1 channel needed.\n");
54 else printf("%d channels needed.\n", ans);
55 }
56
57 int main() {
58 while(~scanf("%d", &n), n) {
59 getchar();
60 memset(maz, false, sizeof(maz));
61 for(int i = 0; i < n; i++) {
62 scanf("%c:", &a);
63 while((ch = getchar()) != 10) {
64 maz[a-'A'][ch-'A'] = maz[ch-'A'][a-'A'] = true;
65 }
66 }
67 solve();
68 }
69 return 0;
70 }
71