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 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 ,
, and a set of undirected edges denoted by pairs of node numbers
,
. 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

1
6 8
1 2
1 3
2 4
2 5
3 4
3 6
4 6
5 6

Sample Output

3
1 4 5

题目大意:给定n个节点,节点的编号为1-n,在给定m个节点链接的信息,现在要求对节点图色,只有两种颜色可以黑色和白色并且相邻的节点不能同时为黑色但是可以为白色,要求黑色节点最多的个数,以及一组节点的编号

解题思路:

刚开始所有点没有着色,且最终结果至少有一个点被着黑色(一个点时直接着黑色,多个点时,可以任选一个点为黑色,其余点全为白色);

枚举这个黑色的点,并且把与它相邻的点都着白色,剩下的可以看作是一个相同的子问题了,因为剩下的点都不与这个黑色的点相邻。

最优解满足:每个白色的点至少与一个黑色的点相邻(如果这个点相邻的都是白色,可以把它改为黑色),且每个黑色的点周围都是白色。

#include<stdio.h>
#include<string.h>
int gra[105][105], A[105], B[105], ans;
int n, m;
void DFS(int d, int num) {
	int temp[105], cnt;
	if (d > ans && num >= n) {
		ans = d;
		memcpy(B, A, sizeof(B));
		return;
	}
	for (int i = 1; i <= n; i++) {
		if (A[i] == 0) {
			A[i] = 2;
			cnt = 0;
			temp[cnt++] = i;
			for (int j = 1; j <= n; j++) {
				if (gra[i][j] && !A[j]) {
					A[j] = 1;
					temp[cnt++] = j;
				}
			}
			DFS(d + 1, num + cnt);
			while (cnt) {
				A[temp[--cnt]] = 0;
			}
		}
	}
	return;
}
int main() {
	int T;
	scanf("%d", &T);
	while (T--) {
		memset(gra, 0, sizeof(gra));
		memset(A, 0, sizeof(A));
		memset(B, 0, sizeof(B));
		scanf("%d %d", &n, &m);
		int a, b;
		ans = 0;
		for (int i = 0; i < m; i++) {
			scanf("%d %d", &a, &b);
			gra[a][b] = 1;
			gra[b][a] = 1;
		}
		DFS(0, 0);
		printf("%d\n", ans);
		int flag = 1;
		for (int i = 1; i <= n; ++i) {
			if (B[i] == 2)
			{
				if (flag) flag = 0;
				else putchar(' ');
				printf("%d", i);
			}
		}
		printf("\n");
	}
	return 0;
}
时间: 2024-10-10 23:32:58

uva 193 Graph Coloring(回溯)的相关文章

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

193 - Graph Coloring(DFS)

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

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

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

UVA 1479 - Graph and Queries(Treap)

UVA 1479 - Graph and Queries 题目链接 题意:给定一个n个结点m条边的无向图,每个结点一个权值,现在有3种操作 D x,删除id为x的边 Q x k 计算与x结点的连通分量中第k大的数字,不存在就是0 C x v 把x结点权值改为v 要求计算所有Q操作的和除以Q操作的次数的值 思路:Treap的经典题,进行离线操作,把操作全部逆向进行,删边就可以转化为加边,就可以利用并查集,那么维护第k大就利用Treap 代码: #include <cstdio> #include

uva 1479 - Graph and Queries(伸展树)

题目链接:uva 1479 - Graph and Queries 题目大意:有一张m条边的无向图,每个节点都有一个权值,现在有若干个操作, D x:删除ID为x的节点 Q x k:计算与节点x联通的节点当中,第k大的权值 C x v:把节点x的权值改为v 解题思路:把所有操作反过来处理,先执行所有的D操作,获得最终的图,然后逆操作的时候对于D来说即为合并操作,Q和C则是查询和修改操作. #include <cstdio> #include <cstring> #include &

GPS-Graph Processing System Graph Coloring算法分析 (三)

Graph coloring is the problem of assigning a color to each vertex of an undirected graph such that no two adjacent vertices have the same color. We implement the greedy algorithm from Scalable parallel graph coloring algorithms. The algorithm iterati

uva193 - Graph Coloring

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 a maxim

【POJ 1419】Graph Coloring

[POJ 1419]Graph Coloring 求图的最大独立集 最大独立集=补图最大团 很适合理解最大团/最大独立集的模板题 建立补图套模板既可 需要输出集合点 原本想用stack 但发现copy比较麻烦 vector用一个iterator指针 循环很便利 代码如下: #include <iostream> #include <cstdlib> #include <cstdio> #include <cstring> #include <vecto