The topics are as follows:
You is to write a program this tries to find a optimal coloring for agiven graph. Colors is applied to the nodes of the graph and the only availablecolors is black and white. The coloring of the graph is called optimalif a maximum of nodes are black. The coloring is restricted by the rule thatno and the 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 number m is given on the first line. The first line Ofeach graph containsn and K, the number of nodes and the numberof edges, respectively. The followingK lines contain the edges Givenby a pair of node numbers, which is separated by a space.
The output should consists of 2m lines, and the input file is a lines for each graphfound. The first line of should contain the maximum numberof nodes that can is 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
Figure out the maximum number of black nodes, black nodes can not be adjacent, it looks very simple (in fact, very easy.)
。
。 But self NC, time out and WA a few times, did not understand clearly the meaning of the DFS cur, I would like to Cur is inferred the number of nodes, but this can not determine the recursive boundary, assuming that the boundary is cur==n, then each recursive will have to infer N points, a very waste of time, some points is obviously impossible.
Later change to cur is the subscript of the point that is currently being inferred. The idea is much clearer, but there is still a problem: if the current node can not be dyed, do you want to recursively go down? If you think about it, you will find that it must be handed down. Otherwise, the recursive boundary cannot be reached. But suppose the current node is capable of staining. Does it just dye the current node and then recursively? This can be done by example. But WA, because of this missing a lot of circumstances. This is equivalent to starting from the first point of inference. Then the first point is sure to be dyed. Then look at other points in turn, a total of one solution, but the total number of different dyeing solutions. So even if the current node can be dyed, it is also recursive to the situation of the node is not dyed.
The code for the AC is as follows:
Copyright notice: This article blog original articles, blogs, without consent, may not be reproduced.
UVA Graph Coloring