UVA Graph Coloring
主题如以下:
| 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的更多相关文章
- POJ 1419 Graph Coloring(最大独立集/补图的最大团)
Graph Coloring Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 4893 Accepted: 2271 ...
- POJ1419 Graph Coloring(最大独立集)(最大团)
Graph Coloring Time Limit: 1000MS Memor ...
- uva193 - Graph Coloring
Graph Coloring You are to write a program that tries to find an optimal coloring for a given graph. ...
- Graph Coloring I(染色)
Graph Coloring I https://www.nowcoder.com/acm/contest/203/J 题目描述 修修在黑板上画了一些无向连通图,他发现他可以将这些图的结点用两种颜色染 ...
- poj 1419 Graph Coloring
http://poj.org/problem?id=1419 题意: 一张图黑白染色,相邻点不能都染黑色,最多能染几个黑色点 最大点独立集 但是图不能同构为二分图,不能用二分图匹配来做 那就爆搜吧 还 ...
- 【POJ】1419:Graph Coloring【普通图最大点独立集】【最大团】
Graph Coloring Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 5775 Accepted: 2678 ...
- GPS-Graph Processing System Graph Coloring算法分析 (三)
HamaWhite 原创,转载请注明出处!欢迎大家增加Giraph 技术交流群: 228591158 Graph coloring is the problem of assignin ...
- CF-1354 E. Graph Coloring(二分图,背包,背包方案输出)
E. Graph Coloring 链接 n个点m条边的无向图,不保证联通,给每个点标号1,2,3.1号点个数n1,2号点个数n2,3号点个数n3.且每条边的两点,标号之差绝对值为1.如果有合法方案, ...
- uva 193 Graph Coloring(图染色 dfs回溯)
Description You are to write a program that tries to find an optimal coloring for a given graph. Col ...
随机推荐
- ASP.NET5 Beta8
ASP.NET5 Beta8 ASP.NET5 beta8现已上都的NuGet作为一个工具升级到Visual Studio2015!此版本极大地扩展.NET核心对OS X和Linux所支持的范围.您现 ...
- SQL入门学习0-数据库与SQL
1.1 DBMS DatabaseManagermentSystem 数据库管理系统 DBMS种类 层次型数据库(HDB) 最古老的数据库之一,把数据通过层次结构的方式表现. 关系型数据库(RDB) ...
- MySQL之终端(Terminal)管理MySQL
原文:MySQL之终端(Terminal)管理MySQL 前言:MySQL有很多的可视化管理工具,比如“mysql-workbench”和“sequel-pro-”. 现在我写MySQL的终端命令操作 ...
- ios 串
1.是推断包括字符串 if( [str rangeOfString:@"hello"].location != NSNotFound) { NSLog(@"yes&quo ...
- Cache
在选择的几点思考
Cache为缓解DB装有重要数据,下面经常使用memcached和redis要总结,促进技术的选择. 1 memcached (1) 有限支持的操作,持经常使用的set.get.delete和过期删 ...
- 编写高质量代码改善C#程序的157个建议[1-3]
原文:编写高质量代码改善C#程序的157个建议[1-3] 前言 本文主要来学习记录前三个建议. 建议1.正确操作字符串 建议2.使用默认转型方法 建议3.区别对待强制转换与as和is 其中有很多需要理 ...
- zzu--2014年11月16日月潭赛 C称号
1230: Magnets Time Limit: 1 Sec Memory Limit: 128 MB Submit: 24 Solved: 13 [id=1230" style=&q ...
- android 有效载荷大图,避OOM
我们的项目往往会载入图片.有时,承担太多,再装图片,它导致了非常小的程序卡,而在铅oom从而导致异常app再见,今天翻译google官方网站,它已经做了很好的图像处理汇总,由于Google我们已经给解 ...
- 一旦rhel5.8造成只读文件系统ORA-00354: corrupt redo log block header
一旦rhel5.8造成只读文件系统ORA-00354: corrupt redo log block header Wed Jun 03 03:31:24 CST 2015 Thread 1 adva ...
- aul 学习测试(测量)
-------------------aul5 ----------test0------------------------- select file#,rfile#,name from v$dat ...