hdu1530 Maximum Clique

Maximum Clique


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
   
   
   
   
5 0 1 1 0 1 1 0 1 1 1 1 1 0 1 1 0 1 1 0 1 1 1 1 1 0 0
 

Sample Output
   
   
   
   
4
 

Author
CHENG, Long





最大团模板题

#include<bits/stdc++.h>
using namespace std;
const int maxn=51;
int num[maxn];
int G[maxn][maxn],ret;

bool dfs(int *adj,int tot,int cnt){
    if(tot==0){
        if(cnt>ret){
            ret=cnt;
            return 1;
        }
        return 0;
    }
    for(int i=0;i<tot;i++){
        if(cnt+tot-i<=ret)
            continue;
        if(cnt+num[adj[i]]<=ret)
            continue;
        int t[maxn],k=0;
        for(int j=i+1;j<tot;j++){
            if(G[adj[i]][adj[j]])
                t[k ++] = adj[j];
        }
        if(dfs(t,k,cnt+1))
            return 1;
    }
    return 0;
}

void Maxclique(int n){
    int adj[maxn],tot;
    for(int i=n;i>=1;i--){
        tot=0;
        for(int j=i+1;j<=n;j++)
            if(G[i][j]==1)
                adj[tot++]=j;
        dfs(adj,tot,1);
        num[i]=ret;
    }
}

int main(){
    int n;
    while(scanf("%d",&n)!=EOF){
        if(n==0)
            break;
        memset(G,0,sizeof(G));
        ret=0;
        for(int i=1;i<=n;i++)
            for(int j=1;j<=n;j++)
                scanf("%d",&G[i][j]);
        Maxclique(n);
        printf("%d\n",ret);
    }
    return 0;
}



你可能感兴趣的:(hdu1530 Maximum Clique)