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 ...
随机推荐
- Socket编程实践(13) --UNIX域名协议
UNIX域名协议 UNIX域套接字与TCP相比, 在同一台主机上, UNIX域套接字更有效率, 差点儿是TCP的两倍(因为UNIX域套接字不须要经过网络协议栈,不须要打包/拆包,计算校验和,维护序号和 ...
- Android开发中验证码的生成
近期在做电商金融类的项目,验证码的生成方法不可缺少.先学习了一种.经过測试好用.从别处学习的代码,稍修改了一下可选择是否支持识别大写和小写.直接上代码. import android.app.Acti ...
- profile与bashrc
/etc/profile./etc/bashrc 是系统全局环境变量设定 ~/.profile,~/.bashrc用户家文件夹下的私有环境变量设定 当登入系统时候获得一个shell进程时.其读取环境设 ...
- 性能是全新的 SEO
作为一个前端project师,那不只就是公开地处理那些美丽的html5, css3 和javascript特效.小而重要的一部分工作就是要让项目朝着代码稳定和代码标准方向进展.设计.信息结构以及后台限 ...
- iOS 真机调试(最具体的步骤来解决历史,hmt精心打造)
/*************************************************************1************************************* ...
- 几种流行Webservice控制框架
转会[http://blog.csdn.net/thunder4393/article/details/5787121],写的非常好,以收藏. 1 摘要 开发webservice应用程序中 ...
- post跨域请求
[名词解释] 跨域:https://developer.mozilla.org/en-US/docs/JavaScript/Same_origin_policy_for_JavaScript 同源策略 ...
- HDU 5050 Divided Land(进制转换)
题意 给你两个二进制数m,n 求他们的最大公约数 用二进制表示 0<m,n<2^1000 先把二进制转换为十进制 求出最大公约数 再把结果转换为二进制 数比較大要用到大数 ...
- 华为-on演习--身高找到最好的二人
称号: 从5个人选择2作为个人礼仪器.中的每个个体的身高的范围160-190,要求2各高度差值至少(假设差异值同样的事情,他们中最高的选择),输出的两个人的身高升序. Smple input:161 ...
- web即时通讯2--基于Spring websocket达到web聊天室
如本文所用,Spring4和websocket要构建web聊天室,根据框架SpringMVC+Spring+Hibernate的Maven项目,后台使用spring websocket进行消息转发和聊 ...