POJ_2989

    求极大团的数量,我的程序是参照维基上的伪代码写的http://en.wikipedia.org/wiki/Bron%E2%80%93Kerbosch_algorithm

#include<stdio.h>
#include<string.h>
#define MAXN 130
int N, M, a[MAXN], g[MAXN][MAXN];
void input()
{
    memset(g, 0, sizeof(g));
    for(int i = 0; i < M; i ++)
    {
        int x, y;
        scanf("%d%d", &x, &y);
        g[x][y] = g[y][x] = 1;
    }
}
int S, all[MAXN][MAXN], some[MAXN][MAXN], none[MAXN][MAXN];
void dfs(int d, int an, int sn, int nn)
{
    if(S > 1000) return ; // 极大团数量超过1000就不再统计
    if(sn == 0 && nn == 0) ++ S;
    int u = sn > 0 ? some[d][0] : none[d][0];
    for(int i = 0; i < sn; i ++)
    {
        int v = some[d][i];
        if(g[u][v]) continue;
        int tsn = 0, tnn = 0;
        for(int j = 0; j < an; j ++) all[d + 1][j] = all[d][j];
        all[d + 1][an] = v;
        for(int j = 0; j < sn; j ++)
            if(g[v][some[d][j]]) some[d + 1][tsn ++] = some[d][j];
        for(int j = 0; j < nn; j ++)
            if(g[v][none[d][j]]) none[d + 1][tnn ++] = none[d][j];
        dfs(d + 1, an + 1, tsn, tnn);
        some[d][i] = 0, none[d][nn ++] = v;
    }
}
void process()
{
    S = 0;
    for(int i = 0; i < N; i ++) some[0][i] = i + 1;
    dfs(0, 0, N, 0);
    if(S > 1000) printf("Too many maximal sets of friends.\n");
    else printf("%d\n", S);
}
int main()
{
    while(scanf("%d%d", &N, &M) == 2)
    {
        input();
        process();
    }
    return 0;
}

 

 

posted on 2012-11-10 23:04  Staginner  阅读(930)  评论(0编辑  收藏  举报