POJ 1419 最大团问题

来源:互联网 发布:外国人吃西洋参吗 知乎 编辑:程序博客网 时间:2024/05/17 06:24
Graph Coloring
Time Limit: 1000MS Memory Limit: 10000KTotal Submissions: 3830 Accepted: 1711 Special Judge

Description

You are to write a program that tries to find an optimal coloring for a given graph. Colors are applied to the nodes of the graph and the only available colors are black and white. The coloring of the graph is called optimal if a maximum of nodes is black. The coloring is restricted by the rule that no two 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 <= 100, 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 are separated by a space.

Output

The output should consists of 2m lines, two lines for each graph found in the input file. The first line of should contain the maximum number of nodes that can be 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

最大团裸题,由定理可知,最大团是最大独立集的补图,因此可以计算补图最大独立集,从而得到最大团。

代码:

/* ***********************************************Author :rabbitCreated Time :2014/3/25 20:44:48File Name :12.cpp************************************************ */#pragma comment(linker, "/STACK:102400000,102400000")#include <stdio.h>#include <iostream>#include <algorithm>#include <sstream>#include <stdlib.h>#include <string.h>#include <limits.h>#include <string>#include <time.h>#include <math.h>#include <queue>#include <stack>#include <set>#include <map>using namespace std;#define INF 0x3f3f3f3f#define eps 1e-8#define pi acos(-1.0)typedef long long ll;const int maxn=110;int n,ans,cnt,x[maxn],opt[maxn],mp[maxn][maxn];void dfs(int i){if(i>n){ans=cnt;for(int i=1;i<=n;i++)opt[i]=x[i];return;}bool flag=true;for(int j=1;j<i&&flag;j++)if(x[j]==1&&!mp[j][i])flag=false;if(flag){x[i]=1;cnt++;dfs(i+1);cnt--;}if(cnt+n-i>ans){x[i]=0;dfs(i+1);}}int main(){     //freopen("data.in","r",stdin);     //freopen("data.out","w",stdout);     int T,m,x,y; scanf("%d",&T); while(T--){ scanf("%d%d",&n,&m); memset(mp,1,sizeof(mp)); while(m--){ scanf("%d%d",&x,&y); mp[x][y]=mp[y][x]=0; } ans=cnt=0; dfs(1); cout<<ans<<endl; for(int i=1;i<=n;i++) if(opt[i])printf("%d ",i); puts(""); }     return 0;}


0 0
原创粉丝点击