Title Address: http://poj.org/problem?id=1419
Graph Coloring
Time Limit: 1000MS |
|
Memory Limit: 10000K |
Total Submissions: 4468 |
|
Accepted: 2042 |
|
Special Judge |
Description
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.
Figure 1:an Optimal graph with three black nodes
Input
The graph is given as a set of nodes denoted by numbers 1...N, n <=, 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
16 81 21 32 42 53 43 64 65 6
Sample Output
31 4 5
Topic Interpretation: For a given figure to find the best coloring. Coloring a node in a diagram can only be done in black or white, and the rule of coloring is that two adjacent nodes cannot be black.
(Of course there may be white) T-group data, each group has n nodes, M-bars. That is, the maximum independent set of the current graph is required.
The algorithm realizes that the maximal independent set of the current graph is calculated by finding the largest group on the graph. The complement diagram is constructed directly at the input edge, and the maximum regiment of the complement graph is calculated by the specified mode.
Code:
1#include <stdio.h>2#include <string.h>3#include <stdlib.h>4#include <ctype.h>5#include <math.h>6#include <iostream>7#include <string>8#include <queue>9#include <algorithm>Ten #defineN 105 One A using namespacestd; - - intN, M; the BOOLG[n][n]; - int Get[n][n]; - intNode[n], ans[n], dp[n]; - + intMax;//the number of nodes in the current regiment is Max - + A voidDfsintNowintsum) at{//Starting from the current state (the number of nodes of the current regiment is now and the number of sides to which the last node is connected), recursively calculates the largest group - if(sum==0){//If the constituent group is a complete sub-graph - if(Now >Max) { -max=Now ; - for(intI=1; i<=max; i++) -Ans[i]=node[i];//nodes in the storage group in } - return ; to } + for(intI=1; i<=sum; i++){ - intv=Get[Now] [i], t=0; the if(Now+dp[v]<=max)return ; * for(intj=i+1; j<=sum; J + +){ $ if(g[v][Get[Now][j]])Panax Notoginseng Get[now+1][++t]=Get[now][j]; - } thenode[now+1]=v; +DFS (now+1, T); A } the } + - voidInit () $ { $scanf"%d%d", &n, &m); -Memset (G,true,sizeof(g));//connect the initial edges of the complement diagram - for(intI=0; i<m; i++){ the intu, v; -scanf"%d%d", &u, &v);Wuyig[u][v]=false; g[v][u]=false;//should be broken in the complement diagram the } - } Wu - voidSolve ()//The largest group of calculation and output complements is the largest independent set of the original image About { $max=0;// - for(intI=n; i>=1; i--) {//each node I as the first node of the current regiment in descending order - intsum=0; - for(intj=i+1; j<=n; J + +) {//computes the endpoint adjacent to I in the I+1...N and deposits it in get[1][] A if(G[i][j])Get[1][++sum]=J; + } thenode[1]=i; -Dfs1, sum); $dp[i]=Max; the } theprintf"%d\n", Max); the for(intI=1; i<=max-1; i++){ theprintf"%d", Ans[i]); - } inprintf"%d\n", Ans[max]); the } the About intMainvoid) the { the intTG; thescanf"%d", &TG); + while(tg--){ - init (); the solve ();Bayi } the return 0; the}
View Code
POJ 1419Graph Coloring "dfs+ + COMPUTE MAX Group + COMPUTE maximum Standalone set template" "