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. java将白色背景图片转换成无色

    package com.cxf.dao; import java.awt.Graphics2D; import java.awt.Image; import java.awt.image.Buffer ...

  2. HDU 2841 Visible Trees 数论+容斥原理

    H - Visible Trees Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u S ...

  3. mongoose学习笔记1--基础知识2

    Schema简述 Schema —— 一种以文件形式存储的数据库模型骨架,无法直接通往数据库端,也就是说它不具备对数据库的操作能力,仅仅只是数据库模型在程序片段中的一种表现,可以说是数据属性模型(传统 ...

  4. Java中栈结构的自我实现

    package com.pinjia.shop.common.collection; /** * Created by wangwei on 2017/1/3. */ public class MyL ...

  5. QML入门教程

    QML是Qt推出的Qt Quick技术的一部分,是一种新增的简便易学的语言.QML是一种陈述性语言,用来描述一个程序的用户界面:无论是什么样子,以及它如何表现.在QML,一个用户界面被指定为具有属性的 ...

  6. Android实现高仿QQ附近的人搜索展示

    本文主要实现了高仿QQ附近的人搜索展示,用到了自定义控件的方法 最终效果如下 1.下面展示列表我们可以使用ViewPager来实现(当然如果你不觉得麻烦,你也可以用HorizontalScrollVi ...

  7. 在iOS开发中,给项目添加新的.framework

    首先需要了解一下iOS中静态库和动态库.framework的概念 静态库与动态库的区别 首先来看什么是库,库(Library)说白了就是一段编译好的二进制代码,加上头文件就可以供别人使用. 什么时候我 ...

  8. 以普通用户登录 su root 用vncviewer:xxxxx 会报错!!exit 回到最初环境变的用户 问题解决!!!!

    [root@ok IT-DOC]# vncviewer : TigerVNC Viewer - built May :: Copyright (C) - TigerVNC Team and many ...

  9. SQL 替换指定列中的指定字符串

    Update 表名 Set 列名 = Replace(列名,‘被替换的字符’,‘要替换的字符’) Demo: UPDATE BPM_DailySET Workstation = REPLACE(Wor ...

  10. wp8 入门到精通

    <StackPanel Orientation="Vertical"> <StackPanel Orientation="Horizontal" ...