uva 193 Graph Coloring(图染色 dfs回溯)
Description
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 tex2html_wrap_inline33 , tex2html_wrap_inline35 , and a set of undirected edges denoted by pairs of node numbers tex2html_wrap_inline37 , tex2html_wrap_inline39 . 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
Sample Output
题意: 一开始题意理解错了。题意是这样的,给出一个图,让你染成黑白两种颜色,但是相连的两点不能都染成黑色,问最多能染色多少的黑色点。
思路:直接dfs回溯,庆幸的是没有超时。dfs(int u/*u表示当前正在判断的点*/,int num/*num表示黑点的数量*/),u初值为1,num初值为0,然后分为两种情况,第一种情况先判断与当前点u相连的点有没有染成黑色的,如果有的话直接把当前点u染成白色,(即dfs(u+1,num);return;),否则进入第二种情况:将当前点u染成黑色或者白色。
if(u>=n+1)边界限制,ans取更大值。最后输出的格式注意。
AC代码:
#pragma comment(linker, "/STACK:1024000000,1024000000")
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<math.h>
#include<algorithm>
#include<queue>
#include<set>
#include<bitset>
#include<map>
#include<vector>
#include<stdlib.h>
#include <stack>
using namespace std;
#define PI acos(-1.0)
#define max(a,b) (a) > (b) ? (a) : (b)
#define min(a,b) (a) < (b) ? (a) : (b)
#define ll long long
#define eps 1e-10
#define MOD 1000000007
#define N 106
#define inf 1e12
int n,m;
int mp[N][N];
int ans;
int color[N];
int res[N];
void dfs(int u/*u表示当前正在判断的点*/,int num/*num表示黑点的数量*/){
if(u>=n+){
if(ans<num){
ans=num;
for(int i=;i<=n;i++){
res[i]=color[i];
}
}
return;
} for(int i=;i<=n;i++){
if(mp[u][i] && color[i]){
dfs(u+,num);//只能染成白色的情况,否则下面的情况能染成黑色或白色
return;
}
}
color[u]=;
dfs(u+,num+);//能染成黑色 color[u]=;
dfs(u+,num);//能染成白色 }
int main()
{
int t;
scanf("%d",&t);
while(t--){
memset(mp,,sizeof(mp));
scanf("%d%d",&n,&m);
for(int i=;i<m;i++){
int a,b;
scanf("%d%d",&a,&b);
mp[a][b]=mp[b][a]=;
}
ans=;
memset(color,,sizeof(color));
dfs(,); printf("%d\n",ans);
int w=;
for(int i=;i<=n;i++){
if(res[i]){
w++;
printf("%d",i);
if(w!=ans){
printf(" ");
}
} }
printf("\n"); }
return ;
}
uva 193 Graph Coloring(图染色 dfs回溯)的更多相关文章
- UVA 193 Graph Coloring 图染色 DFS 数据
题意:图上的点染色,给出的边的两个点不能都染成黑色,问最多可以染多少黑色. 很水的一题,用dfs回溯即可.先判断和当前点相连的点是否染成黑色,看这一点是否能染黑色,能染色就分染成黑色和白色两种情况递归 ...
- Graph Coloring I(染色)
Graph Coloring I https://www.nowcoder.com/acm/contest/203/J 题目描述 修修在黑板上画了一些无向连通图,他发现他可以将这些图的结点用两种颜色染 ...
- UVA - 524 Prime Ring Problem(dfs回溯法)
UVA - 524 Prime Ring Problem Time Limit:3000MS Memory Limit:0KB 64bit IO Format:%lld & % ...
- nowcoder 203J Graph Coloring I(dfs)
题目链接 题目描述 修修在黑板上画了一些无向连通图,他发现他可以将这些图的结点用两种颜色染色,满足相邻点不同色. 澜澜不服气,在黑板上画了一个三个点的完全图.修修跟澜澜说,这个图我能找到一个简单奇环. ...
- UVA 1613 K度图染色
题目 \(dfs+\)证明. 对于题目描述,可以发现\(K\)其实就是大于等于原图中最大度数的最小奇数,因为如果原图度数最大为奇数,则最多颜色肯定为K,而如果原图最大度数为偶数,则\(K\)又是奇数, ...
- UVa 524 Prime Ring Problem(DFS , 回溯)
题意 把1到n这n个数以1为首位围成一圈 输出全部满足随意相邻两数之和均为素数的全部排列 直接枚举排列看是否符合肯定会超时的 n最大为16 利用回溯法 边生成边推断 就要快非常多了 #inc ...
- Codeforces 664D Graph Coloring 二分图染色
题意: 一个无向图的每条边为红色或蓝色,有这样一种操作:每次选一个点,使与其相邻的所有边的颜色翻转. 求解是否可以经过一系列操作使所有的边颜色相同,并输出最少操作次数和相应的点. 分析: 每个点要么选 ...
- 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 ...
随机推荐
- Nginx 变量漫谈(五)
前面在 (二) 中我们已经了解到变量值容器的生命期是与请求绑定的,但是我当时有意避开了“请求”的正式定义.大家应当一直默认这里的“请求”都是指客户端发起的 HTTP 请求.其实在 Nginx 世界里有 ...
- 阿里云 配置apache+python+django 环境 适合菜鸟
云服务器环境:阿里云服务:20G+1M带宽+centos+512M 一.python安装: 刚开始没有好好利用centos 的yum方法.采用wget从python官网上下载的2.7.5版本.解压安装 ...
- 实测Eclipse连接小米2S调试问题
小米2S手机在Eclipse真机调试时,设备选择列表无法显示手机,DDMS也连接不上设备,解决步骤: 1.打开手机设置中开发者选项 - USB调试开启: 2.保证小米2S手机Windows下设备驱动已 ...
- mode(思维,注意内存)
mode Time Limit:1000MS Memory Limit:1024KB 64bit IO Format:%lld & %llu Submit Status Pra ...
- DEDE列表页调用TAG标签
[field:id function=GetTags(@me)/] 标签就可以调用出来了 只不过不带连接的,如果需要连接,请注释include\helpers\archive.helper.php文件 ...
- public void Delete<T>(List<T> EntityList) where T : class, new() 这是什么意思
就是说T必须是一个类(class)类型,不能是结构(structure)类型. 这是类型参数约束,.NET支持的类型参数约束有以下五种: where T : struct ...
- Stopwatch 类【转】
一般我们想要测试使用那种方法或着那种类型效率更高,使用Stopwatch类进行测试就可以,我也是现在才知道,汗一个. 先来看个小示例,如下. 前提,先引用using System.Diagnostic ...
- paip.oracle query export to insert sql
paip.oracle query export to insert sql 作者Attilax , EMAIL:1466519819@qq.com 来源:attilax的专栏 地址:http:/ ...
- Linux之旅(1): diff, patch和quilt (下)
Linux之旅(1): diff, patch和quilt (下) 2 quilt 我们自己的项目能够用cvs或svn管理所有代码.但有时我们要使用其它开发人员维护的项目.我们须要改动一些文件,但又不 ...
- PHP 生成UUID的方法
. . . ...