Maximum Clique+最大团

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/ujn20161222/article/details/80440901
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


#define happy

#include<bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef long double ld;

typedef pair<int,int> pi;
typedef pair<ll,ll> pl;
typedef pair<ld,ld> pd;

typedef vector<int> vi;
typedef vector<ld> vd;
typedef vector<ll> vl;
typedef vector<pi> vpi;
typedef vector<pl> vpl;


#define rep(i,a,b) for(int i=a;i<=b;i++)
#define all(a) (a).begin(),(a).end()
#define sz(x) (int)(x).size()
#define mp make_pair
#define pb push_back
#define f first
#define s second



const ll INF=1e18;
const int MOD=1e9+7;
const int N=50+5;

ll rd(){
    ll x=0,f=1;char ch=getchar();
    while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
    while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
    return x*f;
}

bool g[N][N];
int len[N],n,ans,llist[N][N],mc[N];
bool found;
void dfs(int size){
    int i,j,k;
    if(len[size]==0){
        if(size>ans){
            ans=size;
            found=true;
        }return;
    }
    for(k=0;k<len[size]&&!found;++k){
        if(size+len[size]-k<=ans)
            break;
        i=llist[size][k];
        if(size+mc[i]<=ans)break;
        for(j=k+1,len[size+1]=0;j<len[size];++j)
            if(g[i][llist[size][j]])
            llist[size+1][len[size+1]++]=llist[size][j];
        dfs(size+1);
    }
}

void max_c(){
    int i,j;
    mc[n]=ans=1;
    for(i=n-1;i;i--){
        found=false;
        len[1]=0;
        for(j=i+1;j<=n;j++)
            if(g[i][j])
            llist[1][len[1]++]=j;
        dfs(1);
        mc[i]=ans;
    }
}


int main(){
#ifdef happy
    freopen("in.txt","r",stdin);
#endif
   while(n=rd()){
    rep(i,1,n)rep(j,1,n)
    g[i][j]=rd();
    max_c();
    printf("%d\n",ans);
   }
}

阅读更多
相关热词
换一批

没有更多推荐了,返回首页