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. VirtualBox 虚拟 Ubuntu 的一些感想

    之前有说过最近在学习Shell,公司及家里的电脑目前都是Windows系统,进行shell学习主要是用一些模拟linux环境的软件,比如banbun.cygwin banbun的功能很强大,集成了oh ...

  2. Codeforces 417 C

    Football Time Limit: 1000MS   Memory Limit: 262144KB   64bit IO Format: %I64d & %I64u Submit Sta ...

  3. July 21st, Week 30th Thursday, 2016

    What youth deemed crystal, age finds out was dew. 年少时的水晶,在岁月看来不过是露珠. As time goes by, we are gradual ...

  4. Linux命令行下创建纳入版本控制下的新目录

    [root@ok 资料库]# svn mkdir test A test [root@ok 资料库]# svn ci -m "mkdir test" Adding 资料库/test ...

  5. Linux电源管理(11)_Runtime PM之功能描述

    转自:http://www.wowotech.net/pm_subsystem/rpm_overview.html 1. 前言 终于可以写Runtime PM(后面简称RPM)了,说实话,蜗蜗有点小激 ...

  6. Mac下Erlang环境安装

    下载源码(地址:http://www.erlang.org/download.html),  传统的三步安装: ./configure ./make sudo make install 备注:在编译系 ...

  7. poj 1087 最大流

    没啥好说的,慢慢建图 Sample Input 4 A B C D 5 laptop B phone C pager B clock B comb X 3 B X X A X D Sample Out ...

  8. Ubuntu各版本下载地址

    Ubuntu各版本下载地址:     http://old-releases.ubuntu.com/releases/

  9. C#学习笔记(五)——函数

    一.定义和使用函数. 直接通过例子进行说明吧 class Program { static void Write() { Console.WriteLine("Test output fro ...

  10. XML基础总结2

    在上篇的博客中,我们系统的介绍了一下xml与html之间的异同以及一部分xml的特性或者说是优点,接下来,我们就xml文档的一些语法规则具体向大家阐述一下: 1.xml文档形成了一种"树结构 ...