The questions are as follows:
You are to write a program that tries to find an optimal coloring for agiven graph. colors are applied to the nodes of the graph and the only availablecolors are black and white. the coloring of the graph is called optimalif a maximum of nodes is black. the coloring is restricted by the rule thatno two connected nodes may be black.
Figure:An Optimal graph with three black nodes
Input and Output
The graph is given as a set of nodes denoted by numbers, and a set of undirected edges denoted by pairs of node numbers,. The input file containsMGraphs. the numberMIs given on the first line. The first line ofeach graph containsNAndK, The number of nodes and the numberof edges, respectively. The followingKLines contain the edges givenby a pair of node numbers, which are separated by a space.
The output shoshould consists of 2MLines, two lines for each graphfound in the input file. the first line of shoshould contain the maximum numberof nodes that can be colored black in the graph. the second line shouldcontain one possible optimal coloring. it is given by the list of blacknodes, separated by a blank.
Sample Input
16 81 21 32 42 53 43 64 65 6
Sample output
31 4 5
Calculate the maximum number of black nodes in the figure. The black nodes cannot be adjacent and seem simple (actually very simple ...), However, the NC times out and Wa times again. I didn't understand the meaning of cur in DFS clearly. I originally meant that cur would be the number of nodes I have judged, but this would not determine the recursive boundary, if the boundary is cur = N, it is a waste of time to judge N points for each recursion. Some points are obviously impossible. Later it was changed to cur, which is the subscript of the current vertex to be judged. The idea is much clearer, but there is still a problem: if the current node cannot be colored, do you want to recursion? If you think about it, you will find that recursion is required. Otherwise, the recursive boundary cannot be reached. But if the current node can be stained, will it only dye the current node and then recursion? This can be an example, but it will be wa, because it misses many situations, which is equivalent to judging from the first point, and then the first point will certainly be stained, then let's look at the other points in sequence. There is a total of one solution, but there are a variety of dyeing solutions. So even if the current node can be stained, We Should recursively dye the node.
The AC code is as follows:
UV graph coloring