题目:

  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.

题意:

  给出一个图有n个点、m条边,给出子图的大小s,要求求出子图是一个完全图,而且图中需要有s个点。

PS:

我竟然把这个题目读成了求图中点数为s的环的个数,这个锅背的很强,,,,

首先回顾一下完全图的性质:完全图中任意一个点与其他的所有的点都有连边。如下:

所以有n个点的完全图会有n*(n-1)/2条边。

思路:这个题如果双向建图,然后遍历求点数为s的完全图,铁定TLE,因为同一个图会被多次搜索。要避免重复搜索,可以单向建图,从小到大dfs遍历,同时在path数组中记录已经在完全图中的点,只要当前的点和之前path中的点都有边(这样path中所有的点才能构成完全图),就将该点记录到path中,点数达到s,答案ans++。

代码:

#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <queue>
#include <map>
#include <set>
#include <vector>
using namespace std;
const int maxn = +;
int m,n,s,ans;
int mp[maxn][maxn];
int path[maxn];
vector<int> G[maxn]; int read()
{
int res = ;
char op = getchar();
if(op>='' && op<='')
{
res = op-'';
op = getchar();
}
while(op>='' && op<='')
{
res = res* + op-'';
op = getchar();
}
return res;
} void init()
{
ans = ;
memset(mp,,sizeof(mp));
memset(path,,sizeof(path));
for(int i = ; i<maxn; i++)
G[i].clear();
} void dfs(int i,int cnt)
{
if(cnt == s)
{
ans++;
return;
}
for(int j = ; j<G[i].size(); j++)
{
int u = G[i][j],f = ;
for(int k = ; k<=cnt; k++)//看点u是否和path中的点是否都有边相连
{
if(mp[u][path[k]] == )
{
f = ;
break;
}
}
if(!f)//都想连就存入path继续遍历
{
path[cnt+] = u;
dfs(u, cnt+);
path[cnt+] = ;
}
}
} int main()
{
int T;
T = read();
//scanf("%d",&T);
while(T--)
{
init();
n = read(),m = read(),s= read();
//scanf("%d%d%d",&n,&m,&s);
for(int i = ; i<m; i++)
{
int st,en;
st = read(),en = read();
//scanf("%d%d",&st,&en);
if(st>en)//单向建图,避免重复遍历
G[en].push_back(st);
else
G[st].push_back(en);
mp[st][en] = ,mp[en][st] = ;//在遍历的时候用来检查是否能构成完全图
}
for(int i = ; i<=n; i++)
{
path[] = i;//记录拿出的可以构成完全图的点
dfs(i,);
path[] = ;
}
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. Given a ...

  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. HDU 1241 Oil Deposits DFS搜索题

    题目大意:给你一个m*n的矩阵,里面有两种符号,一种是 @ 表示这个位置有油田,另一种是 * 表示这个位置没有油田,现在规定相邻的任意块油田只算一块油田,这里的相邻包括上下左右以及斜的的四个方向相邻的 ...

随机推荐

  1. YTU 2636: B3 指向基类的指针访问派生类的成员函数

    2636: B3 指向基类的指针访问派生类的成员函数 时间限制: 1 Sec  内存限制: 128 MB 提交: 433  解决: 141 题目描述 领导类(Leader)和工程师类(Engineer ...

  2. 【POJ 3233】Matrix Power Series

    [题目链接] 点击打开链接 [算法] 要求 A^1 + A^2 + A^3 + ... + A^k 考虑通过二分来计算这个式子 : 令f(k) = A^1 + A^2 + A ^ 3 + ... + ...

  3. 从Linux内核中获取真随机数

    内核随机数产生器 Linux内核实现了一个随机数产生器,从理论上说这个随机数产生器产生的是真随机数.与标准C库中的rand(),srand()产生的伪随机数不同,尽管伪随机数带有一定的随机特征,但这些 ...

  4. 用js获取access_token

    尝试用js获取access_token 最终失败告终,哈哈 1需要 appId和AppSecret 这两个参数是要保密的,建议不要暴露在外面,以防被别有用心的人利用. $.ajax({ url:'ht ...

  5. IDEA中 Spark 读Hbase 报错处理:

    SLF4J: Actual binding is of type [org.slf4j.impl.Log4jLoggerFactory] // :: ERROR RecoverableZooKeepe ...

  6. 昆石VOS3000_2.1.2.4完整安装包及安装脚本

    安装包下载地址:http://www.51voip.org/post/56.html 安装教程: 上传安装包 ·给整个目录授权 chmod 777 /root/vosintsall `核实 关闭sel ...

  7. iptables的介绍

    iptables介绍 iptables 1)iptables程序工作在内核的TCP/IP网络协议栈框架netfilter上,通过网络过滤可以实现入侵检测以及入侵防御功能,而不是单个协议当中. 2)ip ...

  8. 洛谷P3400 仓鼠窝(单调栈)

    P3400 仓鼠窝 题目描述 萌萌哒的Created equal是一只小仓鼠,小仓鼠自然有仓鼠窝啦. 仓鼠窝是一个由n*m个格子组成的行数为n.列数为m的矩阵.小仓鼠现在想要知道,这个矩阵中有多少个子 ...

  9. 洛谷P5055 【模板】可持久化文艺平衡树(FHQ Treap)

    题面 传送门 题解 日常敲板子.jpg //minamoto #include<bits/stdc++.h> #define R register #define inline __inl ...

  10. Hdu 4738 Caocao's Bridges (连通图+桥)

    题目链接: Hdu 4738 Caocao's Bridges 题目描述: 有n个岛屿,m个桥,问是否可以去掉一个花费最小的桥,使得岛屿边的不连通? 解题思路: 去掉一个边使得岛屿不连通,那么去掉的这 ...