关于平面图着色

来源:互联网 发布:中华诗词软件 编辑:程序博客网 时间:2024/05/01 19:39
原文地址
poj 1129

Channel Allocation
Time Limit: 1000MS Memory Limit: 10000KTotal Submissions: 12777 Accepted: 6547

Description

When a radio station is broadcasting over a very large area, repeaters are used to retransmit the signal so that every receiver has a strong signal. However, the channels used by each repeater must be carefully chosen so that nearby repeaters do not interfere with one another. This condition is satisfied if adjacent repeaters use different channels. 

Since the radio frequency spectrum is a precious resource, the number of channels required by a given network of repeaters should be minimised. You have to write a program that reads in a description of a repeater network and determines the minimum number of channels required.

Input

The input consists of a number of maps of repeater networks. Each map begins with a line containing the number of repeaters. This is between 1 and 26, and the repeaters are referred to by consecutive upper-case letters of the alphabet starting with A. For example, ten repeaters would have the names A,B,C,...,I and J. A network with zero repeaters indicates the end of input. 

Following the number of repeaters is a list of adjacency relationships. Each line has the form: 

A:BCDH 

which indicates that the repeaters B, C, D and H are adjacent to the repeater A. The first line describes those adjacent to repeater A, the second those adjacent to B, and so on for all of the repeaters. If a repeater is not adjacent to any other, its line has the form 

A: 

The repeaters are listed in alphabetical order. 

Note that the adjacency is a symmetric relationship; if A is adjacent to B, then B is necessarily adjacent to A. Also, since the repeaters lie in a plane, the graph formed by connecting adjacent repeaters does not have any line segments that cross. 

Output

For each map (except the final one with no repeaters), print a line containing the minumum number of channels needed so that no adjacent channels interfere. The sample output shows the format of this line. Take care that channels is in the singular form when only one channel is required.

Sample Input

2A:B:4A:BCB:ACDC:ABDD:BC4A:BCDB:ACDC:ABDD:ABC0

Sample Output

1 channel needed.3 channels needed.4 channels needed. 

传送门:题解 

此题极其特别,因为是平面图,我们可以利用它的对偶图的面着色来处理,由于四色猜想已被计算机证明(尽管很多数学家不承认,但结论确实是对的),如果你也投靠那些不承认的数学家,那也没有问题,五色定理是已经被证明的,因此可以利用这个来剪枝,其实剪不剪复杂度都是O(n^2),但是对于n很大时,剪枝要快得多,这里数据量较小,就不剪枝了,剪枝也很方便,改几处代码就可以了,留给你们思考。

代码(未剪枝):

[cpp] view plaincopy在CODE上查看代码片派生到我的代码片
  1. #include<cstdio>  
  2. #include<iostream>  
  3. #include<cstring>  
  4. #define Maxn 30  
  5. using namespace std;  
  6.   
  7. char s[Maxn];  
  8. int adj[Maxn][Maxn];  
  9. int vis[Maxn],color[Maxn];  
  10. int dye(int n){ //染色范围[0,n-1]  
  11.     int ans=-1;  
  12.     memset(color,-1,sizeof color);  
  13.     for(int i=0;i<n;i++){  
  14.         memset(vis,0,sizeof vis);  
  15.         for(int j=0;j<n;j++)  
  16.             if(adj[i][j]&&color[j]!=-1)  
  17.                 vis[color[j]]=1;  
  18.         for(int j=0;j<n;j++)  
  19.             if(!vis[j]){  
  20.                 color[i]=j;  
  21.                 ans=max(ans,j);  
  22.                 break;  
  23.             }  
  24.     }  
  25.     return ans+1;  
  26. }  
  27. int main()  
  28. {  
  29.     int n;  
  30.     while(cin>>n,n){  
  31.         memset(adj,0,sizeof adj);  
  32.         for(int i=0;i<n;i++){  
  33.             cin>>s;  
  34.             for(int j=2;s[j];j++)  
  35.                 adj[i][s[j]-'A']=adj[s[j]-'A'][i]=1;  
  36.         }  
  37.         int ans=dye(n);  
  38.         if(ans==1) printf("1 channel needed.\n");  
  39.         else printf("%d channels needed.\n",ans);  
  40.     }  
  41.     return 0;  
  42. }  

特别补上:

poj数据太弱!

这题没那么容易,上面的解法是错误的,特此纠正,顺序染色得到的不一定是最优解。

错误样例:

5
A:D
B:C
C:BDE
D:AC
E:C

正解应该是2,程序跑出来是3.


还有一种解法就是dfs贪心染色,这种做法我也写了程序,poj上反而WA了,说来奇怪,貌似该方法看上去是对的。但还是不要想当然,可以负责任地告诉你,贪心思想也是错的,得到的也不一定是最优解。

错误样例:

6
A:BEF
B:AC
C:BD
D:CEF
E:ADF
F:ADE

正解应该是3,程序跑出来是4.

正确的要怎么做?要我说就是纯暴力(把所有情况都试探一下),当然利用四色定理将复杂度优化下来,我优化到了O(3^n)。

正确的代码:

[cpp] view plaincopy
  1. #include<cstdio>  
  2. #include<iostream>  
  3. #include<cstring>  
  4. #include<algorithm>  
  5. #define Maxn 30  
  6. using namespace std;  
  7.   
  8. char s[Maxn];  
  9. int adj[Maxn][Maxn];  
  10. int color[Maxn];  
  11. int n;  
  12. int ans;  
  13. bool check(int u,int c){  
  14.     for(int v=0;v<u;v++)  
  15.         if(adj[u][v]&&color[v]==c)  
  16.             return false;  
  17.     return true;  
  18. }  
  19. void dfs(int d){  
  20.     if(d==n){  
  21.         ans=min(ans,*max_element(color,color+n));  
  22.         return;  
  23.     }  
  24.     for(int c=1;c<4;c++)  
  25.         if(check(d,c)){  
  26.             color[d]=c;  
  27.             dfs(d+1);  
  28.         }  
  29. }  
  30. void solve(){  
  31.     color[0]=1;  
  32.     ans=4;  
  33.     dfs(1);  
  34. }  
  35. int main()  
  36. {  
  37.     while(cin>>n,n){  
  38.         memset(adj,0,sizeof adj);  
  39.         for(int i=0;i<n;i++){  
  40.             cin>>s;  
  41.             for(int j=2;s[j];j++)  
  42.                 adj[i][s[j]-'A']=adj[s[j]-'A'][i]=1;  
  43.         }  
  44.         solve();  
  45.         if(ans==1) printf("1 channel needed.\n");  
  46.         else printf("%d channels needed.\n",ans);  
  47.     }  
  48.     return 0;  
  49. }  
0 0