hdu 5952 Counting Cliques 求图中指定大小的团的个数 暴搜

题目链接

题意

给定一个\(n个点,m条边\)的无向图,找出其中大小为\(s\)的完全图个数\((n\leq 100,m\leq 1000,s\leq 10)\)

思路

暴搜。

搜索的时候判断要加进来的点是否与当前集合中的每个点之间都有边。搜到集合大小为\(s\)就答案+1.

注意

如果不做处理的话,每个完全图都会被搜到\(2^s\)次,其中只有一次是必要的。

因此,一个很显然的常用的考虑是:搜索的时候下一个节点比当前的节点编号大,这样就肯定不会搜重复了。

再稍微转化一下,在建图的时候就可以只建小点指向大点的边。

Code

#include <bits/stdc++.h>
#define maxn 110
#define maxm 1010
using namespace std;
typedef long long LL;
bool mp[maxn][maxn];
int st[12], ecnt, s, ne[maxn];
LL ans;
struct Edge {
    int to, ne;
    Edge(int _to=0, int _ne=0) : to(_to), ne(_ne) {}
}edge[maxm];
bool add(int u, int v) {
    edge[ecnt] = Edge(v, ne[u]);
    ne[u] = ecnt++;
}
bool check(int v, int tot) {
    for (int i = 0;i <= tot; ++i) {
        if (!mp[st[i]][v]) return false;
    }
    return true;
}
void dfs(int tot, int u) {
    st[tot] = u;
    if (tot == s-1) { ++ans, --tot; return; }
    for (int i = ne[u]; ~i; i = edge[i].ne) {
        int v = edge[i].to;
        if (check(v, tot)) dfs(tot+1, v);
    }
}
void work() {
    int n, m;
    scanf("%d%d%d", &n, &m, &s);
    memset(mp, 0, sizeof(mp));
    ecnt = 0; memset(ne, -1, sizeof(ne));
    for (int i = 0; i < m; ++i) {
        int x, y;
        scanf("%d%d", &x, &y);
        if (x > y) swap(x, y);
        mp[x][y] = 1; add(x, y);
    }
    ans = 0;
    for (int i = 1; i <= n; ++i) dfs(0, i);
    printf("%lld\n", ans);
}
int main() {
    int T;
    scanf("%d", &T);
    while (T--) work();
    return 0;
}

转载于:https://www.cnblogs.com/kkkkahlua/p/7704245.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值