POJ1129 Channel Allocation [图染色 暴搜]
来源:互联网 发布:网络领域破解版 编辑:程序博客网 时间:2024/06/05 00:40
题意:
给定一个图。如今要给每个点分配一个频段(可视为染上一种颜色)。要求如果某两个点之间有边,则染上的颜色不能相同。为如何颜色可以使使用的颜色数最少。
思路:
因为数据量N<=26,所以颜色种类数最多为26。
从1到n依次染色,初始时染色点1为颜色1,即color[1]=1;
对于每个点,他的颜色可能值用一个has数组表示。
如果它与已经染色的点之间有边,则将该has值赋值true。
然后遍历该has数组,找出最小的i使得has[i]==false,即可能使用的颜色。然后color[now]=i;
然后再进行下一个点。直到n点都染色完毕。
这样时间复杂度O(26*26).
一次AC.记得之前写图论题的时候,曾经被染色的问题整的半死,一看到染色就有畏惧感。
现在想想其实是基础不扎实就接触图论的比较复杂的染色建图之类的题目,下场必须惨。
#include<iostream>#include<vector>#include<string>#include<queue>#include<cmath>#include<algorithm>#define llong long long#define Min(a,b) (a<b?a:b)#define Max(a,b) (a>b?a:b)#define Abs(a) ((a)>0?(a):-(a))#define Mod(a,b) (((a)-1+(b))%(b)+1)using namespace std;int n,m;const int N=30;const int inf=99999999;bool mat[N][N];int color[N];void solve(){memset(color,0,sizeof(color));color[1]=1;for(int i=1;i<=n;i++){bool has[N]={0};for(int j=1;j<i;j++){if(mat[j][i])has[color[j]]=true;}int k=1;while(has[k]){k++;}color[i]=k;}int ans=0;for(int i=1;i<=n;i++)ans=Max(ans,color[i]);if(ans>1)printf("%d channels needed.\n",ans);elseprintf("%d channel needed.\n",ans);}int main(){while(scanf("%d",&n),n){char str[30];memset(mat,0,sizeof(mat));for(int i=1;i<=n;i++){scanf("%s",str);for(int j=2;j<strlen(str);j++){mat[i][str[j]-'A'+1]=true;mat[str[j]-'A'+1][i]=true;}}solve();}return 0;}
- POJ1129 Channel Allocation [图染色 暴搜]
- poj1129 Channel Allocation 染色问题
- POJ1129 Channel Allocation——染色问题 暴搜
- poj1129-Channel Allocation(搜索练习6-染色)
- POJ1129—Channel Allocation
- POJ1129--Channel Allocation
- POJ1129 Channel Allocation
- poj1129 Channel Allocation
- POJ1129 Channel Allocation
- POJ1129-Channel Allocation
- POJ1129 Channel Allocation DFS
- POJ1129 Channel Allocation
- POJ1129 Channel Allocation(AC)
- poj1129 Channel Allocation
- poj1129 Channel Allocation
- POJ1129-Channel Allocation
- HDU1373&&POJ1129-Channel Allocation
- POJ1129 Channel Allocation
- linux增加用户
- 怎么获取本地时间存入mysql数据库,数据库类型是datetime
- js使用技巧5
- wubi
- RACLE中用一条SQL实现其它进制到十进制的转换
- POJ1129 Channel Allocation [图染色 暴搜]
- Python Multiprocess with SIGTERM supported
- 深入嵌入式linux知识要点
- 1350. Piggy banks
- RTX的实时TCP-IP协议栈
- js使用技巧6
- linux环境变量设置 (PS1,PS2)
- 使用 EditPlus 替换行编号和空行的方法
- Android中TabHost嵌套TabHost