题目地址: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. Ansible 安装jdk

    1. 在hosts文件添一个group,里面是你需要安装jdk的ip,如: [newhosts]192.168.2.155 ansible_ssh_user=hadoop ansible_ssh_pa ...

  2. linux系统下面ftp的一些命令

    service vsftpd restart重启vsftpd服务service vsftpd stop停止vsftpd服务service vsftpd start启动vsftpd服务 chkconfi ...

  3. Android实现夜间模式小结

    随着APP实现的功能越来越丰富, 看小说看视频上网等等, 如今不少人花在手机平板等移动终端上的时间越来越长了. 但手机和平板的屏幕并不像Kindle那类电纸书的水墨屏那么耐看, 因为自发光的屏幕特性, ...

  4. ubuntu16.04 安装系统之后的开发必备-sourcelist--idk-sublime--opencv

    设置sourcelist.txt # 默认注释了源码镜像以提高 apt update 速度,如有需要可自行取消注释deb https://mirrors.tuna.tsinghua.edu.cn/ub ...

  5. (三)spark算子 分为3大类

    ation算子通过sparkContext执行提交作业的runJob,触发rdd的DAG执行 (foreach) foreach(f) 会对rdd中的每个函数进行f操作,下面的f操作就是打印输出没有元 ...

  6. 【数据结构】29、hashmap=》tableSizeFor 中求大于等于当前数的最小2的幂

    最近面试被问到hashmap的实现,因为前段时间刚好看过源码,显得有点信心满满,但是一顿操作下来的结论是基础不够扎实... 好吧,因为我开始看hashmap是想了解这到底是一个什么样的机制,具体有啥作 ...

  7. LayoutInflater这个类还是非常有用的,它的作用类似于findViewById()。 但他是找XML文件并实例化

    在实际开发中LayoutInflater这个类还是非常有用的,它的作用类似于findViewById().不同点是LayoutInflater是用来找res/layout/下的xml布局文件,并且实例 ...

  8. IntelliJ IDEA 2014 付费版 免费版比较

    http://www.jetbrains.com/idea/features/editions_comparison_matrix.html Freemarker, Velocity IDE Feat ...

  9. android 布局特点

    <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_cont ...

  10. Android 4.4(KitKat)中apk包的安装过程

    原文地址:http://blog.csdn.net/jinzhuojun/article/details/25542011 事实上对于apk包的安装.4.4和之前版本号没大的区别. Android中a ...