Counting Cliques
Time Limit: 8000/4000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 4066 Accepted Submission(s): 1439
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亚洲区沈阳站-重现赛(感谢东北大学)
找出一个图中大小为S的团的数量
数据量很小,无脑暴力就行了
注意为了保证不重复,这些点按照递增排列,那么对于边u-v(u<v)此时图就只需要存u->v就行了,不然会T
#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);
if(u > v) swap(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;
}