#include <iostream>
#include <stdio.h>
#include <string>
#include <cmath>
#include <string.h>
#include <set>
#include <vector>
#include <algorithm>
using namespace std;
set<int> S;
int road[30][30];
int channel[30];
int n;
string s;
int res;
void dfs(int x, int ch){
if (x == n){
// 全部放置完成
res = min(res, ch-1);
return;
}
// 首先查找出所有可以放的东西
bool flag;
bool use = true;
for (int i = 1; i < ch; ++i) {
flag = true;
for (int j = 0; j < x; ++j) {
if (road[x][j]&&channel[j] == i){
// 说明这个数字不能用了
flag = false;
break;
}
}
if (flag){
// 说明这个数字可以用
use = false;
channel[x] = i;
dfs(x+1, ch);
}
}
// 如果一个都不能用,把这个ch用掉
if (use){
channel[x] = ch;
dfs(x+1, ch+1);
}
}
int main(){
// freopen("../in.txt", "r", stdin);
while (cin>>n&&n){
memset(channel, 0, sizeof(channel));
memset(road, 0, sizeof(road));
int ix = 1;
for (int i = 0; i < n; ++i) {
cin>>s;
if (s.length() == 2) continue;
s = s.substr(2);
for (int j = 0; j < s.length(); ++j) {
road[i][s[j]-'A'] = 1;
}
}
// 数据读取完成
// test
// for (int i = 0; i < n; ++i) {
// for (int j = 0; j < n; ++j) {
// cout<<road[i][j]<<" ";
// }
// cout<<endl;
// }
channel[0] = 1;
res = 0x3f3f3f3f;
dfs(1, 2);
cout<<res<<" ";
printf(res>1?"channels":"channel");
cout<<" "<<"needed."<<endl;
}
}
POJ 1129
最新推荐文章于 2018-11-20 22:13:35 发布