POJ-1419(Graph Coloring)

【题目意思】

把给定图涂颜色,问黑色点的最大个数

补图的最大独立团,可以用dfs做

int m[101][101];
int color[101];
int ans[101];
int a, b, maxn, black;
void process()
{
	int i;
	if (black > maxn) {
		maxn = black;
		for (i = 1; i <= a; ++i)
			ans[i] = color[i];
	}
}
void dfs(int p)
{
	int i, able = 1;
	for (i = 1; i <= a; ++i) {
		if (m[i][p] && color[i] && i != p) able = 0;
	}
	if (able) {
		color[p] = 1;
		++black;
		if (p < a) dfs(p + 1);
		else process();
		color[p] = 0;
		--black;
	}
	if (p < a) dfs(p + 1);
	else process();
}
int main()
{
	int cases;
	cin>>cases;
	while (cases--) {
		memset(m, 0, sizeof(m));
		memset(color, 0, sizeof(color));
		memset(ans, 0, sizeof(ans));
		maxn = black = 0;
		int i, j;
		scanf("%d%d", &a, &b);
		for (i = 0; i < b; ++i) {
			int x, y;
			scanf("%d%d", &x, &y);
			m[x][y] = m[y][x] = 1;
		}
		dfs(1);
		printf("%d\n" , maxn);
		for (i = 1; i < a; i++)
			if (ans[i]) printf("%d " , i);
		if (ans[i]) printf("%d\n", i);
		else printf("\n");
	}
	return 0;
}


你可能感兴趣的:(Graph)