题意:相邻的点着不同的颜色 问一个图最少能用多少种颜色 着色
暴力搜索
虽然没有用四色定理剪枝 但还是介绍一下四色定理
四色定理 最少用4种颜色就能将任意一个图按上述要求染色
#include <cstdio>
#include <iostream>
#include <queue>
#include <cstring>
#include <algorithm>
#define sf scanf
#define pf printf
using namespace std;
const int maxn = 30;
int Adj[maxn][maxn];
int n;
int color[maxn];
bool vis[maxn];
char str[30];
int main(){
while( sf("%d",&n) && n ){
memset(Adj,0,sizeof(Adj));
for(int i = 0;i < n;++i){
sf("%s",str);
int u = str[0] - 'A';
int len = strlen(str);
for(int j = 2;j < len;++j){
Adj[u][str[j] - 'A'] = 1;
}
}
memset(color,0,sizeof(color));
int max_color = 0;
color[0] = ++max_color;
for(int i = 1;i < n;++i){
memset(vis,0,sizeof(vis));
for(int j = 0;j < n;++j){
if(i == j) continue;
if(Adj[i][j]) vis[color[j]] = 1;
}
for(int k = 1;k <= max_color;++k){
if(!vis[k]){
color[i] = k;
break;
}
}
if(color[i] == 0) color[i] = ++max_color;
}
pf("%d channel%s needed.\n",max_color,max_color == 1 ? "" : "s");
}
return 0;
}