UVA 193 Graph Coloring

Source: Internet
Author: User

Original question:
You is to write a program this tries to find a optimal coloring for a given graph. Colors is applied to the nodes of the graph and the only available Colors is black and white.
The coloring of the graph is called optimal if a maximum of nodes are black. The coloring is restricted by the rule, that no, and connected nodes may be black.
Input
The graph is given as a set of nodes denoted by numbers 1 ... n, n≤100, 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 is separated by a space.
Output
The output should consists of 2m lines, and the lines for each graph found in the input file. The first line of should contain the maximum number of nodes that can is 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


English:
Give you a picture that lets you dye the picture in black and white. Black dots cannot be adjacent, asking how many black dots you can have.
。。。 Note that this graph is not necessarily connected Tumen may have many connected components


#include <bits/stdc++.h>
using namespace std;
vector<int> G[111];
int n,k,solve[111],tmp[111],color[111],ans;
void ini()
{
    memset(tmp,0,sizeof(tmp));
    for(int i=1;i<=n;i++)
        G[i].clear();
        ans=0;
}
void dfs(int x,int tmp[])
{
    int color[101];
    if(x==n)
    {
        int cnt=0;
        for(int i=1;i<=n;i++)
            if(tmp[i]==1)
                cnt++;
        if(cnt>ans)
        {
            memcpy(solve,tmp,sizeof(solve));
            ans=cnt;
        }
    }
    else
    {
        for(int i=1;i<=n;i++)
        {
            if(tmp[i]==0)
            {
                memcpy(color,tmp,sizeof(color));
                color[i]=1;
                int cnt=1;
                for(int j=0;j<G[i].size();j++)
                {
                    if(color[G[i][j]]==0)
                    {
                        color[G[i][j]]=-1;
                        cnt++;
                    }
                }
                dfs(x+cnt,color);
            }
        }
    }
}
int main()
{
    ios::sync_with_stdio(false);
    int t;
    cin>>t;
    while(t--)
    {
        cin>>n>>k;
        ini();
        for(int i=1;i<=k;i++)
        {
            int a,b;
            cin>>a>>b;
            G[a].push_back(b);
            G[b].push_back(a);
        }
        dfs(0,tmp);
        cout<<ans<<endl;
        /*
        for(int i=1;i<=n;i++)
        {
            if(solve[i]==1&&ans>0)
            {
                if(ans==1)
                    cout<<i<<endl;
                else
                    cout<<i<<" ";
                ans--;
            }
        }*/
        k=0;
        for(int i=1;i<=n;i++)
            if(solve[i]==1)
            {
                if(k++)
                    cout<<" ";
                cout<<i;
            }
            cout<<endl;

    }
    return 0;
}


Answer:
The idea is simple, the nodes that are dyed black and the nodes adjacent to it are definitely white dots. Violence can be solved, but how to write it is WA, later read others code know the diagram may be disconnected.



There is an optimized idea of how to calculate the maximum black point, which may not be as much of an optimized method. But in turn can go to calculate the minimum point of white, record the last white point of the result, the next time the number of white dots in the enumeration graph, if the enumeration to halfway is already more than the last result, then you can prune.


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.