E - Counting Cliques-vector矩阵同建图

  • E - Counting Cliques

  •  HDU - 5952 
  • 题意:
  • A clique is a complete graph, in which there is an edge between every pair of the vertices. Given a graph with N vertices and M edges, your task is to count the number of cliques with a specific size S in the graph. 
  • 思路:
  • 构建mmp[][]矩阵以便于查询各个点之间的关系。vector建图减少查询此点所能到达的点的时间 。
  • dfs递归进行搜索只要限制下一个进入集合的点比当前的点大就无需考虑搜索重复。
  • #include <bits/stdc++.h>
    using namespace std;
    #define maxn 1100
    #define ll long long
    ll t,n,m,ans,u,v,a[maxn];
    ll mmp[maxn][maxn],sum;
    vector<int>gra[maxn];
    bool check(int c,int s)
    {
        for(int i=1; i<=s; i++)
            if(mmp[c][a[i]]!=1)
                return false;
        return true;
    }
    void dfs(int c,int s)
    {
        if(s==ans)
        {
            sum++;
            return;
        }
        int len=gra[c].size();
        for(int j=0; j<len; j++)
        {
            if(gra[c][j]<c)continue;
            if(check(gra[c][j],s))
            {
                a[s+1]=gra[c][j];
                dfs(gra[c][j],s+1);
            }
        }
    }
    int main()
    {
        scanf("%lld",&t);
        while(t--)
        {
            sum=0;
            scanf("%lld%lld%lld",&n,&m,&ans);
            for(int i=1; i<=n; i++)
            {
                gra[i].clear();
                for(int j=1; j<=m; j++)
                    mmp[i][j]=0;
            }
            while(m--)
            {
                scanf("%lld%lld",&u,&v);
                mmp[u][v]=mmp[v][u]=1;
                gra[u].push_back(v);
                gra[v].push_back(u);
            }
            for(int i=1; i<=n; i++)
            {
                a[1]=i;
                dfs(i,1);
            }
            printf("%lld\n",sum);
        }
        return 0;
    }
    

猜你喜欢

转载自blog.csdn.net/BePosit/article/details/82689787