题目地址: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. mfs客户端挂载

    1.安装fuse yum install fuse fuse-devel 2.加载fuse模块 modprobe fuse 3.创建mfs用户 useradd mfs -s /sbin/nologin ...

  2. nginx根据目录反向代理到后端服务器

    nginx根据目录反向代理到后端不同的服务器 server {         listen 80;         server_name demo.domain.com;         #通过访 ...

  3. 使用nginx搭建https服务器(转)

    最近在研究nginx,整好遇到一个需求就是希望服务器与客户端之间传输内容是加密的,防止中间监听泄露信息,但是去证书服务商那边申请证书又不合算,因为访问服务器的都是内部人士,所以自己给自己颁发证书,忽略 ...

  4. Java交通灯系统

    交通灯管理项目模拟了对十字路口交通灯的控制,一般在我们生活中的十字路口是有人行道的,而此项目没有考虑人行道.具体需求如下:  1.异步随机生成按照各个路线行驶的车辆.          例如:     ...

  5. 机器学习13—PCA学习笔记

     主成分分析PCA 机器学习实战之PCA test13.py #-*- coding:utf-8 import sys sys.path.append("pca.py") impo ...

  6. Java IO、网络编程、NIO、Netty、Hessian、RPC、RMI的学习路线

    好久没看Java IO这块的内容,感觉都快忘得差不多了.平成编程也没有设计到太多的Java基础知识,所以这里希望可以抽点时间回顾一下,让艾宾浩斯记忆曲线不要下降的太快. 回顾这个主要还是以总结为主,能 ...

  7. spring boot配置文件

    1.spring boot通常打成一个jar文件发布,想修改配置文件比较麻烦,但他提供了一种读取外部配置文件的方式.在代码的主类中增加如下代码 System.setProperty("spr ...

  8. 基于树莓派3B+Python3.5的OpenCV3.4的配置教程

    https://www.cnblogs.com/Pyrokine/p/8921285.html

  9. [LeetCode] Remove Duplicates from Sorted Array II [27]

    题目 Follow up for "Remove Duplicates": What if duplicates are allowed at most twice? For ex ...

  10. 写出完美的snprintf

    平时公司的代码安全扫描会给出不安全代码的告警,其中会检查代码中间的strcpy和sprintf函数,而要求使用strncpy和snprintf.今天我们讨论一下怎样写出完美的snprintf. snp ...