题目:

  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. android 6.0编译时出现ERROR:Security problem ,see jack server log【转】

    本文转载自:http://blog.csdn.net/a567890k/article/details/52956798 最近编译Android6.0时经常出现以下错误 临时解决方法: Buildin ...

  2. ResolveUrl in external JavaScript file in asp.net project

    https://stackoverflow.com/questions/11263425/page-resolveurl-is-not-working-in-javascript The proble ...

  3. oracle游标的定义使用

    oracle游标的定义使用 2008-02-23 15:12:57|  分类: oracle|字号 订阅 游标中定义的参数只要定义类型,不用定义长度,精度等: 游标使用一: declarecursor ...

  4. Window 无法启动此程序,因为计算机中丢失api-ms-win-crt-runtime-l1-1-0.dll。尝试重新安装该程序以解决此问题。

    现象: 解决办法: 方法一:缺什么补什么 http://www.greenxf.com/soft/125654.html 把api-ms-win-crt-runtime-l1-1-0.dll下载到电脑 ...

  5. pyhon-----安装yaml踩过的坑以及正解

    之前在网上找了各种资料,cmd安装yaml,网上大部分写的都是pip install yaml 可是,执行完就变成Could not find a version that satisfies the ...

  6. bzoj 1689: [Usaco2005 Open] Muddy roads 泥泞的路【贪心】

    按左端点排序,贪心的选即可 #include<iostream> #include<cstdio> #include<algorithm> using namesp ...

  7. 洛谷 P4180 【模板】严格次小生成树[BJWC2010]【次小生成树】

    严格次小生成树模板 算法流程: 先用克鲁斯卡尔求最小生成树,然后给这个最小生成树树剖一下,维护边权转点权,维护最大值和严格次大值. 然后枚举没有被选入最小生成树的边,在最小生成树上查一下这条边的两端点 ...

  8. 洛谷 P1880 [NOI1995]石子合并

    题目描述 在一个圆形操场的四周摆放N堆石子,现要将石子有次序地合并成一堆.规定每次只能选相邻的2堆合并成新的一堆,并将新的一堆的石子数,记为该次合并的得分. 试设计出1个算法,计算出将N堆石子合并成1 ...

  9. C#方法的一些规则

    C# 方法 一个方法是把一些相关的语句组织在一起,用来执行一个任务的语句块.每一个 C# 程序至少有一个带有 Main 方法的类. 要使用一个方法,您需要: 定义方法 调用方法 下面是方法的各个元素: ...

  10. 二分图最大匹配(匈牙利算法) URAL 1721 Two Sides of the Same Coin

    题目传送门 /* 题意:三种人,statements,testdata,anthing.要求两个人能完成s和t两个工作,且rank相差2 二分图匹配:此题学习建图技巧,两个集和内部一定没有边相连,ra ...