POJ 1129-Channel Allocation(四色定理+迭代深搜)
2014-11-23 22:57
489 查看
题目链接:传送门
题意:n个信号站,给出连接情况,要用信号覆盖所有信号站,要求相连的信号站不能用同一个信号。
等价问题==无向图染色==四色定理(每个平面地图都可以只用四种颜色来染色,而且没有两个邻接的区域颜色相同。已证明)
思路:深搜一条路(枚举颜色,判断当前点用已有的颜色能不能染,如不能则加一种颜色,判断强判就行了),搜到头答案就出来了。。然后返回就可以了
注意单复数。。
题意:n个信号站,给出连接情况,要用信号覆盖所有信号站,要求相连的信号站不能用同一个信号。
等价问题==无向图染色==四色定理(每个平面地图都可以只用四种颜色来染色,而且没有两个邻接的区域颜色相同。已证明)
思路:深搜一条路(枚举颜色,判断当前点用已有的颜色能不能染,如不能则加一种颜色,判断强判就行了),搜到头答案就出来了。。然后返回就可以了
注意单复数。。
#include <algorithm> #include <iostream> #include <cstring> #include <cstdlib> #include <string> #include <cctype> #include <vector> #include <cstdio> #include <cmath> #include <queue> #include <stack> #include <map> #include <set> #define maxn 360 #define _ll __int64 #define ll long long #define INF 0x3f3f3f3f #define Mod 1000000007 #define pp pair<int,int> #define ull unsigned long long #define max(x,y) ( ((x) > (y)) ? (x) : (y) ) #define min(x,y) ( ((x) > (y)) ? (y) : (x) ) using namespace std; int n, ans, ok, vis[28]; bool ma[28][28]; bool check(int u, int sb) { for (int i = 1; i <= n; i++) if (ma[u][i] && vis[i] == sb) { return 0; } return 1; } void dfs(int u, int s) { if (ok) { return ; } if (u == n + 1) { ans = s; ok = 1; return ; } for (int k = 1; k <= s; k++) { if (check(u, k)) { vis[u] = k; dfs(u + 1, s); } } vis[u] = ++s; dfs(u + 1, s); } int main() { char s[38]; while (scanf("%d", &n) != EOF && n) { memset(ma, 0, sizeof(ma)); memset(vis, 0, sizeof(vis)); getchar(); for (int i = 1; i <= n; i++) { scanf("%s", s); int len = strlen(s); for (int j = 2; j <= len - 1; j++) { ma[i][s[j] - 'A' + 1] = 1; ma[s[j] - 'A' + 1][i] = 1; } } ok = 0; dfs(1, 1); if (ans == 1) { puts("1 channel needed."); } else { printf("%d channels needed.\n", ans); } } return 0; }
相关文章推荐
- POJ 1129 图的四色定理
- poj 1129 四色定理
- poj 1129 Channel Allocation(四色定理+着色问题)
- poj 1129(ID-DFS四色定理)
- POJ 1129:Channel Allocation 四色定理+暴力搜索
- POJ 1129:Channel Allocation 四色定理+暴力搜索
- POJ 1129 Channel Allocation DFS+四色问题优化
- POJ1129Channel Allocation[迭代加深搜索 四色定理]
- poj 1129 DFS+四色问题 Channel Allocation
- POJ 1129 四色问题 暴力搜索
- 迭代加深搜索 POJ 1129 Channel Allocation
- poj 1006中国剩余定理模板
- POJ-1129 Channel Allocation DFS搜索
- POJ 1129 Channel Allocation(染色)
- poj 2891Strange Way to Express Integers 中国剩余定理(不互质) 数据没读完就结束了程序居然是runtime error。
- POJ 2409 Let it Bead(Polya定理)
- poj 1286 Necklace of Beads【polya定理+burnside引理】
- POJ 1265 Area(PICK定理)
- poj 1129 搜索
- [Poj 1129] Channel Allocation DFS+四色定理剪支