hdu5952(暴搜+剪枝+铜牌题)
来源:互联网 发布:python pillow手册 编辑:程序博客网 时间:2024/04/27 13:51
Counting Cliques
Time Limit: 8000/4000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 2956 Accepted Submission(s): 1072
Problem Description
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.
Sample Input
34 3 21 22 33 45 9 31 31 41 52 32 42 53 43 54 56 15 41 21 31 41 51 62 32 42 52 63 43 53 64 54 65 6
Sample Output
3715
Source
2016ACM/ICPC亚洲区沈阳站-重现赛(感谢东北大学)
题意:n个点,m条边的无向图中,求点数为s的完全子图的数量。
思路:首先想到的肯定是建立一个无向边的图,进行暴力搜索每一个点即可,但是这样一来就会TLE。现在进行一些优化就是直接建立有向图,默认从低到高,因此只需要暴力搜索每一个点,将可能成为s元完全子图的点(这个点相连的所有点)也加入,同时更新这个完全子图中的点数和那些点,搜索到完全子图中点数为s返回即可。具体相应操作见代码中注释。
代码:
#include<bits/stdc++.h>using namespace std;typedef long long LL;const int maxn=1005;const int maxm=10005;int point[maxn],in[maxn];bool vis[maxn][maxn];int ans,n,m,s;vector<int>edge[maxm];void dfs(int u,int sum,int fa){ if(sum==s)//点数满足条件的完全图 { ans++; return; } if(in[u]<s-1) return;//如果某个点的入度小于s-1,这个点舍弃 for(int i=0;i<edge[u].size();i++) { int v=edge[u][i]; if(v==fa) continue; bool flag=true; for(int j=0;j<sum;j++)//判断这个点是否和当前图中其他点相互连通 { if(vis[point[j]][v]==false) { flag=false; break; } } if(flag) { point[sum++]=v;//满足条件就加进当前图中继续搜索 dfs(v,sum,u); sum--; } }}int main(){ int T; scanf("%d",&T); while(T--) { scanf("%d%d%d",&n,&m,&s); for(int i=0;i<n;i++) edge[i].clear(); memset(in,0,sizeof(in)); memset(point,0,sizeof(point)); memset(vis,false,sizeof(vis)); for(int i=1;i<=m;i++) { int u,v; scanf("%d%d",&u,&v); if(u>v) swap(u,v); in[u]++,in[v]++; edge[u].push_back(v); vis[u][v]=vis[v][u]=true;//标记哪些边出现 } ans=0; for(int i=1;i<=n;i++)//枚举这些点 { point[0]=i;//记录点 dfs(i,1,-1); } printf("%d\n",ans); } return 0;}
阅读全文
0 0
- hdu5952(暴搜+剪枝+铜牌题)
- HDU5952 Counting Cliques(DFS+剪枝)
- HDU5952 Counting Cliques 【搜索剪枝】
- hdu5952
- HDU5952
- hdu5952-搜索&剪枝&别人都A系列&技巧-Counting Cliques
- hdu5952 Counting Cliques(搜索)
- HDU5952 Counting Cliques (dfs)
- HDOJ 5538 House Building(qwb铜牌水题 几何)
- HDOJ 5546 Ancient Go(qwb铜牌题 DFS搜索)
- HDOJ 5573 Binary Tree(qwb铜牌题 贪心+DFS)
- HDOJ 5533 Dancing Stars on Me(qwb铜牌水题 几何)
- HDOJ 5551 Huatuo's Medicine(qwb铜牌水题 做过的最水的)
- HDOJ 5532 Almost Sorted Array(qwb铜牌水题 最大上升子序列)
- HDOJ 5536 Chip Factory (qwb铜牌水题 暴力+运气)
- HDOJ 5540 Secrete Master Plan(qwb铜牌题 用%进行环的旋转)
- 2015 ICPC长春赛区铜牌题
- 金牌、银牌、铜牌(C语言)
- validate验证+ajax
- Maven之自定义archetype生成项目骨架
- Dubbo学习笔记
- 【python系列】numpy中的tile函数
- Python:类属性,实例属性,私有属性与静态方法,类方法,实例方法
- hdu5952(暴搜+剪枝+铜牌题)
- Qt入门学习——Qt Creator的使用
- proto3 语法+使用
- spring源代码-mybatis(2)
- svn 升级 文件 提交 问题
- easyui图标对照
- 杂记——CSS sprites
- 1073. 多选题常见计分法(20)
- 遇到问的机器学习基本问题(2)