题目地址: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. python--pipe

    1.pipe 除了使用队列外,还可以使用管道在进程间执行消息传递 pipe([]duplex) 在进程间创建一条管道,并返回元组(conn1,conn2),其中conn1和conn2是表示管道两端的C ...

  2. iOS 最新公布app到AppStore全流程具体解释

    一.生成公布证书(证书的作用:类似于驾照,证明你的身份能够进行开发人员一些操作) 打开https://developer.apple.com 点击右上角开发人员中心 这里输入你付款过的Apple 帐号 ...

  3. 利用Bootstrap简单实现一个文件上传进度条

    © 版权声明:本文为博主原创文章,转载请注明出处 说明: 1. 使用commons-fileupload.jar实现文件上传及进度监听 2. 使用bootstrap的进度条进行页面显示 3. 因为进度 ...

  4. NoHostAvailableException: All host(s) tried for query failed (no host was tried)

    NoHostAvailableException: All host(s) tried for query failed (no host was tried) cassandra连接报错:tried ...

  5. Viewer 是一款强大的 jQuery 图像浏览插件。

    Viewer 是一款强大的 jQuery 图像浏览插件. 主要功能: 支持选项 支持方法 支持事件 支持触摸 支持移动 支持缩放 支持旋转 支持键盘 跨浏览器支持 链接: viewer的官方演示,及g ...

  6. 过年啦!小B高兴的不行了,她收到了很多红包,可以实现好多的愿望呢。小B可是对商店货架上心仪的货物红眼好久了,只因囊中羞涩作罢,这次她可是要大大的shopping一番。小B想去购物时,总是习惯性的把要买的东西列在一个购买清单上,每个物品单独列一行(即便要买多个某种物品),这次也不例外。

    include "stdafx.h" #include<iostream> #include<vector> #include <algorithm& ...

  7. IIS7设置默认页

    一般用ASP.NET创建的网站默认页都是Default.aspx,不需要设置. 但是如果有网站的起始页不是Default.aspx,就需要在IIS里设置了. IIS7的设置方法和IIS6的不一样: 在 ...

  8. px与与rem vw的区别

    1.px 使用具体像素点为单位,好处是比较稳定和精确,但在浏览器放大缩小会出现问题 2.rem 参考根元素的值 例如设置根元素字体大小是20像素 在h1中设置字体大小 那么H1的大小就是40px p的 ...

  9. yum lock 解决方法

    方法一: # ps aux | grep yum # kill -9 pid 方法二:可以通过执行rm -rf /var/run/yum.pid 来强行解除锁定,然后你的yum就可以运行了 解释: [ ...

  10. 有关于__align(n) ,内存对齐

    __align __align 关键字指示编译器在 n 字节边界上对齐变量. __align 是一个存储类修饰符.它不影响函数的类型. 语法 __align(n) 其中: n 是对齐边界. 对于局部变 ...