zoukankan      html  css  js  c++  java
  • POJ All Friends (Bron-Kerbosch算法 极大团数量)

    题面

    Problem Description
    Sociologists are interested in the phenomenon of "friendship". To study this property, they analyze various groups of people. For each two persons in such a group they determine whether they are friends (it is assumed that this relation is symmetric). The sociologists are mostly interested in the sets of friends. The set S of people is the set of friends if every two persons in S are friends. However, studying the sets of friends turns out to be quite complicated, since there are too many such sets. Therefore, they concentrate just on the maximal sets of friends. A set of friends S is maximal if every person that does not belong to S is not a friend with someone in S.

    Your task is to determine the number of maximal sets of friends in each group. In case this number exceeds 1 000, you just need to report this -- such a group is too complicated to study.

    Input
    The input consists of several instances, separated by single empty lines.

    The first line of each instance consists of two integers 1 ≤ n ≤ 128 and m -- number of persons in the group and number of friendship relations. Each of m following lines consists of two integers ai and bi (1 ≤ ai, bi ≤ n). This means that persons ai and bi (ai ≠ bi) are friends. Each such relationship is described at most once.

    Output
    The output for each instance consists of a single line containing the number of maximal sets of friends in the described group, or string "Too many maximal sets of friends." in case this number is greater than 1 000.

    Sample Input
    5 4
    1 2
    3 4
    2 3
    4 5

    Sample Output
    4

    思路

    极大团裸题。用BK算法解决。

    代码实现

    #include<cstdio>
    #include<algorithm>
    #include<vector>
    #include<queue>
    #include<map>
    #include<iostream>
    #include<cstring>
    #include<cmath>
    using namespace std;
    #define rep(i,f_start,f_end) for (int i=f_start;i<=f_end;++i)
    #define per(i,n,a) for (int i=n;i>=a;i--)
    #define MT(x,i) memset(x,i,sizeof(x) )
    #define rev(i,start,end) for (int i=0;i<end;i++)
    #define inf 0x3f3f3f3f
    #define mp(x,y) make_pair(x,y)
    #define lowbit(x) (x&-x)
    #define MOD 1000000007
    #define exp 1e-8
    #define N 1000005 
    #define fi first 
    #define se second
    #define pb push_back
    typedef long long ll;
    typedef pair<int ,int> PII;
    ll gcd (ll a,ll b) {return b?gcd (b,a%b):a; }
    inline int read() {
        char ch=getchar(); int x=0, f=1;
        while(ch<'0'||ch>'9') {
            if(ch=='-') f = -1;
            ch=getchar();
        } 
        while('0'<=ch&&ch<='9') {
            x=x*10+ch-'0';
            ch=getchar();
        }   return x*f;
    }
    const int maxn=200;
    int g[maxn][maxn];
    int all[maxn][maxn],some[maxn][maxn],none[maxn][maxn];
    int ans,n,m,s;
    
    void dfs (int n,int an,int sn,int nn) {
        if (!sn&&!nn) ans++;
        if (ans>1000) return ;
        int key=some[n][1];
        rep (j,1,sn) {
            int v=some[n][j],tsn=0,tnn=0;
            if (g[key][v]) continue;
            rep (i,1,an) all[n+1][i]=all[n][i]; all[n+1][an+1]=v;
            rep (i,1,sn) if (g[v][some[n][i]]) some[n+1][++tsn]=some[n][i];
            rep (i,1,nn) if (g[v][none[n][i]]) none[n+1][++tnn]=none[n][i];
            dfs (n+1,an+1,tsn,tnn);
            some[n][j]=0,none[n][++nn]=v;
        } 
    }
    
    
    
    int main () {
        while (cin>>n>>m) {
            MT (g,0);
            ans=0;
            rep (i,1,m) {
              int x,y;
              x=read (),y=read ();
              g[x][y]=g[y][x]=1;
            }
            rep (i,1,n) some[1][i]=i;
            dfs (1,0,n,0);
            if (ans>1000) cout<<"Too many maximal sets of friends."<<endl;
            else cout<<ans<<endl;
        }
     
        return 0;
    }
    
  • 相关阅读:
    微信小程序上拉分页
    关于检测数据类型,三种方法(typeof,instanceof,Object.prototype.toString.call())优缺点
    如何在Devc++中配置OpenCv
    数据库系统和应用
    这是一篇测试文档
    Pandas 表格合并
    es6一些好用的方法总结
    前端面试题
    超有趣! JS是怎么计算1+2!!!
    彻底理解闭包
  • 原文地址:https://www.cnblogs.com/hhlya/p/13418216.html
Copyright © 2011-2022 走看看