POJ 1129 Channel Allocation

来源:互联网 发布:警用强光手电 淘宝 编辑:程序博客网 时间:2024/04/29 05:41

题目链接:http://poj.org/problem?id=1129

题目描述:

这题可以看成就是一个涂色问题,给定一个无向图,节点最多为26个,然后给每个节点涂色,相邻的不能涂相同的颜色,其实就可以对每一个节点开始模拟涂色,1号还是,看是否合适即可。

<span style="font-size:18px;">#include<cstdio>#include<iostream>#include<cstring>#include<algorithm>using namespace std ;const int MAXN = 30 ;bool val[MAXN][MAXN] ;int col[MAXN], n ;//bool judge(int a, int b){for( int i = 0; i < n; i++ ){if( i == a )continue ;if( val[a][i] && col[i] == b )return false ;}return true ;}//void cal(){int res = 0 ;for( int i = 0; i < n; i++ ){for( int j = 0; j < 26; j++ ){if( judge(i, j) ){col[i] = j ;res = max(j, res) ;break ;} }}cout << res+1 ;if( res == 0 )cout << " channel needed.\n" ;else cout << " channels needed.\n" ;}//int main(){//freopen("1234.txt", "r", stdin) ;//freopen("out.txt", "w", stdout) ;while( scanf("%d",&n) != EOF && n ){memset(val, false, sizeof(val)) ;memset(col, -1, sizeof(col)) ;char ch ;ch = getchar() ;for( int i = 0; i < n; i++ ){scanf("%c:", &ch) ;int num = ch - 'A' ;ch = getchar() ;while( ch != '\n' ){val[num][ch-'A'] = true ; ch = getchar() ;}}cal() ;}return 0 ;}</span>


0 0
原创粉丝点击