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

Solution

最大点独立集???

差点就拍二分图叻....

然后发现这道题根本没法二分图啊??就是个普通图?

上网学习才发现,二分图最大点独立集=顶点数-最大匹配普通图最大点独立集=补图的最大团

然而补图是啥...最大团是啥....

图G的补图,通俗的来讲就是完全图Kn去除G的边集后得到的图Kn-G。在图论里面,一个图G的补图(complement)或者反面(inverse)是一个图有着跟G相同的点,而且这些点之间有边相连当且仅当在G里面他们没有边相连。

如果U V,且对任意两个顶点u,v∈U有(u,v)∈E,则称U是G的完全子图。G的完全子图U是G的团。G的最大团是指G的最大完全子图。

显然,原图的最大点独立集在补图中肯定两两相邻,所以求补图的最大团就是原图的最大点独立集。

用dfs求解,需要剪枝!

Code

#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std; int n, m, maxn, num;
int use[], now[], G[][];
void dfs(int x) {
if(x > n) {
maxn = num;
for(int i = ; i <= n; i ++)
use[i] = now[i];
return ;
}
int flag = ;
for(int i = ; i < x; i ++) {
if(now[i] && !G[i][x]) {//////如果之前选了的点与现在的点没有相邻 现在这个点就不能在当前团里面
flag = ; break;
}
}
if(flag) {
num ++;
now[x] = ;
dfs(x + );
num --;
now[x] = ;
}
if(num + n - x > maxn) dfs(x + );
} int main() {
int T;
scanf("%d", &T);
while(T --) {
memset(G, , sizeof(G));
memset(now, , sizeof(now));
memset(use, , sizeof(use));
scanf("%d%d", &n, &m);
for(int i = ; i <= m; i ++) {
int u, v;
scanf("%d%d", &u, &v);
G[u][v] = G[v][u] = ; ////////补图
}
num = maxn = ;
dfs();
printf("%d\n", maxn);
for(int i = ; i <= n; i ++)
if(use[i]) printf("%d ", i);
printf("\n");
}
return ;
}

【POJ】1419:Graph Coloring【普通图最大点独立集】【最大团】的更多相关文章

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

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

  2. poj 1419 Graph Coloring

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

  3. POJ 2771 Guardian of Decency(求最大点独立集)

    该题反过来想:将所有可能发生恋爱关系的男女配对,那么可以带出去的人数应该等于这个二分图的最大独立集 先要做一下预处理,把不符合要求的双方先求出来, company[i][j]表示i.j四个标准都不符合 ...

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

                                                               Graph Coloring Time Limit: 1000MS   Memor ...

  5. uva 193 Graph Coloring(图染色 dfs回溯)

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

  6. hdu 2768(建图,最大点独立集)

    Cat vs. Dog Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total ...

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

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

  8. 【最大点独立集】【poj1419】【Graph Coloring】

    题意: 最多能选取多少点,没有边相连. 解法: 取反图,求最大团 代码: #include<cstdio> #include<cstring> #include<iost ...

  9. POJ 2771 最大点独立集

    这是经典的最大点独立集 还是可以转化成最大匹配数,为什么呢,因为求出最大匹配数之和,匹配的边的两个端点互斥,只能去一个,所以最后结果就用总点数-最大匹配数即可 #include <iostrea ...

随机推荐

  1. jQuery遍历方式

    对象遍历

  2. 关于Java IO与NIO知识都在这里

    由于内容比较多,我下面放的一部分是我更新在我的微信公众号上的链接,微信排版比较好看,更加利于阅读.每一篇文章下面我都把文章的主要内容给列出来了,便于大家学习与回顾. Java面试通关手册(Java学习 ...

  3. ubuntu16.04 eclipse+pydev 配置

    参考:http://blog.csdn.net/bluish_white/article/details/56509446,http://blog.csdn.net/qing101hua/articl ...

  4. 深入理解MySQL的并发控制、锁和事务【转】

    本文主要是针对MySQL/InnoDB的并发控制和加锁技术做一个比较深入的剖析,并且对其中涉及到的重要的概念,如多版本并发控制(MVCC),脏读(dirty read),幻读(phantom read ...

  5. SQL 根据关联表更新主表中字段数据

    今天遇到一个客户的数据更新问题,两个相关联的表,一个主表用于保存单据主要信息,一个副表用于保存单据的明细信息:现在要把主表的其中一个字段的数据更新到副表的一个字段中保存.精通的SQL语法的,当然是很简 ...

  6. 序列化 json和pickle

    序列化 1. 什么叫序列化 将原本的字典.列表等内容转换成一个字符串的过程就叫做序列化. 2.  json dumps loads    一般对字典和列表序列化 dump load       一般对 ...

  7. ZooKeeper的典型应用场景

    <从Paxos到Zookeeper 分布式一致性原理与实践>读书笔记 本文:总结脑图地址:脑图 前言 所有的典型应用场景,都是利用了ZK的如下特性: 强一致性:在高并发情况下,能够保证节点 ...

  8. 20165333 2016-2017-2 《Java程序设计》第1周学习总结

    20165333 2016-2017-2 <Java程序设计>第1周学习总结 教材学习内容总结 java 的地位 Java 的特点 安装JDK 系统环境的设置 Java程序的编写,编译和运 ...

  9. MEF实现设计上的“松耦合”(一)

    1.什么是MEF 先来看msdn上面的解释:MEF(Managed Extensibility Framework)是一个用于创建可扩展的轻型应用程序的库. 应用程序开发人员可利用该库发现并使用扩展, ...

  10. 如何在k8s集群里快速运行一个镜像?

    在docker里,快速run一个镜像,很简单的. k8s的世界,与之类似. 但要注意一下,如果镜像本身没有提供command命令,这个容器由于前台输出完成,很快就退出了. 所以,遇到这种镜像,就最好自 ...