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. Python memecache

    memcached Memcached 是一个高性能的分布式内存对象缓存系统,用于动态Web应用以减轻数据库负载,故常用来做数据库缓存.它通过在内存中缓存数据和对象来减少读取数据库的次数,从而提高动态 ...

  2. Eclipse配置C++时的三个关键环境变量

    ECLIPSE下载很简单,然后装上MinGW,安装就完成了,关键是要配置三个环境变量 include——C:\MinGW\include lib——C:\MinGW\lib path——C:\MinG ...

  3. DFI LP DK P45 T2RS PLUS BIOS SETTING

    standard cmos features date (mm:dd:yy) mon,oct 11 2016 time (hh:mm:ss) 10 : 10 : 26 ide channel 0 sa ...

  4. 原子变量与CAS算法(二)

    一.锁机制存在的问题 (1)在多线程竞争下,加锁.释放锁会导致比较多的上下文切换和调度延时,引起性能问题. (2)一个线程持有锁会导致其它所有需要此锁的线程挂起. (3)如果一个优先级高的线程等待一个 ...

  5. Cmder使用ls中文显示乱码解决方案

    操作系统:Windows 7 旗舰版 Cmder:1.3.2 默认配置不支持使用ls显示中文命名的文件列表. 解决方法: 按下Win+Alt+P打开设置. 在StartUp - Environment ...

  6. [IOI2018]机械娃娃——线段树+构造

    题目链接: IOI2018doll 题目大意:有一个起点和$m$个触发器,给出一个长度为$n$的序列$a$,要求从起点出发按$a$的顺序经过触发器并回到起点(一个触发器可能被经过多次也可能不被经过), ...

  7. jsp大学作业:jsp编写单选,复选判断题及得分情况

    project_1_1.jsp <%@ page contentType="text/html;charset=utf-8" language="java" ...

  8. [POJ2976] Dropping tests

    传送门:>Here< 题意:给出长度相等的数组a和b,定义他们的和为$\dfrac{a_1+a_2+...+a_n}{b_1+b_2+...+b_n}$.现在可以舍弃k对元素(一对即$a[ ...

  9. 最简单的spring boot web项目

    搭建效果为: 直接在网页输入请求,在页面中显示一行文字:Hello,Spring Boot 与一般的wen项目不同的地方: 1.不需要配置web.xml 文件,但需要注解@SpringBootAppl ...

  10. 【hdu 6161】Big binary tree(二叉树、dp)

    多校9 1001 hdu 6161 Big binary tree 题意 有一个完全二叉树.编号i的点值是i,操作1是修改一个点的值为x,操作2是查询经过点u的所有路径的路径和最大值.10^5个点,1 ...