Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1320744
  • 博文数量: 179
  • 博客积分: 4141
  • 博客等级: 中将
  • 技术积分: 2083
  • 用 户 组: 普通用户
  • 注册时间: 2009-03-21 20:04
文章存档

2024年(1)

2019年(13)

2016年(1)

2014年(16)

2011年(8)

2010年(25)

2009年(115)

分类:

2009-06-19 15:56:46

Coloring of Graph

Time Limit:1sMemory limit:32M   Special Judge
Accepted Submit:58Total Submit:79

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.

figure22
Figure: An optimal graph with three black nodes

Input and Output

The graph is given as a set of nodes denoted by numbers 1..n, n<=50, and a set of undirected edges denoted by pairs of node numbers (n1,n2), n1!=n2. 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

#include<stdio.h>
#include<string.h>

struct List
{
    int val;
    int next;
};
struct List list[10000];
int index;

struct Node
{
    int visit;
    int black; /* 1:white 2:black */
    int next;
};
struct Node node[200];

int point[200];
int result, black_node[200];
int node_of_num;
int edge;

int search(int index, int min)
{
    int i, next;

    if (index > result)
    {
        result = index;
        memcpy(black_node, point, sizeof(int) * index);
    }

    for (i = min; i <= node_of_num; ++i)
    {
        if (!node[i].visit)
        {
            node[i].visit = 1;

            point[index] = i;
            next = node[i].next;
            while (next != -1)
            {
                ++node[list[next].val].visit;
                next = list[next].next;
            }

            search(index + 1, i + 1);

            next = node[i].next;
            while (next != -1)
            {
                --node[list[next].val].visit;
                next = list[next].next;
            }

            node[i].visit = 0;
        }
    }

    return 0;
}

int main()
{
    int i, test;
    int j;
    int a, b;

    scanf("%d", &test);
    for (i = 1; i <= test; ++i)
    {
        result = 0;
        scanf("%d %d", &node_of_num, &edge);
        for (j = 1; j <= node_of_num; ++j)
        {
            node[j].next = -1;
            node[j].visit = 0;
        }

        index = 0;
        for (j = 1; j <= edge; ++j)
        {
            scanf("%d %d", &a, &b);
            list[index].val = b;
            list[index].next = node[a].next;
            node[a].next = index;
            ++index;

            list[index].val = a;
            list[index].next = node[b].next;
            node[b].next = index;
            ++index;
        }

        search(0, 1);

        printf("%d\n", result);
        for (j = 0; j < result; ++j)
        {
            if (j == result - 1)
                printf("%d\n", black_node[j]);
            else
                printf("%d ", black_node[j]);
        }
    }

    return 0;
}

阅读(2813) | 评论(0) | 转发(0) |
0

上一篇:Nancy's Birthday

下一篇:Edit Ladders

给主人留下些什么吧!~~