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. Mybatis各种模糊查询

    转载自:http://blog.sina.com.cn/s/blog_667bef380101f2da.html 工作中用到,写三种用法吧,第四种为大小写匹配查询 1. sql中字符串拼接 SELEC ...

  2. 项目总结(五)--- 界面调试工具Reveal

    在开发中,我们也许会碰到以下需求:对于一些动态复杂的交互界面,手码去制定界面是常有的事情,然而我们在开发中想修改过一些参数后想看下实时效果,只能重新运行项目,进入到对应的页面来进行修改,是不是有点麻烦 ...

  3. Fedora 21 install chrome

    Steps to install Google Chrome on Fedora 21 A. Create google-chrome.repo file Use this command to cr ...

  4. Ubuntu13.04 安装 chrome

    1.chrome官网下载deb安装包:https://www.google.com/intl/zh-CN/chrome/browser/ 2.进入下载好的目录执行:sudo dpkg -i googl ...

  5. Fresco 源码分析(二) Fresco客户端与服务端交互(2) Fresco.initializeDrawee()分析 续

    4.2.1.2 Fresco.initializeDrawee()的过程 续 继续上篇博客的分析Fresco.initializeDrawee() sDraweeControllerBuilderSu ...

  6. centos 截图命令 screenshot

    [root@ok ~]# gnome-screenshot#全屏截图 [root@ok ~]# gnome-screenshot --interactive#自定义截图

  7. margin和padding那点事及常见浏览器margin padding相关Bug

    用Margin还是用Padding 何时应当使用margin: 需要在border外侧添加空白时. 空白处不需要背景(色)时. 上下相连的两个盒子之间的空白,需要相互抵消时.如15px + 20px的 ...

  8. Linux snmp监控

    http://blog.csdn.net/apple_llb/article/details/50494787 http://www.ttlsa.com/monitor/snmp-oid/

  9. Xamarin.Android开发实践(十一)

    Xamarin.Android之使用百度地图起始篇 一.前言 如今跨平台开发层出不穷,而对于.NET而言时下最流行的当然还是 Xamarin,不仅仅能够让我们在熟悉的Vs下利用C#开发,在对原生态类库 ...

  10. Java Hour 44 Hibernate

    其实要学习的东西很多,奈何人的精力和时间终归是有限的. 这里先暂且放下struts2 相关的东西,当然这里也先寄存这不少相关的好书,等我来看. 44.1 Hibernate 是一个好项目 目标在于成为 ...