UVA 193 Graph Coloring

来源:互联网 发布:手机淘宝怎样改评价 编辑:程序博客网 时间:2024/06/04 00:33

UVA-193

题意:给出n个点m条边,每条边不允许两端都染色。求最多可以染色多少个点,这些点是哪些。
解题思路:从第一个点开始枚举染或不染,染的条件是它相连的点都未染色。暴力DFS。

/*************************************************************************    > File Name: UVA-193.cpp    > Author: Narsh    >     > Created Time: 2016年07月28日 星期四 13时28分29秒 ************************************************************************/#include <iostream>#include <algorithm>#include <cstdio>#include <cstring>using namespace std;bool map[200][200],color[200];int ans,temp[120],n,m,key[120],t;void dfs(int x, int num) {    if (x > n) {        if (num > ans) {            ans = num;            for (int i = 1; i <= num; i++)                key[i] = temp[i];        }        return ;    }    bool flag = true;    for (int i = 1; i <= n; i++)        if (map[x][i] && !color[i]) flag =false;    if (flag) {        temp[num+1] = x;        color[x] = false;        dfs(x+1,num+1);    }    color[x] =true;    dfs(x+1,num);}int main() {    freopen("xx.in","r",stdin);    scanf("%d",&t);    while (t--) {        scanf("%d%d",&n,&m);        memset(color,true,sizeof(color));        memset(map,false,sizeof(map));        for (int i = 1; i <= m; i++) {            int a,b;            scanf("%d%d",&a,&b);            map[a][b] = map[b][a] = true;        }        ans = 0;        dfs(1,0);        printf("%d\n",ans);        for (int i = 1; i < ans; i++)             printf("%d ",key[i]);        printf("%d\n",key[ans]);    }}
0 0
原创粉丝点击