题目地址:http://poj.org/problem?id=1419

Graph Coloring
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 4468   Accepted: 2042   Special Judge

Description

You are to write a program that tries to find an optimal coloring for a given graph. Colors are applied to the nodes of the graph and the only available colors are black and white. The coloring of the graph is called optimal if a maximum of nodes is black. The coloring is restricted by the rule that no two connected nodes may be black.



Figure 1: An optimal graph with three black nodes

Input

The
graph is given as a set of nodes denoted by numbers 1...n, n <= 100,
and a set of undirected edges denoted by pairs of node numbers (n1, n2),
n1 != n2. The input file contains m graphs. The number m is given on
the first line. The first line of each graph contains n and k, the
number of nodes and the number of edges, respectively. The following k
lines contain the edges given by a pair of node numbers, which are
separated by a space.

Output

The
output should consists of 2m lines, two lines for each graph found in
the input file. The first line of should contain the maximum number of
nodes that can be colored black in the graph. The second line should
contain one possible optimal coloring. It is given by the list of black
nodes, separated by a blank.

Sample Input

1
6 8
1 2
1 3
2 4
2 5
3 4
3 6
4 6
5 6

Sample Output

3
1 4 5
题目解读:对于一个给定的图找出一个最佳的着色。对图中的节点进行着色,只能用黑色或白色,着色的规则就是两个相邻的节点不能都是黑色。
(当然有可能是白色)T组数据,每组有n个节点,m条边。也就是要求当前图的最大独立集。
算法实现:通过求补图上的最大团来计算当前图的最大独立集。补图直接在输入边的时候就构建,采用规定模式计算补图最大团。 代码:
 #include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
#include <math.h>
#include <iostream>
#include <string>
#include <queue>
#include <algorithm>
#define N 105 using namespace std; int n, m;
bool g[N][N];
int get[N][N];
int node[N], ans[N], dp[N]; int Max; //当前团的节点数为Max void dfs(int now, int sum)
{ //从当前状态(当前团的节点数为now 与其中最后节点相连的边数为sum)出发,递归计算最大团
if(sum==){ //若构成团 即完全子图
if(now > Max){
Max=now;
for(int i=; i<=Max; i++)
ans[i]=node[i]; //存储团中的节点
}
return ;
}
for(int i=; i<=sum; i++){
int v=get[now][i], t=;
if(now+dp[v]<=Max) return ;
for(int j=i+; j<=sum; j++){
if(g[v][get[now][j]] )
get[now+][++t]=get[now][j];
}
node[now+]=v;
dfs(now+, t);
}
} void init()
{
scanf("%d %d", &n, &m);
memset(g, true, sizeof(g)); //补图初始化 边之间互相连通
for(int i=; i<m; i++){
int u, v;
scanf("%d %d", &u, &v);
g[u][v]=false; g[v][u]=false; //补图中应该断开
}
} void solve() //计算和输出补图的最大团 即是原图的最大独立集
{
Max=; //
for(int i=n; i>=; i--){ //按照递减顺序将每个节点i作为当前团的首节点
int sum=;
for(int j=i+; j<=n; j++){ //计算i+1...n中与i相邻的端点,将其存入get[1][]
if(g[i][j]) get[][++sum]=j;
}
node[]=i;
dfs(, sum);
dp[i]=Max;
}
printf("%d\n", Max);
for(int i=; i<=Max-; i++){
printf("%d ", ans[i]);
}
printf("%d\n", ans[Max]);
} int main(void)
{
int tg;
scanf("%d", &tg);
while(tg--){
init();
solve();
}
return ;
}
												

