poj 1419Graph Coloring 【dfs+补图+计算最大团+计算最大独立集 【模板】】
题目地址:http://poj.org/problem?id=1419
| Time Limit: 1000MS | Memory Limit: 10000K | |||
| Total Submissions: 4468 | Accepted: 2042 | Special Judge | ||
Description

Figure 1: An optimal graph with three black nodes
Input
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
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+补图+计算最大团+计算最大独立集 【模板】】的更多相关文章
- POJ.3172 Scales (DFS)
POJ.3172 Scales (DFS) 题意分析 一开始没看数据范围,上来直接01背包写的.RE后看数据范围吓死了.然后写了个2^1000的DFS,妥妥的T. 后来想到了预处理前缀和的方法.细节以 ...
- SQL Server-聚焦计算列或计算列持久化查询性能(二十二)
前言 上一节我们详细讲解了计算列以及计算列持久化的问题,本节我们依然如前面讲解来看看二者查询性能问题,简短的内容,深入的理解,Always to review the basics. 持久化计算列比非 ...
- poj 1061 青蛙的约会 拓展欧几里得模板
// poj 1061 青蛙的约会 拓展欧几里得模板 // 注意进行exgcd时,保证a,b是正数,最后的答案如果是负数,要加上一个膜 #include <cstdio> #include ...
- boost计算随机数和计算crc32简单示例 - jwybobo2007的专栏 - 博客频道 - CSDN.NET
boost计算随机数和计算crc32简单示例 - jwybobo2007的专栏 - 博客频道 - CSDN.NET boost::crc_32_type crc32; crc32. ...
- boost计算随机数和计算crc32简单示例
boost计算随机数和计算crc32简单示例 - jwybobo2007的专栏 - 博客频道 - CSDN.NET boost计算随机数和计算crc32简单示例 2013-02-18 17:14 10 ...
- 实验四 (1):定义一个形状类(Shape)方法:计算周长,计算面积
(1)定义一个形状类(Shape)方法:计算周长,计算面积子类:矩形类(Rectangle) :额外的方法:differ() 计算长宽差圆形类(Circle)三角形类(Triangle)正方形类(Sq ...
- excel表计算和计算器计算结果不一致
excel表计算和计算器计算结果不一致 : 建议安装完excel进行精度设置:
- 函数计算 触发式计算 日志 MP3 图片 合成视频
函数计算 触发式计算 日志 MP3 图片 合成视频 [start_time]:20120511 06:59:11 [20120511 06:59:11_0.4950568322522534]: ...
- (1)定义闭合图形抽象类ClosedFigure定义属性:1.形状;2.定义构造方法,给形状赋值;3.定义两个抽象方法:计算面积和计算周长;4.定义一个显示方法:显示图像形状,周长,面积;
题目显示不全,完整题目描述: (1)定义闭合图形抽象类ClosedFigure定义属性:1.形状:2.定义构造方法,给形状赋值:3.定义两个抽象方法:计算面积和计算周长:4.定义一个显示方法:显示图像 ...
随机推荐
- CSS实现绝对定位居中
我们经常用margin:0 auto来实现水平居中,而一直认为margin:auto不能实现垂直居中……实际上,实现垂直居中仅需要声明元素高度和下面的CSS: .Absolute-Center { m ...
- 【Cocos2dX(2.x)_Lua开发之三】
[Cocos2dX(2.x)_Lua开发之三]在Lua中使用自定义精灵(Lua脚本与自创建类之间的访问)及Lua基础讲解 本站文章均为李华明Himi原创,转载务必在明显处注明:(作者新浪微博:@李华明 ...
- Android SDK环境搭建
方法有二 方法一: Android SDK开发包国内下载地址 http://www.cnblogs.com/bjzhanghao/archive/2012/11/14/android-platform ...
- Eclipse配置总结
按照公司要求,开发环境使用Eclipse Juno版本,需要安装maven插件和RTC插件. 经过一下午的尝试,总结经验教训: 1.eclipse安装maven的m2ec ...
- apache占用内存高解决办法
我用512M的vps,访问量不大,但内存占用很大,甚至宕机. 我用top,然后shitf+m发现,httpd占用内存极大.经过网上找资料设置后,用过一段时间终于没再出现内存问题了. 首先查找配置文件的 ...
- 产生n个数全排列的算法
给定n个数{1...n},如何给出这n个数的全排列呢? 给定一个整数k,我们给它一个向左或向右的方向,k(->)或者k(<-),我们说k是可以移动的,如果它的方向指向一个相邻的比它小的数, ...
- 在Ubuntu下利用Eclipse开发FFmpeg配置小结
首先需要编译FFmpeg得到头文件和lib文件,参见:在Ubuntu下编译FFmpeg 选择File-New-C Project 选择Executable下的Empty Project,右侧选择Lin ...
- python学习 03 函数 (只会执行一次return就不会往下执行)
1.调用函数只会执行一次return,而且执行return后不会往下执行
- js自己定义插件-选项卡
该功能比較简单.巩固一下jquery插件写法,注意引入的jquery.js . 自己定义插件路径代码例如以下: 页面: <!doctype html> <html> < ...
- 查找 TextBox 对象中非法数据的示例
private void GetErrors(StringBuilder sb, DependencyObject obj){ foreach (object child in LogicalTree ...