Graph Coloring
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 4926   Accepted: 2289   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
【分析】此题就是求最大独立集。二部图的最大独立集==顶点数-匹配数,普通图的最大独立集==补图的最大团,且此题需要输出路径,所以用最大团的模板算法较好。
#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <string>
#include <map>
#include <queue>
#include <vector>
#define inf 0x7fffffff
#define met(a,b) memset(a,b,sizeof a)
typedef long long ll;
using namespace std;
const int N = ;
const int M = ;
bool w[N][N];
bool use[N]; //进入团的标号
bool bestx[N];
int cn,bestn,p,e; void dfs(int x) {
bool flag;
if(x>p) {
bestn=cn; //cn的值是递增的
for( int i=;i<=p; i++) //赋值给另外一个数组,
bestx[i]=use[i];
return ;
}
flag=true;
for( int i=; i<x; i++)
if(use[i]&&!w[i][x]) {
flag=false;
break;
}
if(flag) {
cn++;
use[x]=true;
dfs(x+);
cn--;
use[x]=false;//回溯
}
if(cn+p-x>bestn) { //剪枝
dfs(x+);
}
} int main() {
int num,u,v;
scanf("%d",&num);
while(num--) {
memset(w,true,sizeof(w));
memset(use,false,sizeof(use));
memset(bestx,false,sizeof(bestx));
scanf("%d%d",&p,&e);
for(int i=; i<e; i++) {
scanf("%d%d",&u,&v);
w[u][v]=false;
w[v][u]=false;
}
cn=bestn=;
dfs();
printf("%d\n",bestn);
for (int i=; i<=p; i++)if(bestx[i])printf("%d ",i);printf("\n");
}
return ;
}

POJ1419 Graph Coloring(最大独立集)(最大团)的更多相关文章

  1. poj1419 Graph Coloring 最大独立集(最大团)

    最大独立集: 顶点集V中取 K个顶点,其两两间无连接. 最大团: 顶点集V中取 K个顶点,其两两间有边连接. 最大独立集=补图的最大团最大团=补图的最大独立集 #include<iostream ...

  2. POJ1419 Graph Coloring

    嘟嘟嘟 求无向图的最大独立集. 有这么一回事:最大独立集=补图的最大团. 所谓的最大团,就是一个子图,满足图中任意两点都有边. 然后ssy巨佬告诉了我一个很没有道理强的做法:随机. 每一次random ...

  3. POJ 1419 Graph Coloring(最大独立集/补图的最大团)

    Graph Coloring Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 4893   Accepted: 2271   ...

  4. 【POJ】1419:Graph Coloring【普通图最大点独立集】【最大团】

    Graph Coloring Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 5775   Accepted: 2678   ...

  5. uva193 - Graph Coloring

    Graph Coloring You are to write a program that tries to find an optimal coloring for a given graph. ...

  6. poj 1419 Graph Coloring

    http://poj.org/problem?id=1419 题意: 一张图黑白染色,相邻点不能都染黑色,最多能染几个黑色点 最大点独立集 但是图不能同构为二分图,不能用二分图匹配来做 那就爆搜吧 还 ...

  7. GPS-Graph Processing System Graph Coloring算法分析 (三)

        HamaWhite 原创,转载请注明出处!欢迎大家增加Giraph 技术交流群: 228591158     Graph coloring is the problem of assignin ...

  8. UVA Graph Coloring

    主题如以下: Graph Coloring  You are to write a program that tries to find an optimal coloring for agiven ...

  9. Graph Coloring I(染色)

    Graph Coloring I https://www.nowcoder.com/acm/contest/203/J 题目描述 修修在黑板上画了一些无向连通图,他发现他可以将这些图的结点用两种颜色染 ...

随机推荐

  1. hbase基本概念和hbase shell常用命令用法

    1. 简介 HBase是一个分布式的.面向列的开源数据库,源于google的一篇论文<bigtable:一个结构化数据的分布式存储系统>.HBase是Google Bigtable的开源实 ...

  2. 分享我设计的iOS项目目录结构

    公司新项目就要着手研发了,希望能为这个项目多准备点知识.回想自己做过的项目,目录结构的划分总不如我的心意,有些目录命名不规范导致表达不明确,有些目录因为不具有代表性,导致在实际中不能充分发挥作用,导致 ...

  3. Android基础总结

    原文  http://blog.csdn.net/heimady/article/details/10363995 1. 前言 1.1. 什么是 3G . 4G Ÿ 第三代移动通信技术(3rd - G ...

  4. 2013年8月份第1周51Aspx源码发布详情

    校企工作室OA源码  2013-8-9 [VS2010]源码描述:主要模块及系统管理功能说明:一.考勤功能模块:考勤分成三个功能,显示签到功能,查询功能,管理功能.1.签到功能分析:在签到功能中,我们 ...

  5. 功率与dbm的对照表

     功率与dbm的对照表 分类: 嵌入式 功率与dbm的对照表 对于无线工程师来说更常用分贝dBm这个单位,dBm单位表示相对于1毫瓦的分贝数,dBm和W之间的关系是:dBm=10*lg(mW)1w的功 ...

  6. IOS图片缩放

    1.自动缩放到指定大小 + (UIImage *)thumbnailWithImage:(UIImage *)image size:(CGSize)asize { UIImage *newimage; ...

  7. Android.mk中添加宏定义

    在Boardconfig.mk 中添加一个 IS_FLAG := true 由于Boardconfig.mk和各目录的Android.mk是相互关联的 所以我们可以在Android.mk 中添加 一个 ...

  8. HDU 3047

    http://acm.hdu.edu.cn/showproblem.php?pid=3047 和hdu 3038以及poj那个食物链一样,都是带权并查集,此题做法和hdu 3038完全相同,具体操作看 ...

  9. nginx模块_使用gdb调试nginx源码

    工欲善其事必先利其器,如何使用调试工具gdb一步步调试nginx是了解nginx的重要手段. ps:本文的目标人群是像我这样初接触Unix编程的同学,如果有什么地方错误请指正. 熟悉gdb的使用 这里 ...

  10. CSS 实现:父元素包含子元素,子元素垂直居中布局

    ☊[实现要求]:父元素包含子元素,子元素垂直居中布局 <div class="demo5"> <div class="child">A& ...