poj1129——dfs,四色问题

POJ 1129  dfs 四色问题

Channel Allocation

Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 12799   Accepted: 6558

Description

When a radio station is broadcasting over a very large area, repeaters are used to retransmit the signal so that every receiver has a strong signal. However, the channels used by each repeater must be carefully chosen so that nearby repeaters do not interfere with one another. This condition is satisfied if adjacent repeaters use different channels.

Since the radio frequency spectrum is a precious resource, the number of channels required by a given network of repeaters should be minimised. You have to write a program that reads in a description of a repeater network and determines the minimum number of channels required.

Input

The input consists of a number of maps of repeater networks. Each map begins with a line containing the number of repeaters. This is between 1 and 26, and the repeaters are referred to by consecutive upper-case letters of the alphabet starting with A. For example, ten repeaters would have the names A,B,C,...,I and J. A network with zero repeaters indicates the end of input.

Following the number of repeaters is a list of adjacency relationships. Each line has the form:

A:BCDH

which indicates that the repeaters B, C, D and H are adjacent to the repeater A. The first line describes those adjacent to repeater A, the second those adjacent to B, and so on for all of the repeaters. If a repeater is not adjacent to any other, its line has the form

A:

The repeaters are listed in alphabetical order.

Note that the adjacency is a symmetric relationship; if A is adjacent to B, then B is necessarily adjacent to A. Also, since the repeaters lie in a plane, the graph formed by connecting adjacent repeaters does not have any line segments that cross.

Output

For each map (except the final one with no repeaters), print a line containing the minumum number of channels needed so that no adjacent channels interfere. The sample output shows the format of this line. Take care that channels is in the singular form when only one channel is required.

Sample Input

2
A:
B:
4
A:BC
B:ACD
C:ABD
D:BC
4
A:BCD
B:ACD
C:ABD
D:ABC
0

Sample Output

1 channel needed.
3 channels needed.
4 channels needed. 

题意:在给定的图中涂色,使所有的结点都涂上色且相邻结点颜色不能相同,求最小需要的颜色思路:dfs,由四色定理可知最多只需四种颜色,故只需遍历四种颜色,从结点1,结点2,...结点n,利用贪心选择性质从颜色1开始涂,涂满时第一个解一定是最优解,图不一定是连通图,因此只能dfs(u){...dfs(u+1)..} 边界为n,而不能直接向四周扩展

/* poj1129_dfs  16ms */
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#include<vector>

using namespace std;

const int maxn=30;
const int INF=(1<<28);
int N;
vector<int> G[maxn];
int color[maxn];
bool vis[maxn][6];//第i个结点禁用颜色j
int ans,cnt;
bool flag;

void put(int u,int cor)
{
    color[u]=cor;
    for(int i=0;i<G[u].size();i++){
        int v=G[u][i];
        vis[v][cor]=1;
    }
}

void remov(int u,int cor)
{
    color[u]=0;
    for(int i=0;i<G[u].size();i++){
        int v=G[u][i];
        vis[v][cor]=0;
    }
}

void dfs(int u)
{
    if(flag) return;
    if(color[u]) return;
    if(u==N+1){
        ans=cnt;
        flag=1;return;
    }
    for(int i=1;i<=4;i++){
        if(!vis[u][i]){
            put(u,i);
            bool tag=0;
            if(i>cnt){
                tag=1;
                cnt++;
            }
            dfs(u+1);
            remov(u,i);
            if(tag) cnt--;
        }
    }
}

int main()
{
    while(cin>>N,N){
        for(int i=1;i<=N;i++) G[i].clear();
        getchar();
        for(int u=1;u<=N;u++){
            char ch;
            cin>>ch>>ch;
            while((ch=getchar())!=‘\n‘) G[u].push_back(ch-‘A‘+1);
        }
        ans=cnt=0;
        flag=0;
        memset(color,0,sizeof(color));
        memset(vis,0,sizeof(vis));
        dfs(1);
        if(ans==1) printf("1 channel needed.\n");
        else printf("%d channels needed.\n",ans);
    }
    return 0;
}

poj_1129_dfs

时间: 2024-10-03 19:11:07

poj1129——dfs,四色问题的相关文章

poj1129 Channel Allocation DFS

