# HDU 5952 Counting Cliques（无向图定向搜索）

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$100$个点的无向图，求大小为S$S$的团有多少个。

### 解题思路

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

### 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;
}