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 ...
随机推荐
- Sql Server专题一:索引(下)
首先这次的内容是全文索引,跟前面讲的其实没有多大关系 两种索引的功能和结构都是不同的,普通索引的结构主要以B+树和哈希索引为主,用于实现对字段中数据的精确查找,比如查找某个字段值等于给定值的记录,A= ...
- opencv菜鸟学习之旅cvNorm
Norm 计算数组的绝对范数, 绝对差分范数或者相对差分范数 double cvNorm( const CvArr* arr1, const CvArr* arr2=NULL, int norm_ty ...
- 一个.java文件中可以有几个同级类
1.在一个.java文件中可以有几个类.修饰符只可以public abstract final和无修饰符,不能是其他的private等修饰符.2.public修饰的只能有一个,且要与文件名相同 若没有 ...
- openjpa框架入门_openbooks项目Overview(四)
- word2vec 入门基础(一)
一.基本概念 word2vec是Google在2013年开源的一个工具,核心思想是将词表征映 射为对应的实数向量. 目前采用的模型有一下两种 CBOW(Continuous Bag-Of-Words, ...
- hdu4331 Image Recognition 就暴力啊。。啊。。
题意: 给一个1和0组成的正方形矩阵,求 四条边都由1构成的正方形的个数. 方法: 先统计矩阵中每一点,向四个方向,最多有多少个连续的1,这里用dp做也 与此同时,顺便求下 能向右下和 左上 两个方向 ...
- 胜利大逃亡(续)(状态压缩bfs)
胜利大逃亡(续) Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total S ...
- python中pip的使用和安装
Ubuntu下安装pip的方法 安装pip的方法: Install pip and virtualenv for Ubuntu 10.10 Maverick and newer $ sudo ...
- jquery 设置asp:dropdownlist 选中项
$("#ddlPro").find('option').each(function () { this.selected = (this.text == dlprom); });
- 取得网站的IP 地址
select utl_inaddr.get_host_address('smtp.163.com') ipaddress from dual;