题目 题意:给定N个节点,让你对其涂色,使其任何相邻的两个节点颜色不同. 思路: 1. 问题模型是着色问题,枚举颜色的个数, 每次枚举看可以完成全部点的着色. 2. 采用深搜,每一次当前色的种数深搜完毕就加1种,直到全部点都被着色完毕, 这样 从最少的开始深搜,结果出现肯定是最少的. 该题N<26,可以不用四色原理剪枝. 附上一发代码: 1 #include<cstdio> 2 #include<iostream> 3 #include<cstring> 4 #i

[DFS]poj1129

题意: 就是给出一个图,涂色,相邻的点不能涂一样的颜色,问最少涂几种颜色? 分析: 首先数据量很小,可以暴力搜索.刚开始想的是bfs,一层一层的搜,但总写不对..,好吧,换种思路,一个点一个点的搜吧. #include <cstdio> #include <cstring> #include <cmath> #include <iostream> #include <vector> #include <map> #include &l

POJ-1129 Channel Allocation (DFS)

Description When a radio station is broadcasting over a very large area, repeaters are used to retransmit the signal so that every receiver has a strong signal. However, the channels used by each repeater must be carefully chosen so that nearby repea

poj1129 Channel Allocation(染色问题)

题目链接:poj1129 Channel Allocation 题意:要求相邻中继器必须使用不同的频道,求需要使用的频道的最少数目. 题解:就是求图的色数,这里采用求图的色数的近似有效算法——顺序着色算法(实质是一种贪心策略:在给任何一个顶点着色时,采用其邻接顶点中没有使用的,编号最小的颜色). 注:中继器网络是一个平面图,即图中不存在相交的边. 看讨论后发现这组数据,AC代码没过orz: 6 A:BEF B:AC C:BD D:CEF E:ADF F:ADE 正确答案应该是3 : A(1)B(

解救小哈——DFS算法举例

一.问题引入 有一天,小哈一个人去玩迷宫.但是方向感不好的小哈很快就迷路了.小哼得知后便去解救无助的小哈.此时的小哼已经弄清楚了迷宫的地图,现在小哼要以最快的速度去解救小哈.那么,问题来了... 二.问题的分析 首先我们用一个二维数组来存储这个迷宫,刚开始的时候,小哼处于迷宫的入口处(1,1),小哈在(p,q).其实这道题的的本质就在于找从(1,1)到(p,q)的最短路径. 此时摆在小哼面前的路有两条,我们可以先让小哼往右边走,直到走不通的时候再回到这里,再去尝试另外一个方向. 在这里我们规定一

【BZOJ4942】[Noi2017]整数 线段树+DFS(卡过)

[BZOJ4942][Noi2017]整数 题目描述去uoj 题解:如果只有加法,那么直接暴力即可...(因为1的数量最多nlogn个) 先考虑加法,比较显然的做法就是将A二进制分解成log位,然后依次更新这log位,如果最高位依然有进位,那么找到最高位后面的第一个0,将中间的所有1变成0,那个0变成1.这个显然要用到线段树,但是复杂度是nlog2n的,肯定过不去. 于是我在考场上yy了一下,这log位是连续的,我们每次都要花费log的时间去修改一个岂不是很浪费?我们可以先在线段树上找到这段区间

uva1103(dfs)

UVA - 1103 还是没写好,,看的别人的 1 #include <iostream> 2 #include <cstdio> 3 #include <cmath> 4 #include <cstring> 5 #include <algorithm> 6 #include <cstdlib> 7 #include <stack> 8 #include <cctype> 9 #include <str

poj 1088 滑雪 DP(dfs的记忆化搜索)

题目地址:http://poj.org/problem?id=1088 题目大意:给你一个m*n的矩阵 如果其中一个点高于另一个点 那么就可以从高点向下滑 直到没有可以下滑的时候 就得到一条下滑路径 求最大的下滑路径 分析:因为只能从高峰滑到低峰,无后效性,所以每个点都可以找到自己的最长下滑距离(只与自己高度有关).记忆每个点的最长下滑距离,当有另一个点的下滑路径遇到这个点的时候,直接加上这个点的最长下滑距离. dp递推式是,dp[x][y] = max(dp[x][y],dp[x+1][y]+

蓝桥杯 大臣的旅费_树的最长度_两次DFS

#include <iostream> #include <cstdio> #include <cstdlib> #include <algorithm> #include <cstring> #include <functional> #include <vector> using namespace std; const int maxn = 1000000 + 10; const int INF = 10000000