题意:给无向图染色, 相邻顶点不能为同色,要求最少颜色数。
四色定理是一个著名的数学定理,通俗的说法是:每个平面地图都可以用不多于四种颜色来染色,而且没有两个邻接的区域颜色相同。
#include <iostream>
#include <cstdio>
#include <string>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <vector>
#include <list>
#include <deque>
#include <queue>
#include <cctype>
#include <map>
#include <set>
#include <bitset>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iomanip>
#include <cstdlib>
#include <ctime>
#include <cassert>
#include <limits>
#include <fstream>
using namespace std;
#define mem(A, X) memset(A, X, sizeof A)
#define pb(x) push_back(x)
#define mp(x,y) make_pair((x),(y))
#define rep1(i,l,u) for(int (i)=(int)(l);(i)<(int)(u);++(i))
#define rep2(i,l,u) for(int (i)=(int)(l);(i)<=(int)(u);++(i))
#define min3(a,b,c) min(a,min(b,c))
#define max3(a,b,c) max(a,max(b,c))
#define dbg(a) cout << a << endl;
typedef long long int64;
int gcd(const int64 &a, const int64 &b) {return b == 0 ? a : gcd(b, a % b);}
int64 int64pow(int64 a, int64 b){if(b == 0) return 1;int64 t = int64pow(a, b / 2);if(b % 2) return t * t * a;return t * t;}
const int inf = 1 << 29;
const double eps = 1e-8;
const double pi = acos(-1.0);
int graph[30][30], used[30];
int n;
string s;
bool flag;
bool dfs(int id, int color_cnt)
{
rep1(i, 0, color_cnt) {
used[id] = i;
flag = true;
rep1(j, 0, id) {
if (graph[id][j] && used[id] == used[j]) {
flag = false;
break;
}
}
if (flag && (id == n - 1 || dfs(id + 1, color_cnt)))
return true;
}
return false;
}
void solve()
{
mem(graph, 0);
mem(used, 0);
bool one = true;
rep1(i, 0, n) {
cin >> s;
rep1(j, 2, (int)s.size()) {
graph[i][s[j] - 'A'] = 1;
graph[s[j] - 'A'][i] = 1;
one = false;
}
}
if (one)
cout << "1 channel needed." << endl;
else if (dfs(0, 2))
cout << "2 channels needed."<< endl;
else if (dfs(0, 3))
cout << "3 channels needed."<< endl;
else
cout << "4 channels needed."<< endl;
}
int main()
{
while (cin >> n && n) {
solve();
}
return 0;
}