这题数据出现了很大问题,不知道标程是怎样的。
网上大部分版本的AC代码,虽然能过,但是仍是错的。
类似这组数据,基本都过不了
6
A:BEF
B:AC
C:BD
D:CEF
E:ADF
F:ADE
out
3
没做过染色问题,看到题目一开始以为是贪心,贪心正是处理不了这样的数据;贪心的话会先把AC放到一组,再把B D 放一组,E, F 各一组,答案是4.实际上可以 是 A D , B E ,C F各一组只要 3 个频道。
这题确实也不难,n<=26,各种姿势爆搜应该都能过。
用dfs的话,需要遍历所有的合理解,再取最优。比如上组数据,3、4都满足,但是取更小的3。网上大部分dfs的做法都是没有取优,找到一个解就直接退出深搜了
比如这个:http://blog.youkuaiyun.com/xcszbdnl/article/details/8008044
也有考虑到这个问题但是没有处理正确的,只不过由于数据水过了:
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
#define M 26
int n, ans, color[M];
bool map[M][M], isFind;
bool ok(int x, int c) { //判断是否存在相邻节点颜色相同
for (int i = 0; i < n; i++)
if (map[x][i] && c == color[i])
return false;
return true;
}
void DFS(int id, int total) { //当前搜索到下标为id的节点,此时总共用的色彩数为total
if (isFind) return;
if (id >= n) { isFind = true; return; } //当所有节点都被着色后,返回
for (int i = 1; i <= total; i++) {
if (ok(id, i)) {
color[id] = i;
DFS(id+1, total);
color[id] = 0;
}
}
if (!isFind) { //当用total种颜色无法完成时,则增加一种颜色进行着色
ans++;
DFS(id, total+1); //!!!这样处理是不正确的,在回溯的时候,isFind总会=0;
}
}
int main()
{
int i, j;
char s[M];
while (scanf ("%d", &n) && n) {
getchar();
memset (map, false, sizeof (map));
memset (color, 0, sizeof (color));
for (i = 0; i < n; i++) {
gets(s);
for (j = 2; s[j] != '\0'; j++)
map[s[0]-'A'][s[j]-'A'] = true;
}
isFind = false;
ans = 1;
DFS(0, 1);
if (ans == 1)
printf ("1 channel needed.\n");
else printf ("%d channels needed.\n", ans);
}
return 0;
}
稍微修改一下就可以了
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
#define M 26
int n, ans, color[M];
bool map[M][M], isFind;
bool ok(int x, int c) { //判断是否存在相邻节点颜色相同
for (int i = 0; i < n; i++)
if (map[x][i] && c == color[i])
return false;
return true;
}
void DFS(int id, int total) { //当前搜索到下标为id的节点,此时总共用的色彩数为total
if (isFind) return;
if (id >= n) {
isFind = true;
//for(int i=0;i<n;i++)
// printf("%d ",color[i]);
// printf("\n");
return;
}
for (int i = 1; i <= total; i++) {
if (ok(id, i)) {
color[id] = i;
DFS(id+1, total);
color[id] = 0;
}
}
}
int main()
{
int i, j;
char s[M];
while (scanf ("%d", &n) && n) {
getchar();
memset (map, false, sizeof (map));
for (i = 0; i < n; i++) {
gets(s);
for (j = 2; s[j] != '\0'; j++)
map[s[0]-'A'][s[j]-'A'] = map[s[j]-'A'][s[0]-'A'] = true;
}
isFind = false;
ans = 1;
while(ans<=4) //四色定理
{
DFS(0, ans);
if(isFind)
break;
else
{
memset (color, 0, sizeof (color));
DFS(0, ++ans);
}
}
if (ans == 1)
printf ("1 channel needed.\n");
else printf ("%d channels needed.\n", ans);
}
return 0;
}
/*
在这里利用了四色定理,枚举了总的颜色数量
如果不知道这个东西,或者没有发现这个规律
可以直接在dfs里面比较,当找到合理解时
ans=min(ans,minc)
//四色定理:将平面任意地细分为不相重迭的区域,每一个区域总可以用1,2,3,4这四个数字之一来标记,而不会使相邻的两个区域得到相同的数字,即至多存在四个两两相邻的区域.
其实如果知道了最多只要4中颜色,爆搜就可以了,但是再吐槽一句,网上的爆搜好多方法也是错的;只看到这篇博客写的比较好:http://yuanlanxiaup.iteye.com/blog/1327345
#include <iostream>
#include <algorithm>
#include <string>
#define N 30
#define f(i,a,b) for(i=a;i<b;i++)//简化代码
using namespace std;
//图论中的染色问题,使得相邻两个节点颜色不同最少需要几种颜色
//搜索就是暴力的枚举,记住
bool maps[N][N];
int n,i,j,k,q;
char ch[N];
void solve(){
//如果出现四个区域,每个区域都和其他三个邻接,试探C42次,用四种颜色
f(i,0,n)
f(j,0,n)
f(k,0,n)
f(q,0,n)
if(maps[i][j]&&maps[i][k]
&&maps[i][q]&&maps[j][k]
&&maps[j][q]&&maps[k][q])
{
cout<<"4 channels needed."<<endl;
return;
}
//如果有三个区域两两邻接,试探C32次,要用三种颜色
f(i,0,n)
f(j,0,n)
f(k,0,n)
if(maps[i][j]&& maps[i][k] && maps[j][k])
{
cout<<"3 channels needed."<<endl;
return;
}
//如果存在两个区域邻接的情况,试探C22次,要用2种颜色
f(i,0,n)
f(j,0,n)
if(maps[i][j])
{
cout << "2 channels needed." << endl;
return;
}
//没有区域邻接
cout << "1 channel needed." << endl;
}
int main(){
while(cin>>n,n!=0){
memset(maps,0,sizeof(maps));
f(i,0,n){
cin>>ch;
f(j,2,strlen(ch)){
maps[ch[0]-'A'][ch[j]-'A'] = 1;
maps[ch[j]-'A'][ch[0]-'A'] = 1;
}
}
solve();
}
return 0;
}