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 ...
随机推荐
- Windows下PHP+Eclipse开发环境搭建 及错误解决(apache2.2服务无法启动 发生服务特定错误:1)
前言 Eclipse与php/apache的关系:Eclipse只是用来写代码的,如果想要在浏览器查看运行效果就要让php/apache的运行目录指向你的代码目录.Eclipse貌似不会自己和apac ...
- T-SQL:SQL Server-数据库查询语句基本查询
ylbtech-SQL Server-Basic:SQL Server-数据库查询语句基本查询 SQL Server 数据库查询语句基本查询. 1,数据库查询语句基本查询 数据库 SQL Serv ...
- C++ 虚拟继承
1.为什么要引入虚拟继承 虚拟继承是多重继承中特有的概念.虚拟基类是为解决多重继承而出现的.如:类D继承自类B1.B2,而类B1.B2都继 承自类A,因此在类D中两次出现类A中的变量和函数.为了节省内 ...
- js保留小数点后N位的方法介绍
js保留小数点后N位的方法介绍 利用toFixed函数 代码如下 复制代码 <script language="javascript"> document.write( ...
- JS操作文件
) ; ; fso ); f1.Close(); // 读取文件的内容. // Response.Write("Reading file <br>") ...
- InputFormat 总结
在mr中的输入类,常见的InputFormat是TextInputFormat,也是mr默认的文件处理类,处理普通文本文件,作用是 1 每一行作为一个记录,将每一行在文件中的起始偏移量做为key,内容 ...
- RPC基础篇
RPC概念 RPC(Remote Procedure Call Protocol)——远程过程调用协议, 是一种进程间通信方式.它允许程序调用另一个地址空间(通常是共享网络的另一台机器上)的过程或函数 ...
- kali update can’t found win7 loader
安装win7,kali ,双系统,更新 kali 系统后, grub 找不到win7 ,无法进入win7系统. 解决: grub升级以后为grub2, grub2 默认不能识别win7, 更新一下,即 ...
- 为operamasks增加HTML扩展方式的组件调用
#为operamasks增加HTML扩展方式的组件调用 ##背景 之前的[博文](http://www.cnblogs.com/p2227/p/3540858.html)中有提及到,发现easyui中 ...
- Python程序的混淆和加密
混淆 为了增加代码阅读的难度, 源代码的混淆非常必要, 一个在线的Python代码混淆网站. 如果你觉得有用, 可以购买离线版本.同时需要注意的是, 这个混淆其实还是被很多人怀疑的, 因为即使混淆了, ...