poj 1419Graph Coloring 【dfs+补图+计算最大团+计算最大独立集 【模板】】的更多相关文章

  1. POJ.3172 Scales (DFS)

    POJ.3172 Scales (DFS) 题意分析 一开始没看数据范围,上来直接01背包写的.RE后看数据范围吓死了.然后写了个2^1000的DFS,妥妥的T. 后来想到了预处理前缀和的方法.细节以 ...

  2. SQL Server-聚焦计算列或计算列持久化查询性能(二十二)

    前言 上一节我们详细讲解了计算列以及计算列持久化的问题,本节我们依然如前面讲解来看看二者查询性能问题,简短的内容,深入的理解,Always to review the basics. 持久化计算列比非 ...

  3. poj 1061 青蛙的约会 拓展欧几里得模板

    // poj 1061 青蛙的约会 拓展欧几里得模板 // 注意进行exgcd时,保证a,b是正数,最后的答案如果是负数,要加上一个膜 #include <cstdio> #include ...

  4. boost计算随机数和计算crc32简单示例 - jwybobo2007的专栏 - 博客频道 - CSDN.NET

    boost计算随机数和计算crc32简单示例 - jwybobo2007的专栏 - 博客频道 - CSDN.NET     boost::crc_32_type crc32;       crc32. ...

  5. boost计算随机数和计算crc32简单示例

    boost计算随机数和计算crc32简单示例 - jwybobo2007的专栏 - 博客频道 - CSDN.NET boost计算随机数和计算crc32简单示例 2013-02-18 17:14 10 ...

  6. 实验四 (1):定义一个形状类(Shape)方法:计算周长,计算面积

    (1)定义一个形状类(Shape)方法:计算周长,计算面积子类:矩形类(Rectangle) :额外的方法:differ() 计算长宽差圆形类(Circle)三角形类(Triangle)正方形类(Sq ...

  7. excel表计算和计算器计算结果不一致

    excel表计算和计算器计算结果不一致 : 建议安装完excel进行精度设置:

  8. 函数计算 触发式计算 日志 MP3 图片 合成视频

    函数计算 触发式计算   日志  MP3 图片 合成视频 [start_time]:20120511 06:59:11 [20120511 06:59:11_0.4950568322522534]: ...

  9. (1)定义闭合图形抽象类ClosedFigure定义属性:1.形状;2.定义构造方法,给形状赋值;3.定义两个抽象方法:计算面积和计算周长;4.定义一个显示方法:显示图像形状,周长,面积;

    题目显示不全,完整题目描述: (1)定义闭合图形抽象类ClosedFigure定义属性:1.形状:2.定义构造方法,给形状赋值:3.定义两个抽象方法:计算面积和计算周长:4.定义一个显示方法:显示图像 ...

随机推荐

  1. nightwatchJS ---element用法

    .element() Search for an element on the page, starting from the document root. The located element w ...

  2. Tomcat + Spring MVC + HttpClient:怎样使用PUT和PATCH方法传递数据

    在RESTful风格的API中.PUT/PATCH方法一般用于更新数据.在项目的代码中,使用的是HttpClient 4.5,是这样写的: protected JSONObject doHttpUri ...

  3. [django] 利用多线程添加异步任务

    看到django异步大家的反应应该是celery这样的消息队列组件.如今用的最多的最推荐的也是这样的方式.然而我这需求就是请求来了,运行一个小程序.可是又不能确定这个小程序啥时候运行完.响应又要及时, ...

  4. MySQL三:存储引擎

    阅读目录 一 什么是存储引擎 二 mysql支持的存储引擎 三 使用存储引擎 一 什么是存储引擎 mysql中建立的库===>文件夹 库中建立的表===>文件 现实生活中我们用来存储数据的 ...

  5. linux pptp 服务端安装并正常上网

    linux 下 PPTP VPN 1.安装相关软件32位版:yum -y install ppprpm -Uvh http://poptop.sourceforge.net/yum/stable/rh ...

  6. openwrt修改密码

    默认情况下root是没有密码的 需要设置密码后才能开启ssh 修改/etc/shadow文件: root:$1$wEehtjxj$YBu4quNfVUjzfv8p/PBo5.:0:0:99999:7: ...

  7. Http和Socket 优劣比较 使用场景选择_转

    转自:http://www.cnblogs.com/webwlsong/p/3198712.html 了解HTTP和Socket之前先对网络7层协议有个了解: 7 应用层6 表示层5 会话层 4 传输 ...

  8. Linux进程间通信(IPC)机制总览

    Linux进程间通信 Ø  管道与消息队列 ü  匿名管道,命名管道 ü  消息队列 Ø  信号 ü  信号基础 ü  信号应用 Ø  锁与信号灯 ü  记录锁 ü  有名信号灯 ü  无名信号灯(基 ...

  9. iOS开发之提交App中断出现:Cannot proceed with delivery: an existing transporter instance is currently uploading this package

    iOS开发之提交App中断出现:Cannot proceed with delivery: an existing transporter instance is currently uploadin ...

  10. 九度OJ 1190:大整数排序 (大数运算、排序)

    时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:3219 解决:1467 题目描述: 对N个长度最长可达到1000的数进行排序. 输入: 输入第一行为一个整数N,(1<=N<=1 ...