uva193 - Graph Coloring
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: 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 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.
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
求图的最大独立集,即把尽量多的结点图黑使得任意两个黑点不相邻
#include<cstdio>
#include<cstring>
#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
const int maxn=100+5;
int m,n,k;
vector<int> G[maxn];
int node[maxn], black_node[maxn];
int ans;
enum {white, black}; void dfs(int d, int cnt) {
if(d==n) {
if(cnt>ans) {
int j=0;
for(int i=0;i<n;i++) if(node[i]) black_node[j++]=i+1;
ans=cnt;
}
return;
} if(n-d+cnt<=ans)
return;
//尝试黑的
int ok=true;
node[d]=black;
for(int i=0;i<G[d].size();i++) {
int v=G[d][i];
//两个相邻黑点
if(node[v]==black) {
ok=false;
break;
}
}
if(ok) dfs(d+1, cnt+1); //尝试白的
node[d]=white;
dfs(d+1, cnt);
} int main()
{
#ifndef ONLINE_JUDGE
freopen("./uva193.in", "r", stdin);
#endif
int x,y;
cin>>m;
while(m--) {
cin>>n>>k;
memset(G, 0, sizeof(G));
memset(node, 0, sizeof(node));
ans=0;
for(int i=0;i<k;i++) {
cin>>x>>y;
x--;y--;
G[x].push_back(y);
G[y].push_back(x);
}
dfs(0, 0);
printf("%d\n", ans);
for(int i=0;i<ans-1;i++)
printf("%d ", black_node[i]);
printf("%d\n", black_node[ans-1]); } return 0;
}
uva193 - 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 ...
- UVA Graph Coloring
主题如以下: Graph Coloring You are to write a program that tries to find an optimal coloring for agiven ...
- 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 ...
随机推荐
- explain SQL语句性能检测
一般用来分析sql语句如何执行,使用了那些键 mysql>explain select * from table;+----+-------------+-------+------+----- ...
- Linux进程调度策略
linux内核的三种主要调度策略: 1,SCHED_OTHER 分时调度策略, 2,SCHED_FIFO实时调度策略(先到先服务)3,SCHED_RR实时调度策略(时间片轮转) 实时进程将得到优先调用 ...
- ORACLE常用脚本示例
create table DBO.INDEX_POLICY_TBL( ID NUMBER(10) NOT NULL PRIMARY KEY, POLICY_ID NUMBER(10,0) defaul ...
- ADO.NET+Access: 1,标准表达式中数据类型不匹配
ylbtech-Error-ADO.NET+Access: 1,标准表达式中数据类型不匹配. 1.A,错误代码返回顶部 1,标准表达式中数据类型不匹配. 1.B,出错原因分析返回顶部 未解决 1. ...
- C# winform打印总结 z
http://blog.csdn.net/jing_xin/article/details/41444063 针对BEIYANG收据打印机 BTP-R580测试通过. 操作说明:http://www. ...
- java 判断两个时间段是不是有交集
如上图:X Y Z 分别为传来的开始时间可能位于数据库中时间段的位置. X有三种可能 即传来的开始时间为与数据可中某条数据的开始位置! 这样他的结束时间就有三种可能 1.位于 ...
- org.unsaved transient instance - save the transient instance before flushing: bug解决方案
最近开发和学习过程中,遇到很多零碎的知识点,在此简单地记录下: 1.遇如下bug: org.unsaved transient instance - save the transient instan ...
- windows下mysql5.7安装及配置
装完msi后,复制my-default.ini文件,黏贴为my.ini文件,内容修改如下: # For advice on how to change settings please see# htt ...
- 《深入理解C#》第3版 学习进度备忘
学习资源:<深入理解C#>第3版 知识基础支持: <C# in a nutshell> O Reilly出版社,是一本从头介绍C#的优秀图书.<Essential C#5 ...
- php获取上传多个文件缺失
我们的一个页面编辑发布后台出现了图片无法上传保存的情况,经过调试对比发现,原来是file表单数量过多导致,减少file表单的数量即可上传成功.为了满足需求不减少file表单数并保证上传成功,于是更改了 ...