HDU 5952 Counting Cliques 【DFS+剪枝】 (2016ACM/ICPC亚洲区沈阳站)

Counting Cliques

Time Limit: 8000/4000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 539    Accepted Submission(s): 204

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
 
Recommend
jiangzijing2015   |   We have carefully selected several similar problems for you:  5960 5959 5958 5957 5956 
 

Statistic | Submit | Discuss | Note

题目链接:

  http://acm.hdu.edu.cn/showproblem.php?pid=5952

题目大意:

  给N个点M条边,求大小位S的集合的个数,要求集合内的元素两两有边(完全图)。

题目思路:

  【DFS+剪枝】

  每个点最多20条出边。暴力。

  首先去掉边数<S-1的点,枚举剩下的X,枚举完X可以把X的所有边删掉。加几个小剪枝即可。

 //
//by coolxxx
//#include<bits/stdc++.h>
#include<iostream>
#include<algorithm>
#include<string>
#include<iomanip>
#include<map>
#include<stack>
#include<queue>
#include<set>
#include<bitset>
#include<memory.h>
#include<time.h>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
//#include<stdbool.h>
#include<math.h>
#pragma comment(linker,"/STACK:1024000000,1024000000")
#define min(a,b) ((a)<(b)?(a):(b))
#define max(a,b) ((a)>(b)?(a):(b))
#define abs(a) ((a)>0?(a):(-(a)))
#define lowbit(a) (a&(-a))
#define sqr(a) ((a)*(a))
#define swap(a,b) ((a)^=(b),(b)^=(a),(a)^=(b))
#define mem(a,b) memset(a,b,sizeof(a))
#define eps (1e-8)
#define J 10000
#define mod 2147493647
#define MAX 0x7f7f7f7f
#define PI 3.14159265358979323
#define N 104
#define M 2004
using namespace std;
typedef long long LL;
double anss;
LL aans;
int cas,cass;
int n,m,lll,ans,sz;
int last[N];
struct xxx
{
int next,to;
}a[M];
int b[N],c[N],in[N];
void add(int x,int y)
{
a[++sz].next=last[x];
a[sz].to=y;
last[x]=sz;
}
bool ma[N][N];
void dfs(int top,int now)
{
if(top-+lll-now<cas)return;
if(top==cas+)
{
ans++;
return;
}
int i,j,to;
for(i=last[now];i;i=a[i].next)
{
to=a[i].to;
if(!ma[c[now]][c[to]] || to<now)continue;
for(j=;j<top;j++)
if(!ma[c[to]][b[j]])break;
if(j<top)continue;
b[top]=to;
dfs(top+,to);
b[top]=;
}
}
int main()
{
#ifndef ONLINE_JUDGE
freopen("1.txt","r",stdin);
// freopen("2.txt","w",stdout);
#endif
int i,j,k;
int x,y,z;
// init();
for(scanf("%d",&cass);cass;cass--)
// for(scanf("%d",&cas),cass=1;cass<=cas;cass++)
// while(~scanf("%s",s))
// while(~scanf("%d%d",&n,&m))
{
mem(ma,);mem(in,);mem(last,);ans=;lll=;sz=;
scanf("%d%d%d",&n,&m,&cas);
for(i=;i<=m;i++)
{
scanf("%d%d",&x,&y);
in[x]++,in[y]++;
ma[x][y]=ma[y][x]=;
}
for(i=;i<=n;i++)
if(in[i]>=cas-)
c[++lll]=i;
for(i=;i<=lll;i++)
for(j=i+;j<=lll;j++)
if(ma[c[i]][c[j]])
add(i,j);
for(i=;i<=lll;i++)
{
b[]=c[i];
dfs(,i);
for(j=last[c[i]];j;j=a[j].next)
ma[c[a[j].to]][c[i]]=ma[c[i]][c[a[j].to]]=;
}
printf("%d\n",ans);
}
return ;
}
/*
// //
*/
上一篇:Java程序设计11——异常处理


下一篇:JAVA获取当前系统时间System.currentTimeMillis()