Graph Coloring
Time Limit: 1000MS |
|
Memory Limit: 10000K |
Total Submissions: 4370 |
|
Accepted: 1980 |
|
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
Using nature, maximal group equals maximum independent set of complement graph
#include <iostream> #include <cstdio> #include <cstring> #include <string> #include <cmath > #include <cstdlib> #include <algorithm> #include <queue> #include <vector> #include < stack>using namespace Std;int n,mp[55][55],vis[55],cnt,bestn;void dfs (int x) {if (x>n) {if (cnt>bestn ) bestn=cnt; return; } bool Ok=true; for (int i=1;i<x;i++) {if (vis[i]&&!mp[i][x]) {ok=false; Break }} if (ok) {vis[x]=1; cnt++; DFS (X+1); cnt--; } if (CNT+N-X>BESTN) Vis[x]=0,dfs (x+1);} int main () {while (scanf ("%d", &n)!=eof) {if (n==0) break; cnt=bestn=0; memset (vis,0,sizeof (VIS)); Memset (Mp,0,sizeof (MP)); for (int i=1;i<=n;i++) {for (int j=1;j<=n;j++) scanf ("%d", &mp[i][j]); } dfs (1); printf ("%d\n", BESTN); } return 0;}
(Maximum Regiment) POJ 1419