zoukankan      html  css  js  c++  java
  • uva193

    Graph Coloring

    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 tex2html_wrap_inline33 , tex2html_wrap_inline35 , and a set of undirected edges denoted by pairs of node numbers tex2html_wrap_inline37 , tex2html_wrap_inline39 . 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<cstdio>
    #include<cstring>
    #include<iostream>
    #include<vector>
    #include<algorithm>
    using namespace std;
    const int maxn=100+5;
    int m,n,k;
    vector<int> G[maxn];
    int node[maxn], black_node[maxn];
    int ans;
    enum {white, black};
    
    void dfs(int d, int cnt) {
            if(d==n) {
                    if(cnt>ans) {
                            int j=0;
                            for(int i=0;i<n;i++) if(node[i]) black_node[j++]=i+1;
                            ans=cnt;
                    }
                    return;
            }
    
            if(n-d+cnt<=ans)
                    return;
            //尝试黑的
            int ok=true;
            node[d]=black;
            for(int i=0;i<G[d].size();i++) {
                    int v=G[d][i];
                    //两个相邻黑点
                    if(node[v]==black) {
                            ok=false;
                            break;
                    }
            }
            if(ok) dfs(d+1, cnt+1);
    
            //尝试白的
            node[d]=white;
            dfs(d+1, cnt);
    }
    
    int main()
    {
    #ifndef ONLINE_JUDGE
            freopen("./uva193.in", "r", stdin);
    #endif
            int x,y;
            cin>>m;
            while(m--) {
                    cin>>n>>k;
                    memset(G, 0, sizeof(G));
                    memset(node, 0, sizeof(node));
                    ans=0;
                    for(int i=0;i<k;i++) {
                            cin>>x>>y;
                            x--;y--;
                            G[x].push_back(y);
                            G[y].push_back(x);
                    }
                    dfs(0, 0);
                    printf("%d
    ", ans);
                    for(int i=0;i<ans-1;i++)
                            printf("%d ", black_node[i]);
                    printf("%d
    ", black_node[ans-1]);
    
            }
    
        return 0;
    }
  • 相关阅读:
    HDU 2094 产生冠军
    poj 3269 Building A New Barn
    [js
    有感于NC的强大
    was配置oracle RAC集群的数据源
    vb.net 操作xml
    一个用C++写的Json解析与处理库
    配置apache和nginx的tomcat负载均衡
    Remove Duplicates from Sorted Array [Python]
    LoaderManager使用具体解释(一)---没有Loader之前的世界
  • 原文地址:https://www.cnblogs.com/cute/p/3891865.html
Copyright © 2011-2022 走看看