Graph Coloring
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 4893   Accepted: 2271   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

Source

题目链接:POJ 1419

模版题,熟悉一下写法和过程

代码:

#include<iostream>
#include<algorithm>
#include<cstdlib>
#include<sstream>
#include<cstring>
#include<cstdio>
#include<string>
#include<deque>
#include<stack>
#include<cmath>
#include<queue>
#include<set>
#include<map>
using namespace std;
#define INF 0x3f3f3f3f
#define CLR(x,y) memset(x,y,sizeof(x))
#define LC(x) (x<<1)
#define RC(x) ((x<<1)+1)
#define MID(x,y) ((x+y)>>1)
typedef pair<int,int> pii;
typedef long long LL;
const double PI=acos(-1.0); const int N=110; bool E[N][N];
int x[N],opt[N];
int n,m,ans,cnt,maxn,vis[N<<2]; void dfs(int i)
{
int j;
if(i>n)
{
ans=cnt;
for (j=1; j<=n; ++j)
opt[j]=x[j];
}
else
{
bool flag=true;
for (j=1; j<i&&flag; ++j)
{
if(x[j]==1&&!E[j][i])
flag=false;
}
if(flag)
{
x[i]=1;
++cnt;
dfs(i+1);
--cnt;
}
if(cnt+n-i>ans)
{
x[i]=0;
dfs(i+1);
}
}
}
void init()
{
CLR(E,true);
CLR(x,0);
CLR(opt,0);
maxn=ans=cnt=0;
CLR(vis,0);
}
int main(void)
{
int i,j,tcase,x,y;
scanf("%d",&tcase);
while (~scanf("%d%d",&n,&m))
{
init();
scanf("%d%d",&n,&m);
for (i=0; i<m; ++i)
{
scanf("%d%d",&x,&y);
E[x][y]=E[y][x]=false;
}
dfs(1);
printf("%d\n",ans);
for (i=1; i<n; ++i)
if(opt[i])
printf("%d ",i);
putchar('\n');
}
return 0;
}

POJ 1419 Graph Coloring(最大独立集/补图的最大团)的更多相关文章

  1. poj 1419 Graph Coloring

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

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

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

  3. poj 1419Graph Coloring 【dfs+补图+计算最大团+计算最大独立集 【模板】】

    题目地址:http://poj.org/problem?id=1419 Graph Coloring Time Limit: 1000MS   Memory Limit: 10000K Total S ...

  4. POJ1419 Graph Coloring(最大独立集)(最大团)

                                                               Graph Coloring Time Limit: 1000MS   Memor ...

  5. 【poj1419】 Graph Coloring

    http://poj.org/problem?id=1419 (题目链接) 题意 求一般图最大独立集. Solution 最大独立集=补图的最大团. 代码 // poj1419 #include< ...

  6. POJ1419 Graph Coloring

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

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

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

  8. uva193 - Graph Coloring

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

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

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

随机推荐

  1. PHP---TP框架---添加数据-----有三种方式

    添加数据 添加数据有三种方式: 第一种: <?php namespace Home\Controller;//这个文件的命名空间 use Think\Controller;//use使用哪一个而 ...

  2. POSIX线程

    大多数线程函数以pthread_开头,.h为pthread.h,   用-lpthread来链接线程库. 编写多线程时,定义宏_REENTRANT告诉编译器需要可重入,此宏必须位于任何#include ...

  3. redhat6.2下的ssh密钥免密码登录(原创)

    这个是我自己写的,鼓励转载,请说明转载地址:http://www.cnblogs.com/nucdy/p/5664840.html 在进行hadoop的免密码的登录操作是,老是发生no route等错 ...

  4. Maven使用笔记(三)Maven的工作原理

    概述 Maven是一个项目管理工具,他包含了一个项目对象模型,一组标准集合,一个项目生命周期,一个依赖管理系统和用来运行定义生命周期阶段中插件目标的逻辑. Maven是基于约定优于配置的思想来管理代码 ...

  5. NVelocity模板引擎的使用

    第一种使用方法直接赋值: VelocityEngine vltEngine = new VelocityEngine(); vltEngine.SetProperty(RuntimeConstants ...

  6. Asp.net MVC 利用自定义RouteHandler来防止图片盗链

    你曾经注意过在你服务器请求日志中多了很多对图片资源的请求吗?这可能是有人在他们的网站中盗链了你的图片所致,这会占用你的服务器带宽.下面这种方法可以告诉你如何在ASP.NET MVC中实现一个自定义Ro ...

  7. ServletContext与ServletConfig的详解及区别

    转自http://hi.baidu.com/huaxuelili/item/1704a03dbb5cd7f22784f4c6 一.ServletContext详解ServletContext是serv ...

  8. HealthKit开发快速入门教程之HealthKit开发概述简介

    HealthKit开发快速入门教程之HealthKit开发概述简介 2014年6月2日召开的年度开发者大会上,苹果发布了一款新的移动应用平台,可以收集和分析用户的健康数据.该移动应用平台被命名为“He ...

  9. HDU3251 Being a Hero(最小割)

    题目大概一个国家n个城市由m条单向边相连,摧毁每条边都有一个费用.现在你可以选择所给的f个城市中的若干个,每个城市选择后都有一定的价值,但首都1号城市必须到达不了你选择的城市,因为你可能需要摧毁一些边 ...

  10. python 代码片段11

    #coding=utf-8 #python里面的字典,用{}来表示 book={'title':'python web development','year':2008} print book pri ...