主题如以下:

Graph Coloring 

You are to write a program that tries to find an optimal coloring for agiven graph. Colors are applied to the nodes of the graph and the only availablecolors are black and white. The coloring of the graph is called optimalif a maximum of nodes is black.
The coloring is restricted by the rule thatno two connected nodes may be black.




Figure: An optimal graph with three black nodes

Input and Output

The graph is given as a set of nodes denoted by numbers ,
,and a set of undirected edges denoted by pairs of node numbers
,
. The input file contains
mgraphs. The number m is given on the first line. The first line ofeach graph contains
n and k, the number of nodes and the numberof edges, respectively. The following
k lines contain the edges givenby a pair of node numbers, which are separated by a space.

The output should consists of 2m lines, two lines for each graphfound in the input file. The first line of should contain the maximum numberof nodes that can be colored black in the graph. The second line shouldcontain one possible optimal coloring.
It is given by the list of blacknodes, 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

求图中黑色节点的最大个数,黑色节点不能相邻,看起来挺简单的(实际上也非常easy。

。),但自己NC,超时又WA了几次,没理解清晰DFS中的cur的含义,我原意cur是推断过的节点个数,但这样确定不了递归边界,假设边界是cur==n,那么每次递归都得推断n个点,非常浪费时间,有些点是明显不可能的。

后来改成cur是当前要推断的点的下标。思路清晰多了,但还存在一个问题:若当前节点不能染色,要不要递归下去?细致想想就会发现,肯定须要递归下去。不然达不到递归边界。但假设当前节点能够染色。是不是仅仅把当前节点染色,然后递归?这样能够过例子。但会WA,由于这样漏掉了非常多情况。这样就相当于从第一个点開始推断。然后第一个点肯定能被染色。然后再依次看其他点,总共就一种方案,但总共同拥有多种染色方案。所以说即使当前节点能够染色,也要递归不染该节点的情况。

AC的代码例如以下:

版权声明:本文博客原创文章,博客,未经同意,不得转载。

UVA Graph Coloring的更多相关文章

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

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

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

                                                               Graph Coloring Time Limit: 1000MS   Memor ...

  3. uva193 - Graph Coloring

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

  4. Graph Coloring I(染色)

    Graph Coloring I https://www.nowcoder.com/acm/contest/203/J 题目描述 修修在黑板上画了一些无向连通图,他发现他可以将这些图的结点用两种颜色染 ...

  5. poj 1419 Graph Coloring

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

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

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

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

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

  8. CF-1354 E. Graph Coloring(二分图,背包,背包方案输出)

    E. Graph Coloring 链接 n个点m条边的无向图,不保证联通,给每个点标号1,2,3.1号点个数n1,2号点个数n2,3号点个数n3.且每条边的两点,标号之差绝对值为1.如果有合法方案, ...

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

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

随机推荐

  1. TCP/IP、Http、Socket的差别

    网络由下往上分为 物理层.数据链路层.网络层.传输层.会话层.表示层和应用层. 通过初步的了解,我知道IP协议相应于网络层,TCP协议相应于传输层,而HTTP协议相应于应用层, 三者从本质上来说没有可 ...

  2. JUnit使用参数测试和一组测试

    JUnit该参数测试和一组测试使用简单 参数测试 作为替代阵列int a0,a1,a2喜欢,当测试加法assertEquals(3.0, h.add(1, 2), 0.1);相当于声明一个变量,要測试 ...

  3. C#程序(含多个Dll)合并成一个Exe

    把C#程序(含多个Dll)合并成一个Exe的超简单方法   开发程序的时候经常会引用一些第三方的DLL,然后编译生成的exe文件就不能脱离这些DLL独立运行了. 但是,很多时候我们本想开发一款只需要一 ...

  4. PHP邮件发送(转)

    php带有内置的mail() 发送邮件函数,但是较为繁琐:建议上网下载一个PHPMailer:

  5. Cocos2d-x 2.2.3 Android配置

    今天总结出来的部署流程,已经成功把自己的项目编译到android真机上.省去了安装ndk等步骤 环境: win7 64位 1.导入项目到eclipse 2.导入libcocos2dx 样例:C:\co ...

  6. 新秀学习Hibernate——一个简单的例子

    一个.Hibernate开发. 上篇博客已经为大家介绍了持久层框架的发展流程,持久层框架的种类. 为了可以使用Hibernate高速上手,我们先解说一个简单的Hibernate应用实例hibernat ...

  7. MyEclipse中将项目导出jar包,以及转化成EXE文件

    1.对于项目如何导出jar文件,和生成exe,解答目录如下: 首先生成jar文件,单击项目名称CF-users(这是我的协同过滤项目文件名称)右击--->Export如下图: 单击下一步 Sel ...

  8. HDU 4085 Steiner树模板称号

    Dig The Wells Time Limit: 6000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) To ...

  9. POJ - 2828 Buy Tickets (段树单点更新)

    Description Railway tickets were difficult to buy around the Lunar New Year in China, so we must get ...

  10. jQuery Easy UI Accordion(可伸缩的面板)包

    Accordion 可伸缩的面板组件.基于panel,示为以下的比率: <!doctype html> <html lang="en"> <head& ...