UVA 193 Graph Coloring 图染色 DFS 数据
题意:图上的点染色,给出的边的两个点不能都染成黑色,问最多可以染多少黑色。
很水的一题,用dfs回溯即可。先判断和当前点相连的点是否染成黑色,看这一点是否能染黑色,能染色就分染成黑色和白色两种情况递归,如果不能就单递归白色。
代码:
#include <cstdio>
#include <cstring>
const int maxn = 110;
int cas, v, e, M;
bool g[maxn][maxn];
int color[maxn], rec[maxn]; void dfs(int p, int black) {
if (p > v) {
if (M < black) {
M = black;
for (int i = 1; i <= v; i++)
rec[i] = color[i];
}
return;
}
//judge if can color
for (int i = 1; i <= v; i++)
if (g[p][i] && color[i]) {
dfs(p + 1, black); //can't color black, color white and return
return;
}
color[p] = 1;
dfs(p + 1, black + 1); //can color black
color[p] = 0;
dfs(p + 1, black); //can color white
} int main() {
scanf("%d", &cas);
while (cas--) {
M = 0;
scanf("%d%d", &v, &e);
memset(g, 0, sizeof(g));
for (int i = 0; i < e; i++) {
int a, b;
scanf("%d%d", &a, &b);
g[a][b] = g[b][a] = 1;
}
dfs(1, 0);
printf("%d\n", M);
int cnt = 0;
for (int i = 1; i <= v; i++)
if (rec[i]) {
if (++cnt != M)
printf("%d ", i);
else
printf("%d\n", i);
}
}
return 0;
}
Input:
4 5 0 8 4
1 2
3 4
5 6
6 8 2 1
1 2 20 19
1 10
2 5
3 4
4 9
5 17
6 4
8 19
9 13
10 11
11 14
12 1
13 6
14 3
15 4
16 5
17 8
18 9
19 15
20 4
Output:
5
1 2 3 4 5
5
2 4 5 7 8
1
2
11
1 2 3 6 7 8 9 11 15 16 20
UVA 193 Graph Coloring 图染色 DFS 数据的更多相关文章
- uva 193 Graph Coloring(图染色 dfs回溯)
Description You are to write a program that tries to find an optimal coloring for a given graph. Col ...
- Graph Coloring I(染色)
Graph Coloring I https://www.nowcoder.com/acm/contest/203/J 题目描述 修修在黑板上画了一些无向连通图,他发现他可以将这些图的结点用两种颜色染 ...
- nowcoder 203J Graph Coloring I(dfs)
题目链接 题目描述 修修在黑板上画了一些无向连通图,他发现他可以将这些图的结点用两种颜色染色,满足相邻点不同色. 澜澜不服气,在黑板上画了一个三个点的完全图.修修跟澜澜说,这个图我能找到一个简单奇环. ...
- Codeforces 664D Graph Coloring 二分图染色
题意: 一个无向图的每条边为红色或蓝色,有这样一种操作:每次选一个点,使与其相邻的所有边的颜色翻转. 求解是否可以经过一系列操作使所有的边颜色相同,并输出最少操作次数和相应的点. 分析: 每个点要么选 ...
- UVA 1613 K度图染色
题目 \(dfs+\)证明. 对于题目描述,可以发现\(K\)其实就是大于等于原图中最大度数的最小奇数,因为如果原图度数最大为奇数,则最多颜色肯定为K,而如果原图最大度数为偶数,则\(K\)又是奇数, ...
- UVA Graph Coloring
主题如以下: Graph Coloring You are to write a program that tries to find an optimal coloring for agiven ...
- 【POJ】1419:Graph Coloring【普通图最大点独立集】【最大团】
Graph Coloring Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 5775 Accepted: 2678 ...
- poj 1419 Graph Coloring
http://poj.org/problem?id=1419 题意: 一张图黑白染色,相邻点不能都染黑色,最多能染几个黑色点 最大点独立集 但是图不能同构为二分图,不能用二分图匹配来做 那就爆搜吧 还 ...
- URAL 1080 Map Coloring(染色)
Map Coloring Time limit: 1.0 secondMemory limit: 64 MB We consider a geographical map with N countri ...
随机推荐
- 网络协议——IP
IPv4地址 不论什么网络设备能够经过一个网络接口卡(NIC)接入网,假定该设备要能够访问的其它设备,然后该卡必须有一个唯一的地址.候接入多个网络,相应地该设备就有多个地址.假设这个设备是主机的话.一 ...
- [转]C#自定义开关按钮控件--附带第一个私活项目截图
原文地址:http://www.cnblogs.com/feiyangqingyun/archive/2013/06/15/3137597.html 进入智能手机时代以来,各种各样的APP大行其道,手 ...
- Web服务器Raspkate的RESTful API
基于轻量型Web服务器Raspkate的RESTful API的实现 在上一篇文章中,我们已经了解了Raspkate这一轻量型Web服务器,今天,我们再一起了解下如何基于Raspkate实现简单的RE ...
- Vijos P1881 闪烁的星星 (加强自己多一点。。)
假设每次查询不是整个长度,但[x, y]此时间间隔. . 闲来无事写的,感觉是正确的.这将成为合并范围. #include <cstdio> #include <cstring> ...
- Javascript设计模式系列三
继承,一个类或对象继承另一个类或对象的三种方法.类式继承.原型式继承.掺元类. 一.类式继承,原型链.Extend函数. <script type="text/javascript&q ...
- iOS缓存类的设计
使用执行速度缓存的程序可以大大提高程序,设计一个简单的缓存类并不需要太复杂的逻辑. 只需要一个简单的3接口. 存款对象 以一个对象 删除对象 阅读对象 watermark/2/text/aHR0cDo ...
- POJ2239 Selecting Courses【二部图最大匹配】
主题链接: http://poj.org/problem?id=2239 题目大意: 学校总共同拥有N门课程,而且学校规定每天上12节可,一周上7天. 给你每门课每周上的次数,和哪一天哪一节 课上的. ...
- IOS开展:导航中添加多个button并加入左侧logo
添加多个button,同样只能加入一个 UIBarButtonItem *anotherButton = [[UIBarButtonItem alloc] initWithTitle:NSLocali ...
- 乐在其中设计模式(C#) - 桥接模式(Bridge Pattern)
原文:乐在其中设计模式(C#) - 桥接模式(Bridge Pattern) [索引页][源码下载] 乐在其中设计模式(C#) - 桥接模式(Bridge Pattern) 作者:webabcd 介绍 ...
- sql server 数据库 只有mdf文件,如何附加
直接附加就可以,SQL 会提示LOG文件找不到,可删除提示的错误行,只附加MDF文件,附加成功后,SQL会自动创建LOG文件.