POJ 1129 Channel Allocation 平面图染色问题
2012-01-23 19:13
344 查看
这题大意就是,给出一个平面图,对顶点进行染色, 相邻顶点是不能用同一种颜色。
由四色定理,我们知道任何平面图的色数是不超过4的,所以运用这个进行暴搜,当然这题的数据不强,DFS或者贪心搞都能过
由四色定理,我们知道任何平面图的色数是不超过4的,所以运用这个进行暴搜,当然这题的数据不强,DFS或者贪心搞都能过
/* ID: sdj22251 PROG: subset LANG: C++ */ #include <iostream> #include <vector> #include <list> #include <map> #include <set> #include <deque> #include <queue> #include <stack> #include <bitset> #include <algorithm> #include <functional> #include <numeric> #include <utility> #include <sstream> #include <iomanip> #include <cstdio> #include <cmath> #include <cstdlib> #include <cctype> #include <string> #include <cstring> #include <cmath> #include <ctime> #define MAXN 1005 #define INF 100000000 #define eps 1e-7 #define PI 3.1415926535898 using namespace std; int color[27], n; vector<int>v[27]; bool ok(int u) { int x = v[u].size(); for(int i = 0; i < x; i++) { if(color[v[u][i]] == color[u]) return false; } return true; } bool dfs(int u, int cnum) { if(u >= n || cnum == 4) return true; else { for(int i = 1; i <= cnum; i++) { color[u] = i; if(ok(u)) { if(dfs(u + 1, cnum)) return true; } color[u] = 0; } } return false; } int main() { char s[30]; while(scanf("%d", &n) != EOF && n) { memset(color, 0, sizeof(color)); for(int i = 0; i < n; i++) v[i].clear(); for(int i = 0; i < n; i++) { scanf("%s", s); int len = strlen(s); for(int j = 2; j < len; j++) { v[i].push_back(s[j] - 'A'); } } for(int i = 1; i <= 4; i++) { if(dfs(0, i)) { if(i == 1) printf("1 channel needed.\n"); else printf("%d channels needed.\n",i); break; } } } return 0; }
相关文章推荐
- 相邻区域染色最小染色数问题 POJ 1129
- POJ 1129 平面图顶点染色
- poj 1129 Channel Allocation (DFS 图的染色问题)
- POJ1129 Channel Allocation——染色问题 暴搜
- ACM:着色问题 poj 1129
- POJ 1129 Channel Allocation 涂色问题 DFS 回溯 搜索
- poj 1129(图的着色问题)
- POJ 1129 Channel Allocation 图的染色 最大团
- POJ 1286 Necklace of Beads(Polya染色问题)
- POJ 3734 (矩阵快速幂+染色问题)
- Polya定理染色问题 (poj 1286)
- POJ 1129 Channel Allocation(dfs模拟图的上色问题)
- POJ 2528 Mayor's posters (线段树,染色问题,离散化要注意)
- POJ 2409 Let it Bead(polya染色问题)
- 【POJ】1129【涂色问题】【Channel Allocation】
- POJ2409 染色问题
- poj1129-Channel Allocation(搜索练习6-染色)
- POJ 2777(线段树,区间更新,使用二进制染色问题)
- POJ 1129 Channel Allocation DFS搜索 着色问题
- poj 2777 Count Color(线段树+染色问题)