Counting Cliques
Time Limit: 8000/4000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 184 Accepted Submission(s): 56
Problem DescriptionA 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. InputThe 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. OutputFor 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
思路:构造一个团,如果一个点与这个团的所有点都有边,则将其加入团中,统计含s个点的团的个数。关于优化,可以建单向边来减少搜索量。
代码:
#include<bits/stdc++.h>
//#include<regex>
#define db double
#include<vector>
#define ll long long
#define vec vector<ll>
#define Mt vector<vec>
#define ci(x) scanf("%d",&x)
#define cd(x) scanf("%lf",&x)
#define cl(x) scanf("%lld",&x)
#define pi(x) printf("%d\n",x)
#define pd(x) printf("%f\n",x)
#define pl(x) printf("%lld\n",x)
#define MP make_pair
#define PB push_back
#define inf 0x3f3f3f3f3f3f3f3f
#define fr(i,a,b) for(int i=a;i<=b;i++)
const int N=1e3+;
const int mod=1e9+;
const int MOD=mod-;
const db eps=1e-;
using namespace std;
bool d[][];
int n,m,s,t;
int ans;
vector<int> g[N];
void dfs(int u,int *a,int cnt)
{
if(cnt==s){
ans++;
return;
}
bool ok;
for(int i=;i<g[u].size();i++)
{
ok=;
int v=g[u][i];
for(int j=;j<=cnt;j++){
if(!d[a[j]][v]) {ok=;break;}
}
if(ok)
{
a[++cnt]=v;//加点
dfs(v,a,cnt);//继续搜
a[cnt--]=;
}
}
}
int main(){
//freopen("data.in","r",stdin);
//freopen("data.out","w",stdout);
ci(t);
while(t--)
{
ci(n),ci(m),ci(s);
ans=;
for(int i=;i<=n;i++) g[i].clear();
memset(d,,sizeof(d));
for(int i=;i<m;i++){
int u,v;
ci(u),ci(v);
if(u>v) swap(u,v);
g[u].push_back(v);
d[u][v]=d[v][u]=;
}
for(int i=;i<=n;i++){
if(g[i].size()>=s-){
int a[];
a[]=i;//构建团
int cnt=;
dfs(i,a,cnt);
}
}
pi(ans);
}
return ;
}