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的矩阵,里面有两种符号,一种是 @ 表示这个位置有油田,另一种是 * 表示这个位置没有油田,现在规定相邻的任意块油田只算一块油田,这里的相邻包括上下左右以及斜的的四个方向相邻的 ...
随机推荐
- how to modify vs2017
https://docs.microsoft.com/en-us/visualstudio/install/modify-visual-studio 直接用everything搜索vs_install ...
- [Codeforces 482A] Diverse Permutation
[题目链接] https://codeforces.com/contest/482/problem/A [算法] 首先构造一个(k + 1)个数的序列 , 满足它们的差为1-k 对于i > k ...
- SPOJ OTOCI 动态树 LCT
SPOJ OTOCI 裸的动态树问题. 回顾一下我们对树的认识. 最初,它是一个连通的无向的无环的图,然后我们发现由一个根出发进行BFS 会出现层次分明的树状图形. 然后根据树的递归和层次性质,我们得 ...
- ODB——基于c++的ORM映射框架尝试(使用)
摘要: 2.使用 首先,需要定义一个对象,用来和数据库字段对应: [cce lang=”cpp”] #ifndef VOLUME_H #define VOLUME_H #include #includ ...
- WinRAR 激活码(KEY)
引自:http://blog.sina.com.cn/s/blog_6fd3b4800100nyk0.html WinRAR 是一个强大的压缩文件管理工具.它能备份你的数据,减少你的 E-mail 附 ...
- 如何精通javascript
http://stackoverflow.com/questions/2628672/what-should-every-javascript-programmer-know Not jQuery. ...
- Yii2笔记一
环境LNMP,通过Composer安装 安装Composer(已经安装请跳过) curl -s http://getcomposer.org/installer | php #php可执行文件所在位置 ...
- E20170616-hm
transaction n. 交易,业务,事务; 办理,处理; (一笔) 交易,(一项)事务; (学会等的) 会议记录,学报; isolation n. 隔离; 孤独; 隔离状态; 孤立状 ...
- bzoj1015星球大战(并查集+离线)
1015: [JSOI2008]星球大战starwar Time Limit: 3 Sec Memory Limit: 162 MBSubmit: 5572 Solved: 2563 Descri ...
- P4161 [SCOI2009]游戏
传送门 首先这题的本质就是把\(n\)分成若干个数的和,求他们的\(lcm\)有多少种情况 然后据说有这么个结论:若\(p_1^{c_1}+p_2^{c_2}+...+p_m^{c_m}\leq n\ ...