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 ...
随机推荐
- poj1364(差分约束系统)
poj1364 设s[i] 表示a1 + a2 + ... + a(i-1)的和 给我们n个点,m条约束 如果是a b gt c 那么表示 s[a+b+1] - s[a] > c ...
- 小米2S TWRP 3.0.2-0 最新版Recovery
主界面 使用了我最新修改的内核 下载地址: 链接: http://pan.baidu.com/s/1i5xwddb 密码: 7dyb 验证信息: md5sum: dca410f33020eb87986 ...
- 如何使用 yum 安装/更新/移除 软件
如何使用 yum 安装/更新/移除 软件 一. 建立仓库(repository)和源 a) 拷贝所以相关rpm包到某个目录 b) 执行createrepo /目录/目录/目录/目录 注意:b)中 ...
- 从最大似然到EM算法浅解
从最大似然到EM算法浅解 zouxy09@qq.com http://blog.csdn.net/zouxy09 机器学习十大算法之中的一个:EM算法.能评得上十大之中的一个,让人听起来认为挺NB的. ...
- svn加入新的文件夹
方法一: 1.在远程server上生成新的文件夹 svn mkdir http://svn.xxx.com/svn/mobile/strategy/assistant/branches/talk -m ...
- POJ 2538 WERTYU水的问题
[题目简述]:题意非常easy,没有trick. [分析]:事实上这题还是挺有趣的,在 算法竞赛入门经典中也有这一题. 详见代码: // 120K 0Ms /* 边学边做 -- */ // 字符串:W ...
- windows中间vmware的Linux系统安装jdk步骤
1.设置文件的享受,对于本地阅读windows档 于vmware虚拟机设置共享文件夹,那么共享文件中,你可以 2.然后打开虚拟机上,使用root输入账户,然后,在夹/mnt/hgfs/ 共享文件了 ...
- 编译命令行终端 swift
So, this is where swift lives, after you've installed XCode 6 Beta: /Applications/Xcode6-Beta.app/Co ...
- List、Map和Set实现类
List.Map和Set实现类 1.List实现类 (1)ArrayList (2)Vector 2.Map实现类 (1)HashMap (2)Hashtable 3.Set实现类 (1)HashSe ...
- 注意事项: Solr设备 Hello World
试用 Solr-4.10.2 一 shards, 这两款机器 一是垃圾 rm -r example/solr/collection1/data/* 启动一个 node cd example java ...