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 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搜索)的更多相关文章
- 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 ...
- HDU 5952 Counting Cliques 【DFS+剪枝】 (2016ACM/ICPC亚洲区沈阳站)
Counting Cliques Time Limit: 8000/4000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) ...
- HDU 5952 Counting Cliques(dfs)
Counting Cliques Time Limit: 8000/4000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) ...
- HDU - 5952 Counting Cliques
Counting Cliques HDU - 5952 OJ-ID: hdu-5952 author:Caution_X date of submission:20191110 tags:dfs,gr ...
- hdu 5952 Counting Cliques 求图中指定大小的团的个数 暴搜
题目链接 题意 给定一个\(n个点,m条边\)的无向图,找出其中大小为\(s\)的完全图个数\((n\leq 100,m\leq 1000,s\leq 10)\). 思路 暴搜. 搜索的时候判断要加进 ...
- Counting Cliques HDU - 5952 单向边dfs
题目:题目链接 思路:这道题vj上Time limit:4000 ms,HDU上Time Limit: 8000/4000 MS (Java/Others),且不考虑oj测评机比现场赛慢很多,但10月 ...
- hdu 3887 Counting Offspring dfs序+树状数组
Counting Offspring Time Limit: 15000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Othe ...
- HDU 2952 Counting Sheep(DFS)
题目链接 Problem Description A while ago I had trouble sleeping. I used to lie awake, staring at the cei ...
- HDU 1241 Oil Deposits DFS搜索题
题目大意:给你一个m*n的矩阵,里面有两种符号,一种是 @ 表示这个位置有油田,另一种是 * 表示这个位置没有油田,现在规定相邻的任意块油田只算一块油田,这里的相邻包括上下左右以及斜的的四个方向相邻的 ...
随机推荐
- JavaScript倒计时类
(function (){ var jtimer = function() { // init if(arguments.length >= 1) { this.setEndTime(argum ...
- GeHost powershell
PS C:\Users\clu\Desktop> [System.Net.Dns] | Get-Member -Static | Format-Table -AutoSize TypeName: ...
- 【Codevs1080】质数环
http://codevs.cn/problem/1031/ 不讲什么,预处理素数+搜索 // <C.cpp> - Sun Oct 9 12:58:23 2016 // This file ...
- 【Codevs1183】泥泞的道路
Position: http://codevs.cn/problem/1183/ List Codevs1183 泥泞的道路 List Description Input Output Sample ...
- [国家集训队2]Tree I
https://www.zybuluo.com/ysner/note/1294263 题面 给你一个无向带权连通图,每条边是黑色或白色.让你求一棵最小权的恰好有need条白色边的生成树. 题目保证有解 ...
- gitlab gerrit jenkins CI/CD环境集成
http://blog.csdn.net/williamwanglei/article/details/38498465
- 2-4 Vue中的属性绑定和双向数据绑定
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- nginx - ubutun下安装nginx(详述编译方法)
一.使用apt命令安装 sudo apt-get install nginx 二.编译方法安装(个人实践方法,具体见官方文档) 1)说明:我使用的虚拟机是64位 ubuntu server14.04, ...
- E20171226-hm
stack n.栈 heap n.堆 backtracking 回溯法,後戻り storage n. 贮存; 贮藏; 储藏处,仓库; 贮存器,蓄电(瓶); ストレージ
- NestedPreb
屌丝手动版 One of the things we’re sorely missing from Unity is nested prefabs. So we rolled this little ...