博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
poj1129
阅读量:7081 次
发布时间:2019-06-28

本文共 1726 字,大约阅读时间需要 5 分钟。

题意:平面内最多26个点,给出一些点与点之间的矛盾关系,问最少使用多少颜色才能给这些点染色,并保证矛盾点之间不同色。

分析:深搜,对于每个点先尝试已经给别的点染过的颜色,如果不行再尝试染新的颜色,注意新的颜色只需要尝试一次即可,因为各种新的颜色对于当前状态来说是等价的。

View Code
#include 
#include
#include
#include
using namespace std;#define maxn 30#define maxm maxn * maxn#define maxl maxn + 2#define inf 0x3f3f3f3fstruct Edge{ int v, next;}edge[maxm];int head[maxn];int color[maxn];int n;int edge_cnt;int ans;void addedge(int a, int b){ edge[edge_cnt].v = b; edge[edge_cnt].next = head[a]; head[a] = edge_cnt++;}void input(){ edge_cnt = 0; memset(head, -1, sizeof(head)); char st[maxl]; for (int i = 0; i < n; i++) { scanf("%s", st); int len = strlen(st); for (int j = 2; j < len; j++) { addedge(i, st[j] - 'A'); addedge(st[j] - 'A', i); } }}bool ok(int a){ for (int i = head[a]; ~i; i = edge[i].next) if (color[a] == color[edge[i].v]) return false; return true;}void dfs(int color_num, int a){ if (a == n) { ans = min(ans, color_num); return; } for (int i = 0; i < color_num; i++) { color[a] = i; if (ok(a)) dfs(color_num, a + 1); } color[a] = color_num; dfs(color_num + 1, a + 1); color[a] = -1;}int main(){ //freopen("t.txt", "r", stdin); while (scanf("%d", &n), n) { input(); ans = inf; memset(color, -1, sizeof(color)); dfs(0, 0); if (ans == 1) printf("1 channel needed.\n"); else printf("%d channels needed.\n", ans); } return 0;}

 

转载于:https://www.cnblogs.com/rainydays/archive/2013/01/11/2856638.html

你可能感兴趣的文章
你不知道的Spring配置文件
查看>>
Spark源码分析之Spark-submit和Spark-class
查看>>
SOA系列之基本特性
查看>>
js中的"=="和equals()以及is()三者的区别
查看>>
谨慎注意WebBrowser控件的DocumentCompleted事件
查看>>
回头再说 005 --温暖的文字和音乐
查看>>
C#进行Visio二次开发之电气线路停电分析逻辑
查看>>
简便无刷新文件上传系统
查看>>
匆匆的记录一下,生日快乐!
查看>>
[链接]实现GEF程序中的剪切/复制/粘贴功能
查看>>
lucene 的评分机制
查看>>
Backup Volume 操作 - 每天5分钟玩转 OpenStack(59)
查看>>
JavaWeb之tomcat安装、配置与使用(一)
查看>>
SpringMVC Controller 返回值的可选类型
查看>>
kbmmw 5.03 发布
查看>>
iOS - App 与外设间的通信方式
查看>>
13.7. Device Management
查看>>
Hibernate详细教程
查看>>
144.2. tcpdump - A powerful tool for network monitoring and data acquisition
查看>>
查看ecshop广告位对应的广告详细信息
查看>>