HDU 5952 Counting Cliques(无向图定向搜索)

20 篇文章 0 订阅

Counting Cliques

Time Limit: 8000/4000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 3480 Accepted Submission(s): 1254

Problem Description
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.

Input
The first line is the number of test cases. For each test case, the first line contains 3 integers N,M and S (N ≤ 100,M ≤ 1000,2 ≤ S ≤ 10), each of the following M lines contains 2 integers u and v (1 ≤ u < v ≤ N), which means there is an edge between vertices u and v. It is guaranteed that the maximum degree of the vertices is no larger than 20.

Output
For each test case, output the number of cliques with size S in the graph.

Sample Input
3
4 3 2
1 2
2 3
3 4
5 9 3
1 3
1 4
1 5
2 3
2 4
2 5
3 4
3 5
4 5
6 15 4
1 2
1 3
1 4
1 5
1 6
2 3
2 4
2 5
2 6
3 4
3 5
3 6
4 5
4 6
5 6

Sample Output
3
7
15

Source
2016ACM/ICPC亚洲区沈阳站-重现赛(感谢东北大学)

Recommend
jiangzijing2015

题目大意

  有一张最多 100 个点的无向图,求大小为 S 的团有多少个。

解题思路

  最大团极大团问题是NP问题,所以这题也很自然地想到搜索。不过如果直接暴搜的话可以发现对于每个大小为S的团会被搜到 S <script type="math/tex" id="MathJax-Element-124">S</script>次。考虑怎么优化这里,考虑对于任一一个团一定有且仅有一条按照顶点序号走过所有点的路径。所以我们就可以对于每条边定向为从编号小的点到编号大的点。这样再暴搜的时候就优化了非常多,就可以通过这道题了。

AC代码

#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<cmath>
#include<algorithm>
#include<vector>
#include<queue>
#include<stack>
#include<map>
#include<set>
using namespace std;
#define LL long long
#define INF 0x3f3f3f3f

const int MAXV=100+3;

int V, E, S;
vector<int> G[MAXV];
bool maze[MAXV][MAXV];
int ans;
int save[MAXV];

void init()
{
    for(int i=0;i<=V;++i)
        G[i].clear();
    for(int i=0;i<=V;++i)
        for(int j=0;j<=V;++j)
            maze[i][j]=false;
    ans=0;
}

void dfs(int u, int n)
{
    if(n==S)
    {
        ++ans;
        return ;
    }
    for(int i=0;i<G[u].size();++i)
    {
        int v=G[u][i];
        bool ok=true;
        for(int j=0;j<n;++j)
            if(!maze[save[j]][v])
            {
                ok=false;
                break;
            }
        if(!ok)
            continue;
        save[n]=v;
        dfs(v, n+1);
    }
}

int main()
{
    int T_T;
    scanf("%d", &T_T);
    while(T_T--)
    {
        scanf("%d%d%d", &V, &E, &S);
        init();
        for(int i=0;i<E;++i)
        {
            int u, v;
            scanf("%d%d", &u, &v);
            if(u<=v)
            {
                G[u].push_back(v);
                maze[u][v]=true;
            }
            else
            {
                G[v].push_back(u);
                maze[v][u]=true;
            }
        }
        for(int u=0;u<V;++u)
            if(G[u].size()+1>=S)
            {
                save[0]=u;
                dfs(u, 1);
            }
        printf("%d\n", ans);
    }

    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值