uva 193 Graph Coloring( 图染色 ) DFS+回溯

非自己出品就是容易wa啊,想了一会没想出来,就忍不住去找答案了,实在没忍住去找答案,结果还是wa了两

次,,,还是自己想的比较靠谱啊,

思路:

如果当前点可以被染成黑色,就把它染成黑色,继续深搜,之后回溯,把它染成白色

如果当前点只能被染成白色,就染成白色深搜

代码:

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
int map[105][105];
int visit[105];
int ans[105],n,max;
void dfs(int cur, int sum)
{
	int i,j;
	if(cur > n)
	{
		if(sum > max)
		{
			max = sum;
			for(i=1; i<=n; i++)
				ans[i] = visit[i];
		}
		return ;
	}
	int flag = 1;
	for(i=1; i<=n; i++)
	{
		if((map[cur][i] == 1) && (visit[i] == 1))
		{
			flag = 0;
			break;
		}
	}
	if(flag)
	{
		visit[cur] = 1;
		dfs(cur+1, sum+1);
		visit[cur] = 0;
	}
	dfs(cur+1,sum);
	return ;
}
int main()
{
	 int T,m,i,a,b;
	 scanf("%d",&T);
	 while(T--)
	 {
	 	max = 0;
	 	memset(visit,0,sizeof(visit));
	 	memset(map,0,sizeof(map));
	 	memset(ans,0,sizeof(ans));
	 	scanf("%d%d",&n,&m);
	 	for(i=1; i<=m; i++)
	 	{
	 		scanf("%d%d",&a,&b);
	 		map[a][b] = map[b][a] = 1;
	 	}
	 	dfs(1,0);
	 	printf("%d\n",max);
	 	int cnt = 0;
	 	for(i=1; i<=n; i++)
	 	{
	 		if(ans[i] == 1)
	 		{
	 			printf("%d",i);
	 			cnt++;
	 			if(cnt != max)
	 				printf(" ");
	 		}

	 	}
	 	puts("");
	 }
	 return 0;
} 

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-10-09 05:02:01

uva 193 Graph Coloring( 图染色 ) DFS+回溯的相关文章

uva 193 Graph Coloring(回溯)

uva 193 Graph Coloring 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

193 - Graph Coloring(DFS)

题目:193 - Graph Coloring 题目大意:给出一个图,图里面有点和边,要求相邻的点不可以都是黑色的,问怎样上色黑色的点最多的,给出字典序最大的那种组合情况. 解题思路:dfs每个点是黑是白,将黑的点保存记录下来,然后下次再试探某个点是黑点的时候,就看看这个点和之前的那些点有没有相邻,相邻就表示这个点不是黑点.每个试探的点只需要是这个点之后的点就可以了,因为前面的点已经试探过了. 代码: #include <stdio.h> #include <string.h> c

UVA 23 Out of 5(DFS+回溯)

Problem I 23 Out of 5 Input: standard input Output: standard output Time Limit: 1 second Memory Limit: 32 MB Your task is to write a program that can decide whether you can find an arithmetic expression consisting of five given numbers (1<=i<=5) tha

UVA How Big Is It? (DFS+回溯)

 How Big Is It?  Ian's going to California, and he has to pack his things, including his collection of circles. Given a set of circles, your program must find the smallest rectangular box in which they fit. All circles must touch the bottom of the bo

UVA - 524 Prime Ring Problem(dfs回溯法)

UVA - 524 Prime Ring Problem Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu Description A ring is composed of n (even number) circles as shown in diagram. Put natural numbers  into each circle separately, and the sum of number

UVa 524 Prime Ring Problem(DFS , 回溯)

题意  把1到n这n个数以1为首位围成一圈  输出所有满足任意相邻两数之和均为素数的所有排列 直接枚举排列看是否符合肯定会超时的  n最大为16  利用回溯法 边生成边判断  就要快很多了 #include<cstdio> using namespace std; const int N = 50; int p[N], vis[N], a[N], n; int isPrime(int k) { for(int i = 2; i * i <= k; ++i) if(k % i == 0)

UVA Graph Coloring

主题如以下: Graph Coloring  You are to write a program that tries to find an optimal coloring for agiven graph. Colors are applied to the nodes of the graph and the only availablecolors are black and white. The coloring of the graph is called optimalif a

uva 11218 KTV(DFS+回溯)

uva 11218 KTV One song is extremely popular recently, so you and your friends decided to sing it in KTV. The song has 3 characters, so exactly 3 people should sing together each time (yes, there are 3 microphones in the room). There are exactly 9 peo

POJ1419 Graph Coloring(最大独立集)(最大团)

Graph Coloring Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 4926   Accepted: 2289   Special Judge 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