1530 Maximum Clique 最大团(模板)

来源:互联网 发布:淘宝店一颗心 编辑:程序博客网 时间:2024/06/05 19:27

点击打开链接

 

Maximum Clique

Time Limit: 20000/10000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 2107    Accepted Submission(s): 1117


Problem Description
Given a graph G(V, E), a clique is a sub-graph g(v, e), so that for all vertex pairs v1, v2 in v, there exists an edge (v1, v2) in e. Maximum clique is the clique that has maximum number of vertex.
 


 

Input
Input contains multiple tests. For each test:

The first line has one integer n, the number of vertex. (1 < n <= 50)

The following n lines has n 0 or 1 each, indicating whether an edge exists between i (line number) and j (column number).

A test with n = 0 signals the end of input. This test should not be processed.
 


 

Output
One number for each test, the number of vertex in maximum clique.
 


 

Sample Input
50 1 1 0 11 0 1 1 11 1 0 1 10 1 1 0 11 1 1 1 00
 


 

Sample Output
4
 


 

Author
CHENG, Long
 


 

Source
ZOJ Monthly, February 2003
 


 

Recommend
mcqsmall
 

最大团的模板题。详细解释:点击打开链接

#include<stdio.h>#define M 51int g[M][M],cs[M],ans[M],now[M];int re;bool flag;//最大团模板//////////////////////////////void dfs(int n,int depth,int *u){    int i,j,k,v[M],vn;    if(n)    {        if(depth+cs[u[0]]<=re)            return;        for(i=0;i<n+depth-re;i++)        {            for(j=i+1,vn=0;j<n;j++)                if(g[u[i]][u[j]])                    v[vn++]=u[j];            now[depth]=u[i];            dfs(vn,depth+1,v);            if(flag)                return;        }    }    else if(depth>re)    {        re=depth;        flag=true;        for(i=0;i<depth;i++)            ans[i]=now[i];    }}int clique(int n){    int vn,v[M],i,j,k;    re=0;    for(cs[i=n-1];i>=0;i--)    {        for(vn=0,j=i+1;j<n;j++)            if(g[i][j])                v[vn++]=j;        flag=false;        now[0]=i;        dfs(vn,1,v);        cs[i]=re;    }    return re;}//////////////////////////////////////int main(){    int n;    while(scanf("%d",&n),n)    {        for(int i=0;i<n;i++)            for(int j=0;j<n;j++)                scanf("%d",&g[i][j]);        printf("%d\n",clique(n));    }    return 0;}


 

原创粉丝点击