poj 1129(dfs+图的四色定理)
2024-09-02 15:15:16
题目链接:http://poj.org/problem?id=1129
思路:根据图的四色定理,最多四种颜色就能满足题意,使得相邻的两部分颜色不同。而最多又只有26个点,因此直接dfs即可。
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std; bool map[][];
int mark[];
char str[];
int n,ans; bool Judge(int x,int color)
{
for(int i=;i<n;i++){
if(i!=x&&map[x][i]&&mark[i]==color)
return ;
}
return ;
} void dfs(int pos)
{
if(pos==n){
ans=min(ans,*max_element(mark,mark+n));
return ;
}
for(int i=pos;i<n;i++){
for(int j=;j<=;j++){
if(Judge(i,j)){
mark[i]=j;
dfs(i+);
}
}
}
} int main()
{
while(~scanf("%d",&n)&&n){
memset(map,false,sizeof(map));
for(int i=;i<n;i++){
scanf("%s",str);
for(int j=;j<strlen(str);j++){
map[i][str[j]-'A']=true;
map[str[j]-'A'][i]=true;
}
}
memset(mark,,sizeof(mark));
ans=;
mark[]=;
dfs();
if(ans==){
printf("1 channel needed.\n");
}else
printf("%d channels needed.\n",ans);
}
return ;
}
最新文章
- EasyUI中那些不容易被发现的坑——EasyUI重复请求2次的问题
- DoTween 应用设置
- python PIL Image模块
- ubuntu下crontab编辑方法的设定
- 使用.NET FrameWork获取CPU,内存使用率以及磁盘空间
- SOA_环境安装系列2_Oracle RCU安装和环境搭建(案例)
- codeforces 590C C. Three States(bfs+连通块之间的最短距离)
- IOS 中得runloop 详细解释
- Laravel5中集成Jasig cas统一认证系统
- 设计模式之 - 外观模式 (Facade design pattern)
- Hibernate入门(三)
- IIS最小配置
- 基于WebSocket 私聊、ws_session、httpsession
- 泛微云桥e-birdge之金蝶云之家集成配置手册
- 白话js this指向问题
- hdfs平衡分布
- [Cpp primer] Library string Type
- python:while 语句的使用方法
- react.js学习之路三
- ant-design-pro使用服务器数据接口代理配置