四色定理+dfs(poj 1129)

  题目:Channel Allocation

  题意:要求A:BCD,A与B,C,D都不相同,求不同的值,典型的四色定理;

  

#include <iostream>
#include <algorithm>
#include <stdlib.h>
#include <time.h>
#include <cmath>
#include <cstdio>
#include <string>
#include <cstring>
#include <vector>
#include <queue>
#include <stack>
#include <set>

#define c_false ios_base::sync_with_stdio(false); cin.tie(0)
#define INF 0x3f3f3f3f
#define INFL 0x3f3f3f3f3f3f3f3f
#define zero_(x,y) memset(x , y , sizeof(x))
#define zero(x) memset(x , 0 , sizeof(x))
#define MAX(x) memset(x , 0x3f ,sizeof(x))
#define swa(x,y) {LL s;s=x;x=y;y=s;}
using namespace std ;
#define N 105

const double PI = acos(-1.0);
typedef long long LL ;

int mapp[N][N], color[N];
int col,flag;
int n;
string s;
bool ok(int i){
    for(int j = 1; j <= 26; j++){
        if(!mapp[i][j]) continue;
        if(color[i] == color[j]) return false;
    }
    return true;
}

void dfs(int num){
    if(num > n) {flag = 1; return ;}
    for(int i = 1; i <= col; i ++){
        color[num] = i;
        if(ok(num))
            dfs(num+1);
        color[num]= 0;    ///记得回溯时要清零啊!!!
    }
}

int main(){
    //freopen("in.txt","r",stdin);
    //freopen("out.txt","w",stdout);
    while(cin>>n && n){
        flag = 0;
        zero(mapp);
        for(int i = 0; i< n;i++){
            cin>>s;
            int k = s.size();
            int x,y;
            for(int j = 2;j< k;j++){
                x = s[0] - 'A' +1;
                y = s[j] - 'A' +1;
                mapp[x][y] = mapp[y][x] = 1;
            }
        }
        for(col = 1; col <= 4; col++){
            dfs(1);
            if(flag) break;
        }
        if(col == 1)
            printf("%d channel needed.\n", col);
        else
            printf("%d channels needed.\n", col);
    }
    return 0;
}

 

转载于:https://www.cnblogs.com/yoyo-sincerely/p/5294285.html

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值