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 ...
随机推荐
- 2014第8周三杂记及web标准学习
昨天遇到一个问题,安卓中mp3默认打开方式的设置,本来如果直接用播放器来查找文件打开没问题,但不知为何播放器只能在历史文件夹中查找,那么在ES文件管理器中找到对应mp3文件后却总是被默认的ES播放器打 ...
- How ASP.NET MVC Works?
原文地址:http://www.cnblogs.com/artech/archive/2012/04/10/how-mvc-works.html?ADUIN=7783008&ADSESSION ...
- sql 数据库 庞大数据量 需要分表
17:04:05问下 在什么情况下 审核分区啊 ~..大熊..o○ 17:06:53这个要看应用~..大熊..o○ 17:07:37比如数据量很大,查询多是按照时间段查询,就可以用时间段来做分区~.. ...
- Iptabels详解
http://www.07net01.com/2016/02/1291283.html Iptabels是与linux内核集成的包过滤防火墙系统,几乎所有的linux发行版本都会包含Iptables的 ...
- hdu 5506 GT and set(dfs爆搜)
Problem Description You are given N sets.The i−th set has Ai numbers.You should divide the sets into ...
- XP用户:消除误解,大胆拥抱Linux
4月23日.知名家评论家Silviu Stahie发表文章.题为"Windows Users and Their Misconceptions About Linux". ...
- ADO.NET DataSet、DataTable、DataRow、DataView的学习
对于一个datatable中的数据进行嵌套查询判断某几列数据是否相同从而确定这条数据是否一样,并确定他重复的次数COUNT1字段. 例如: DataTable dt = new DataTable() ...
- 百度编辑器ueditor 在vs2008中的使用方法
个人觉得百度编辑器ueditor还是不错的,虽然出生的时间比较短,但某些方面相比其它富文本编辑器更优秀,免费.可定制等等. 由于在官方下载的ueditor包是在vs2012下开发的,可以在vs2010 ...
- Android 四大组件之Activity生命周期
写这篇博文之前,已经对android有一定的了解和认识.这篇博文主要讲述android的Activity的生命周期,这是android开发者必须掌握的知识.android的Activity组件拥有7个 ...
- 详解Activity的四种启动模式
在Android中每个界面都是一个Activity,切换界面操作其实是多个不同Activity之间的实例化操作.在Android中Activity的启动模式决定了Activity的启动运行方式. Ac ...