poj 1129 Channel Allocation(图着色,DFS)

题意:

N个中继站,相邻的中继站频道不得相同,问最少需要几个频道。


输入输出:

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.


题意抽象+思路:

一张有N个点的无向图,对每个点进行染色,相邻的点颜色不得一致,最少需多少种颜色。DFS即可。



代码:

<span style="font-family:Microsoft YaHei;">int nmax;
bool mapp[30][30], is[30], coll[30];
int col[30];


void dfs(int u){
    mem(coll,false);
    rep(i,0,25){
        if(u!=i && mapp[u][i] && col[i]){
            coll[col[i]] = true;
        }
    }
    rep(i,26,1) if(coll[i]){
        nmax = i;
        break;
    }
    rep(i,1,26) if(!coll[i]){
        col[u] = i;
        nmax = max(nmax,i);
        break;
    }
    rep(i,0,25){
        if(u!=i && mapp[u][i] && !col[i])
            dfs(i);
    }
}

int main(){
    int n;
    char s[100];

    while(scanf("%d",&n),n){
        mem(mapp,false);
        mem(is,false);
        mem(col,0);

        while(n--){
            scanf("%s",s);
            int l=strlen(s);
            is[s[0]-'A'] = true;
            rep(i,2,l-1){
                mapp[s[0]-'A'][s[i]-'A'] = true;
                mapp[s[i]-'A'][s[0]-'A'] = true;
                is[s[i]-'A'] = true;
            }
        }

        nmax = 0;
        rep(i,0,25){
            if(is[i] && !col[i]) dfs(i); //对i进行染色,并从i开始把那个集合中的点都染上色
        }

        if(nmax==1) printf("%d channel needed.\n",nmax);
        else printf("%d channels needed.\n",nmax);
    }
}
</span>


转载于:https://www.cnblogs.com/fish7/p/3985292.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值