poj 1129(dfs+图的四色定理)
来源:互联网 发布:宋慈 知乎 编辑:程序博客网 时间:2024/06/07 12:06
题目链接:http://poj.org/problem?id=1129
思路:根据图的四色定理,最多四种颜色就能满足题意,使得相邻的两部分颜色不同。而最多又只有26个点,因此直接dfs即可。
1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include<algorithm> 5 using namespace std; 6 7 bool map[33][33]; 8 int mark[33]; 9 char str[33];10 int n,ans;11 12 bool Judge(int x,int color)13 {14 for(int i=0;i<n;i++){15 if(i!=x&&map[x][i]&&mark[i]==color)16 return 0;17 }18 return 1;19 }20 21 void dfs(int pos)22 {23 if(pos==n){24 ans=min(ans,*max_element(mark,mark+n));25 return ;26 }27 for(int i=pos;i<n;i++){28 for(int j=1;j<=4;j++){29 if(Judge(i,j)){30 mark[i]=j;31 dfs(i+1);32 }33 }34 }35 }36 37 38 int main()39 {40 while(~scanf("%d",&n)&&n){41 memset(map,false,sizeof(map));42 for(int i=0;i<n;i++){43 scanf("%s",str);44 for(int j=2;j<strlen(str);j++){45 map[i][str[j]-'A']=true;46 map[str[j]-'A'][i]=true;47 }48 }49 memset(mark,0,sizeof(mark));50 ans=4;51 mark[0]=1;52 dfs(1);53 if(ans==1){54 printf("1 channel needed.\n");55 }else 56 printf("%d channels needed.\n",ans);57 }58 return 0;59 }
0 0
- poj 1129(dfs+图的四色定理)
- poj 1129(ID-DFS四色定理)
- poj 1129(DFS+四色定理)
- POJ 1129 图的四色定理
- POJ 1129 Channel Allocation dfs(四色定理)
- POJ 1129 Channel Allocation (四色定理+DFS)
- POJ-1129-Channel Allocation-dfs搜索+四色定理
- POJ 1129 Channel Allocation DFS+四色定理
- POJ 1129 Channel Allocation(DFS + 四色定理)
- [Poj 1129] Channel Allocation DFS+四色定理剪支
- POJ Channel Allocation 1129(dfs+四色定理)
- POJ 1129 Channel Allocation (dfs+四色定理)
- poj 1129 四色定理
- poj 1129 Channel Allocation (四色定理)
- poj-1129-Channel Allocation-四色定理
- POJ 1129 四色定理+spfa
- hrbust 1614 小z的地图【四色定理+Dfs】
- HDU 5113我不是四色定理 dfs
- hdu 3681(bfs+二分+状压dp判断)
- poj 2662(Dijkstra+记忆化)
- Android实现双进程守护
- poj 3414(简单bfs)
- 分享几篇关于gcc和gdb的文章
- poj 1129(dfs+图的四色定理)
- poj 2531(dfs)
- 一个tcp校验码的错误。
- poj 1088(记忆化搜索)
- hdu 4294(bfs)
- hdu 2821(dfs)
- 为什么long和int都是4字节
- hdu 2437(dfs)
- hdu 3085(双向bfs)