UVa 193:Graph Coloring

题目链接:

http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=108&page=show_problem&problem=129

原题:

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

题目大意:

给一张图染色, 只能染白色或黑色, 不能让相连的两个点都是染成黑色(白色的可以)。 求这张图能染黑色的最多点数

分析与总结:

其实就是对于每一个点,染黑,或者不染黑的问题。直接暴力搜索即可。

参考:王晓东的《计算机算法设计与分析第三版》P163页 ”图的m着色问题“。

代码:

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#define MAXN 102
using namespace std;
int n,k, G[MAXN][MAXN], maxVal;
int color[MAXN], ans[MAXN];  

// 判断与这个点相连的是否有黑色的
inline bool isOk(int u){
    for(int i=1; i<=n; ++i)
        if(G[u][i] && color[i])return false;
    return true;
}  

void search(int u, int num){
    if(u > n){
        if(num > maxVal){
            maxVal = num;
            memcpy(ans, color, sizeof(ans)) ;
        }
        return;
    }
    if(num + n - u + 1 <= maxVal) return;// 减枝
    if(isOk(u)){
        color[u] = 1;
        search(u+1, num+1);
        color[u] = 0;
    }
    search(u+1, num);
}  

int main(){
#ifdef LOCAL
    freopen("input.txt","r",stdin);
#endif
    int T, u, v;
    scanf("%d",&T);
    while(T--){
        scanf("%d %d", &n, &k);
        memset(G, 0, sizeof(G));
        for(int i=0; i<k; ++i){
            scanf("%d %d", &u, &v);
            G[u][v] = G[v][u] = 1;
        }  

        maxVal = -2147483646;  

        memset(color, 0, sizeof(color));
        search(1, 0);  

        printf("%d\n", maxVal);  

        bool flag=true;
        for(int i=1; i<=n; ++i)if(ans[i]){
            if(flag){ printf("%d",i);  flag=false;}
            else printf(" %d",i);
        }
        printf("\n");
    }
    return 0;
}

以上是小编为您精心准备的的内容,在的博客、问答、公众号、人物、课程等栏目也有的相关内容,欢迎继续使用右上角搜索按钮进行搜索include
, 图的m着色问题
, nodes
, graph
, black
, 黑色
The
coloring book、coloring、coloring book aac、food coloring、coloring pages,以便于您获取更多的相关知识。

时间: 2025-01-25 12:11:33

UVa 193:Graph Coloring的相关文章

UVa 10720:Graph Construction

链接: http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=113&page=show_problem&problem=1661 原题: Graph is a collection of edges E and vertices V. Graph has a wide variety of applications in computer. There are diffe

uva 193 - Graph Coloring

点击打开链接 题目意思:  给定n个节点,节点的编号为1-n,在给定m个节点链接的信息,现在要求对节点图色,只有两种颜色可以黑色和白色并且相邻的节点不能同时为黑色但是可以为白色,要求黑色节点最多的个数,以及一组节点的编号 解题思路:  对于节点而言,每一个节点就是两种情况,白色或黑色,那么我们知道这一个解空间树的每一层就是一个节点,那么我们只要去搜索这个解空间就可以了,这一题的数据虽然n到达100,但是真正的数据没那么大,不会超时. 注意事项:   这一题uva最后没有换行 , poj有换行,注

算法题:UVa 11069 A Graph Problem (斐波那契)

A Graph Problem Time limit: 2s Given an undirected graph of the following form with n nodes, 1 ≤ n ≤ 76: Your task is to calculate the number of subsets of nodes of the graph with the following properties: no nodes in the subset should be connected i

UVa 10004:Bicoloring

题目链接: http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=105 题目类型:搜索 题目: In 1976 the ``Four Color Map Theorem" was proven with the assistance of a computer. This theorem states that every map can be colored using on

UVa 10305:Ordering Tasks , 经典的拓扑排序

题目链接: http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=105&page=show_problem&problem=1246 题目类型: 拓扑排序 题目: John has n tasks to do. Unfortunately, the tasks are not independent and the execution of one task is onl

UVa 140:Bandwidth

题目链接: http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=108&page=show_problem&problem=76 类型: 回溯 原题: Given a graph (V,E) where V is a set of nodes and E is a set of arcs in VxV, and an ordering on the elements in

UVa 1422:Processor 任务处理问题

题目大意:有n个任务送到处理器处理,每个任务信息包括r,d,w,r代表开始时间,w代表必须要结束的时间,w指需要多少时间处理. 其中处理器的处理速度可以变速,问处理器最小需要多大速度才能完成工作? 输入: 3 5 1 4 2 3 6 3 4 5 2 4 7 2 5 8 1 6 1 7 25 4 8 10 7 10 5 8 11 5 10 13 10 11 13 5 8 15 18 10 20 24 16 8 15 33 11 14 14 1 6 16 16 19 12 3 5 12 22 25

UVa 10905:Children&#039;s Game

题目链接: http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=113&page=show_problem&problem=1846 类型: 排序 There are lots of number games for children. These games are pretty easy to play but not so easy to make. We will

UVa 10763:Foreign Exchange

题目链接: http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=113&page=show_problem&problem=1704 原题: Your non-profit organization (iCORE - international Confederation of Revolver Enthusiasts) coordinates a very succes