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. Google浏览器解决编码乱码问题

    新版google浏览器编码乱码没有设置的入口,怎么办呢?. 步骤一: 可以下载goole的插件,名为charset,下载后的文件名为Charset_v0.4.1 步骤二: google右上角-> ...

  2. 另一个ado工具类

    using System;using System.Collections.Generic;using System.Text;using System.Data.SqlClient;using Sy ...

  3. 文件上传.ashx

    using System;using System.Collections.Generic;using System.IO;using System.Linq;using System.Runtime ...

  4. 【python练习题】程序12

    #题目:判断101-200之间有多少个素数,并输出所有素数. #判断素数的方法:用一个数分别去除2到sqrt(这个数),如果能被整除,则表明此数不是素数,反之是素数. from math import ...

  5. luogu T40984Chino的成绩

    Chino的成绩 题目描述 Chino非常注重自己的成绩 Chino有m种方式给自己增加rp以增加成绩,她的每种增加rp的方式都有n个阶段,第i种的第j个阶段增加的rp表示为Aij​,表示连续进行了j ...

  6. hdu1875(最小生成树prime)

    思路:一开始想用贪心来着,发现贪心有缺陷,然后就用了最小生成树来写,这里用了prime算法,首先,先建个图,两点之间的边的权值就是两个点的距离,然后直接prime模板 代码 #include<i ...

  7. Linux下的好用的编辑软件Remarkable

    Linux下的好用的编辑软件Remarkable最近着手开始学习Linux,就想着找一款好用的编辑器作笔记,在网上爬了些贴选择了Remarkable.官网崩了,有没有梯子,废了好大力气才装好.于是把资 ...

  8. P1140 相似基因 最长公共子序列

    思路 类似于最长公共子序列 把一段基因和另外一段基因匹配  不够长的用空基因替换 #include<bits/stdc++.h> using namespace std; const in ...

  9. hdu 2159 FATE (二维完全背包)

    链接:http://acm.hdu.edu.cn/showproblem.php?pid=2159 思路: dp[j][k] 代表消耗耐久度j,干掉k个敌人获得的经验值. 状态转移方程为: dp[j] ...

  10. MT【282】一道几何题

    2010浙江省数学竞赛,附加题. 设$D,E,F$分别为$\Delta ABC$的三边$BC,CA,AB$上的点,记$\alpha=\dfrac{BD}{BC},\beta=\dfrac{BD}{BC ...