您的位置:首页 > 产品设计 > UI/UE

hdu1530 Maximum Clique

2016-02-14 16:18 344 查看


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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: