Counting Cliques
Time Limit: 8000/4000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 1051 Accepted Submission(s): 409
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亚洲区沈阳站-重现赛(感谢东北大学)
http://acm.split.hdu.edu.cn/showproblem.php?pid=5952
一个集团由S个点组成,其中任意两点都相连
求这样的集团的总数目
疏散图 肯定用vector建图 OR 链式向前星
还是TE 这题有多个剪枝的地方
#include<bits/stdc++.h>
using namespace std;
vector<int>ap[105];
int pp[105][105];
int jl[105];
int cnt;
int s;
int dir[105];
void dfs(int u,int k)
{
int i,j,flag,v,flagg;
if(k==s-1)
{
/* flag=0;//数目可能过多
for(i=0; i<=k; i++)
{
for(j=i+1; j<=k; j++)
if(!pp[jl[i]][jl[j]])
{
flag=1;
break;
}
if(flag) break;
}
if(!flag) */
cnt++;
return ;
}
for(i=0; i<ap[u].size(); i++)
{
v=ap[u][i];
flagg=0;
//if(v<=u) continue; 只添加小点这一步就可以去掉了
for(j=0; j<=k; j++)//剪枝
{ //每次加一个点和前面加进来的点都判断一次
if(!pp[jl[j]][v])//不能全加进来再判断
{
flagg=1;
break;
}
}
if(flagg) continue;
jl[k+1]=v;
dfs(v,k+1);
}
}
int main()
{
int t,n,m,i;
scanf("%d",&t);
while(t--)
{
scanf("%d%d%d",&n,&m,&s);
memset(ap,0,sizeof(ap));
memset(pp,0,sizeof(pp));
memset(dir,0,sizeof(dir));
int u,v;
for(i=0; i<m; i++)
{
scanf("%d%d",&u,&v);
if(pp[u][v]) continue;
//ap[u].push_back(v);
//ap[v].push_back(u);
pp[u][v]=pp[v][u]=1;
dir[u]++;
dir[v]++;
}
cnt=0;
for(i=1; i<=100; i++)//剪枝度小于s-1的
for(int j=i+1; j<=100; j++)
if(dir[i]>=s-1&&dir[j]>=s-1&&pp[i][j])
ap[i].push_back(j);//只添加小点
//反正为了去重吗 1 2 3 2 3 ....就不能往前面去选了
for(i=1; i<=n; i++)
{
if(dir[i]<s-1) continue;
jl[0]=i;
dfs(i,0);
}
printf("%d\n",cnt);
}
return 0;
}
搜索for (即每次的操作数能少尽量少) 这里相当于 N^m 中 的 N