HDU - 5952 Counting Cliques (暴搜)

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

题意:给一个图,问其中大小为S的完全图有多少个

思路:数据就100,明显的暴搜。记录当前连通块中选了哪些点,每次用与当前连通块中所有点都连通的点进行更新

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const double PI = acos(-1.0);
const double eps = 1e-6;
const int mod = 2147493647;
const int inf = 0x3f3f3f3f;
const int N = 110;

inline int read() {
    int x = 0;
    char c = 0;
    c = getchar();
    while(isdigit(c)) {
        x = (x << 1) + (x << 3) + (c ^ 48);
        c = getchar();
    }
    return x;
}

int n, m, s;
ll ans;
bool mp[N][N];
vector<int>vec[N];
bool vis[N];
int node[15];

void dfs(int tot, int u) {
    if(tot == s) {
        ans++;
        return ;
    }
    int siz = vec[u].size();
    for(int i = 0; i < siz; ++i) {
        int v = vec[u][i];
        bool flag = 1;
        for(int j = 1; j <= tot; ++j) {
            if(!mp[node[j]][v]) {
                flag = 0;
                break;
            }
        }
        if(flag) {
            node[tot + 1] = v;
            dfs(tot + 1, v);
        }
    }
}

int main() {
//    freopen("in.txt", "r", stdin);
    int t, u, v;
    t = read();
    while(t--) {
        n = read(), m = read(), s = read();
        for(int i = 1; i <= n; ++i) vec[i].clear();
        for(int i = 0; i < N; ++i)
            for(int j = 0; j <= i; ++j)
                mp[i][j] = mp[j][i] = 0;
        while(m--) {
            u = read(), v = read();
            mp[u][v] = mp[v][u] = 1;
            vec[u].push_back(v);
        }
        ans = 0;
        for(int i = 1; i <= n; ++i) {
            node[1] = i;
            dfs(1, i);
        }
        printf("%lld\n", ans);
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/weixin_43871207/article/details/110092054
今日推荐