1. 程式人生 > >HDU 5952 Counting Cliques(暴搜)

HDU 5952 Counting Cliques(暴搜)

Counting Cliques

Time Limit: 8000/4000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 4066    Accepted Submission(s): 1439Problem 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

找出一個圖中團的點為S的團的數量

資料量很小,無腦暴力就行了

#include <bits/stdc++.h>
using namespace std;
const int MAXN = 105;
const int MAXM = 1005;
int tot,head[MAXN];
int g[MAXN][MAXN];
int t[MAXN];
int sz,ans;
struct node
{
    int to,Next;
}edge[MAXM];
void init()
{
    tot = ans = 0;
    memset(g,0,sizeof(g));
    memset(head,-1,sizeof(head));
}
void addedge(int u,int v)
{
    edge[tot].to = v;
    edge[tot].Next = head[u];
    head[u] = tot++;
}
void dfs(int u,int cnt,int t[])
{
    if(cnt == sz) {
        ans++;
        return;
    }
    for(int i = head[u]; i != -1; i = edge[i].Next) {
        int v = edge[i].to;
        int flag = 0;
        for(int j = 1; j <= cnt; j++) {
            if(!g[v][t[j]]) {
                flag = 1;
                break;
            }
        }
        if(!flag) {
            t[cnt + 1] = v;
            dfs(v,cnt + 1,t);
        }
    }
}
int main(void)
{
    int T,n,m,u,v;
    scanf("%d",&T);
    while(T--) {
        init();
        scanf("%d %d %d",&n,&m,&sz);
        while(m--) {
            scanf("%d %d",&u,&v);
            addedge(u,v);
            g[u][v] = g[v][u] = 1;
        }
        for(int i = 1; i <= n; i++) {
            t[1] = i;
            dfs(i,1,t);
        }
        cout << ans << endl;
    }
    return 0;
}