UVA - 193 Graph Coloring

原创 2016年08月29日 22:27:29

题目大意:涂色,相邻(有连接)的点不能都上色,求最多能涂多色个点,输出个数和编号。

解题思路:dfs 从一号点开始判断,与当前点连接的点是否有涂色,如果都没有,就给当前点涂色(回溯当前点不涂色),直到判断完所有的点,统计一下上色的点的个数,存下较多的情况。

#include<iostream> 
#include<cstdio>
#include<cmath>
#include<string.h>
#include<stdlib.h>
#include<algorithm>
using namespace std;
bool link[200][200];
bool node[200];
int m, n, k;
int ans, tmp;
int as[200];
void dfs(int now) {
    if (now > n) {
        tmp = 0;
        for (int i = 1; i <= n; i++)
            if (node[i]) tmp++;
        if (tmp > ans) {
            ans = tmp;
            int tag = 1;
            for (int i = 1; i <= n; i++)
                if (node[i]) as[tag++] = i;
        }
        return;
    }
    int tag = 1;
    for (int i = 1; i <= n; i++)
        if (link[now][i] && node[i])
            tag = 0;
    if (tag) {
        node[now] = 1;
        dfs(now+1);
    }
    node[now] = 0;
    dfs(now+1);

}
int main() {
    scanf("%d", &m);
    while (m--) {
        memset(link, 0, sizeof(link));
        scanf("%d%d", &n, &k);
        for (int i = 0; i < k; i++) {
            int a, b;
            scanf("%d%d", &a, &b);
            link[a][b] = 1;
            link[b][a] = 1;
        }

        ans = 0;
        dfs(1);

        printf("%d\n", ans);
        for (int i = 1; i < ans; i++)
            printf("%d ", as[i]);
        printf("%d\n", as[ans]);
    }
return 0; 
}
版权声明:本文为博主原创文章,未经博主允许不得转载。

相关文章推荐

uva 193 - Graph Coloring

Graph Coloring  You are to write a program that tries to find an optimal coloring for a g...

UVA - 193 Graph Coloring

Graph Coloring  You are to write a program that tries to find an optimal coloring for a...

[回溯]Graph Coloring UVA193

Graph Coloring  You are to write a program that tries to find an optimal coloring for a given ...

UVA - 193 Graph Coloring(回溯)

Graph Coloring  You are to write a program that tries to find an optimal coloring for a g...

UVA 193 Graph Coloring 图染色 DFS 数据

题意:图上的点染色,给出的边的两个点不能都染成黑色,问最多可以染多少黑色。 很水的一题,用dfs回溯即可。先判断和当前点相连的点是否染成黑色,看这一点是否能染黑色,能染色就分染成黑色和白色两种情况递...
  • hcbbt
  • hcbbt
  • 2013年08月05日 01:13
  • 2760

UVA 193 Graph Coloring

You are to write a program that tries to find an optimal coloring for a given graph. Colors are appl...

uva 193 Graph Coloring(回溯)

uva 193 Graph Coloring You are to write a program that tries to find an optimal coloring ...

UVa 193 & POJ 1419 - Graph Coloring

传送门UVa 193 - Graph Coloring 题意是求出最多的黑色

uva 193 Graph Coloring

题目地址: http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=108&page=show_pr...

uva - 193 Graph Coloring(回溯)

题意:给你一个图形,你的任务是找到一种最佳涂色的方法。图形中的点要被涂顏色(只能涂黑色或白色),而且任何2个相连的点不可以都涂黑色。所谓最佳的涂色方法是指让这个图形黑色的点最多。以下图形的涂色即是一种...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:UVA - 193 Graph Coloring
举报原因:
原因补充:

(最多只允许输入30个字)