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. 

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

Sample Output

3
7
15 思路:
如何找到一个k阶的完全图?如果一个图是完全图,那么引入一个新的点,这个点与原图中的每一点都有边相连,新图还是完全图。用了num数组来记录原图上的点。建图时,、只建了从编号小的点到编号大的点之间的边。
这是由于,每次没有必要建立反向边。反而不建反向边的话,会少了去重的过程。
代码:
#include<iostream>
#include<algorithm>
#include<vector>
#include<stack>
#include<queue>
#include<map>
#include<set>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<ctime>
#define fuck(x) cout<<#x<<" = "<<x<<endl;
#define ls (t<<1)
#define rs ((t<<1)+1)
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const int maxn = ;
const int inf = 2.1e9;
const ll Inf = ;
const int mod = ;
const double eps = 1e-;
const double pi = acos(-);
vector<int>u[maxn];
int n,m,k;
int ans;
int top;
int num[maxn];
bool mp[maxn][maxn];
void dfs(int t,int d)
{
if(d==k){ans++;return;}
int siz = u[t].size();
bool flag = false;
for(int i=;i<siz;i++){
int cnt = u[t][i];
flag = false;
for(int j=;j<=top;j++){
if(!mp[cnt][num[j]]){flag = true;break;}
}
if(flag){continue;} num[++top]=cnt;
dfs(cnt,d+);
top--;
}
} int main()
{
int T;
scanf("%d",&T);
while(T--){
ans = ;
scanf("%d%d%d",&n,&m,&k);
memset(mp,,sizeof(mp));
for(int i=;i<=n;i++){
u[i].clear();
}
int x,y;
for(int i=;i<=m;i++){
scanf("%d%d",&x,&y);
if(x>y){swap(x,y);}
u[x].push_back(y);
mp[x][y]=mp[y][x]=true;
} for(int i=;i<=n;i++){
num[++top]=i;
dfs(i,);
top--;
}
printf("%d\n",ans);
}
return ;
}
 

HDU - 5952 Counting Cliques(DFS)的更多相关文章

  1. HDU - 5952 Counting Cliques(dfs搜索)

    题目: A clique is a complete graph, in which there is an edge between every pair of the vertices. Give ...

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

    Counting Cliques Time Limit: 8000/4000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) ...

  3. HDU 5952 Counting Cliques(dfs)

    Counting Cliques Time Limit: 8000/4000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) ...

  4. HDU - 5952 Counting Cliques

    Counting Cliques HDU - 5952 OJ-ID: hdu-5952 author:Caution_X date of submission:20191110 tags:dfs,gr ...

  5. hdu 5952 Counting Cliques 求图中指定大小的团的个数 暴搜

    题目链接 题意 给定一个\(n个点,m条边\)的无向图,找出其中大小为\(s\)的完全图个数\((n\leq 100,m\leq 1000,s\leq 10)\). 思路 暴搜. 搜索的时候判断要加进 ...

  6. Counting Cliques HDU - 5952 单向边dfs

    题目:题目链接 思路:这道题vj上Time limit:4000 ms,HDU上Time Limit: 8000/4000 MS (Java/Others),且不考虑oj测评机比现场赛慢很多,但10月 ...

  7. hdu 3887 Counting Offspring dfs序+树状数组

    Counting Offspring Time Limit: 15000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Othe ...

  8. HDU 2952 Counting Sheep(DFS)

    题目链接 Problem Description A while ago I had trouble sleeping. I used to lie awake, staring at the cei ...

  9. HDU5952 Counting Cliques计算完全图的个数 巧妙构图+dfs

    题目传送门 题目大意:给出n个点,m条无向边,让你计算这幅母图中有几个大小为s的完全图. 完全图的意思是任意一个点都和其他点直接相连,完全图的大小指的就是完全图点的个数. 思路:比较巧妙的构图方式.我 ...

随机推荐

  1. 转载 --mysql函数大全

    控制流函数 IFNULL(expr1,expr2) 如果expr1不是NULL,IFNULL()返回expr1,否则它返回expr2.IFNULL()返回一个数字或字符串值,取决于它被使用的上下文环境 ...

  2. Java反射和注解

    反射:http://blog.csdn.net/liujiahan629629/article/details/18013523 注解:http://www.cnblogs.com/peida/arc ...

  3. Jira的搭建

    一.环境准备 jira7.2的运行是依赖java环境的,也就是说需要安装jdk并且要是1.8以上版本,如下: java -version 除此之外,我们还需要为jira创建对应的数据库.用户名和密码, ...

  4. .net core 2.0 MVC区域

    区域 创建对应的目录结构 Areas System Controllers Views 在Startup.cs 注册路由 在控制器上方加上`[Area("system")]` // ...

  5. CH2401 送礼物(算竞进阶习题)

    双向dfs 数据不是很大,但是如果直接暴搜的话2^45肯定过不了的.. 所以想到乱搞!!要让程序跑的更快,肯定要减下搜索树的规模,再加上这道题双搜的暗示比较明显(逃),所以就来乱搞+双搜求解 所以先从 ...

  6. HDU2204 Eddy's爱好

    题意:给你一个正整数N,确定在1到N之间有多少个可以表示成M^K(K>1)的数. 解析:一个数N 开K次根后得到M  则小于M的所有数的K次方一定小于N 因为任何一个合数都能分解为素数的乘积 所 ...

  7. 【BZOJ2333】【SCOI2011】棘手的操作 treap合并

    题目大意 有\(n\)个节点,标号从1到\(n\),这\(n\)个节点一开始相互不连通.第\(i\)个节点的初始权值为\(a_i\),接下来有如下一些操作: \(U~x~y\):加一条边,连接第\(x ...

  8. apache 与 tomcat、PHP 关系

    Apache:web网络服务器,只支持静态网页,如HTML,C语言开发的 Tomcat:web网络服务器,是apache的扩展,且是个java代码解释器,可脱离apache独立使用,Servlet.J ...

  9. MT【301】值域宽度

    (2015浙江理科)已知函数$f(x)=x^2+ax+b,(a,b\in R)$.记$M(a,b)$是$|f(x)|$在区间$[-1,1]$上的最大值.(1)证明:当$|a|\ge2$时,$M(a,b ...

  10. 【CF446C】DZY Loves Fibonacci Numbers (线段树 + 斐波那契数列)

    Description ​ 看题戳我 给你一个序列,要求支持区间加斐波那契数列和区间求和.\(~n \leq 3 \times 10 ^ 5, ~fib_1 = fib_2 = 1~\). Solut ...