poj1129
来源:互联网 发布:scala和java的关系 编辑:程序博客网 时间:2024/05/21 22:40
典型的图着色问题,相邻的顶点不能着相同的颜色。求最少的着色数。贪心法求解。每次都从颜色编号1开始着色,若相邻的顶点中存在该颜色,则增加颜色为编号2,若相邻的顶点中仍存在该颜色,则增加颜色为3,一直循环。下面给出代码(代码中注释部分要注意):
#include <stdio.h>#include <stdlib.h>#include <algorithm>#include <string.h>#define Max 30using namespace std;bool trim[Max][Max];int record[Max];int help[Max];int n;void Set_record(int number){int pivot=0;for(int i=1;i<=n;i++) // 将所有相邻的结点的颜色编号记录在help数组中if(trim[i][number])help[pivot++]=record[i]; sort(help,help+pivot); // 排序,便于比较record[number]=1; // 初始为颜色 1for(int i=0;i<pivot;i++) // 贪心算法if(help[i]==record[number])record[number]++;}int main(){char temp;while(scanf("%d",&n),n){memset(trim,0,sizeof(trim)); memset(record,0,sizeof(record)); // 初始为颜色编号0(无效编号) for(int i=1;i<=n;i++){getchar();getchar();while(true){temp=getchar(); if(temp=='\n')break;trim[i][temp-'A'+1]=true;}}for(int i=1;i<=n;i++)Set_record(i);sort(record+1,record+1+n);int Count=1;for(int i=1;i<n;i++) // 求出不同颜色个数if(record[i+1]>record[i])Count++;if(Count==1) // 注意单复数channel printf("1 channel needed.\n");elseprintf("%d channels needed.\n",Count);}return 0;}
0 0
- poj1129
- POJ1129
- POJ1129
- poj1129
- poj1129
- POJ1129
- POJ1129
- Poj1129
- poj1129
- poj1129
- poj1129
- poj1129 dfs
- [DFS]poj1129
- poj1129 poj1106
- POJ1129—Channel Allocation
- POJ1129--Channel Allocation
- POJ1129 Channel Allocation
- poj1129 Channel Allocation
- 少年跟“老大”进城见世面 两天偷80多万财物
- UVa 10404 Bachet's Game(DP)
- 字符串,包装类,正则规
- CFdiv2,A,队列
- nginx.conf文件详解及调优
- poj1129
- mac_显示隐藏文件夹
- php 计算两个时间的差
- 创建logical standby步骤
- 假期水题①
- 多线程学习笔记第一章
- Ctrl+shift+F格式化代码
- kmp算法(POJ 3461 Oulipo)
- C++内部数据类型和非内部数据类型