Graph Coloring (maximum independent set template problem)

Source: Internet
Author: User

Graph ColoringPOJ-1419You 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.


 
Figure 1:an Optimal graph with three black nodesInputThe graph is given as a set of nodes denoted by numbers 1...N, n <=, and a set of undirected edges denoted by P Airs 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.Outputthe 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
16 81 21 32 42 53 43 64 65 6
Sample Output
31 4 5

Test instructions: Give you an out of the picture, and then to the points in the color, only black and white, the requirement is that the black point cannot be adjacent (White can be adjacent), ask how many black vertices can be.

The key: is to find the largest independent set of graphs, the focus is the output path, so the set of templates, if not output path, then you can set the formula: maximum independent set = Vertex number-The maximum number of matches, the Hungarian algorithm to find the maximum number of matches.

1#include <iostream>2#include <cstdio>3#include <cstring>4#include <vector>5#include <algorithm>6 using namespacestd;7 intvis[ the];8 intn,k,m;9 intans,res[ the];Tenvector<int>v[ the]; One intMaxx; A voidDfsintPosintCNT) - { -     if(pos==n+1) the     { -         if(cnt>Maxx) -         { -             intt=0;//must pay attention to AH Ah!!  +              for(intI=1; i<=n;i++) -             { +                 if(Vis[i]) Ares[t++]=i; at             } -maxx=CNT; -         } -         return; -     } -     intflag=0; in      for(intI=0; I<v[pos].size (); i++) -     { to         if(Vis[v[pos][i]]) +         { -flag=1; the              Break; *         } $     }Panax Notoginseng     if(flag==0) -     { thevis[pos]=1; +DFS (pos+1, cnt+1); Avis[pos]=0; the     } +DFS (pos+1, CNT); -     return; $ } $ voidInit () - { -      for(intI=0;i< the; i++) the v[i].clear (); -memset (Res,0,sizeof(res));Wuyimemset (Vis,0,sizeof(Vis)); the } - intMain () Wu { -     intx, y; Aboutscanf"%d",&m); $      while(m--) -     { - init (); -scanf"%d%d",&n,&k); A          for(intI=0; i<k;i++) +         { thescanf"%d%d",&x,&y); - v[x].push_back (y); $ v[y].push_back (x); the         } themaxx=-1; theDfs1,0); theprintf"%d\n", Maxx); -          for(intI=0; i<maxx-1; i++) in         { theprintf"%d", Res[i]); the         } Aboutprintf"%d\n", res[maxx-1]); the     } the  the     return 0; +}



Graph Coloring (maximum independent set template problem)

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.