HDU - 5952 Counting Cliques(图上暴力搜索+技巧剪枝)
2017-10-19 21:56
645 查看
题目大意:
给你一个无向图,100个点1000条边,问你这个图中有多少个小于等于s的点集可以组成一个完全图。s<=10分析:
其实是想复杂了,真的只需要暴力搜索就可以了。分析复杂度的时候不应该是考虑每个点都有20个出边,所以是10的20次方。而应该是考虑只有1000条边,所以我最多才枚举1000条边,100个点,再加上每次判断最多1e6的时间复杂度,再加一些剪枝,肯定不会超时的。然后就是如何避免搜索出重复的呢,我的考虑是,用邻接表存边,而且我只存起点标号小于终点标号的边,这样在枚举的过程中我既避免了重复计算,又完成了剪枝。还降低了代码复杂度。
代码:
#include<bits/stdc++.h> using namespace std; int con[105][105],ans,n,m,s; vector<int>a[105]; int now[20]; void dfs(int x) { if(x==s-1) { ans++; return; } int pre=now[x]; for(int i=0;i<a[pre].size();i++) { int flag=1; for(int j=0;j<=x;j++) { if(con[a[pre][i]][now[j]]==0) { flag=0;break; } } if(flag==1) { now[x+1]=a[pre][i]; dfs(x+1); } } } int main() { int t; scanf("%d",&t); while(t--) { memset(con,0,sizeof(con)); for(int i=0;i<105;i++)a[i].clear(); scanf("%d%d%d",&n,&m,&s); for(int i=0;i<m;i++) { int temp_f,temp_t; scanf("%d%d",&temp_f,&temp_t); con[temp_f][temp_t]=1; con[temp_t][temp_f]=1; if(temp_t<temp_f){int temp=temp_f;temp_f=temp_t;temp_t=temp;} a[temp_f].push_back(temp_t); } ans=0; memset(now,0,sizeof(now)); for(int i=1;i<=n;i++) { now[0]=i; dfs(0); } printf("%d\n",ans); } }
相关文章推荐
- -----暴力搜索-hdu 5952 Counting Cliques
- Network Saboteur POJ - 2531 暴力搜索+剪枝技巧
- HDU 5952 Counting Cliques 暴力搜索+巧妙建边 - 2016ICPC亚洲沈阳站
- Counting Cliques HDU - 5952 (暴力+剪枝)
- HDU 5952 Counting Cliques 暴力搜索
- HDU5952 Counting Cliques 【搜索剪枝】
- hdu 5952 暴力搜索
- hdu 5952 Counting Cliques 暴力搜索 2016沈阳区域赛
- HDU 4620 Fruit Ninja Extreme(2013多校第二场 剪枝搜索)
- [HDU 4277](长春 1011 )搜索+剪枝
- [HDU 3017] Treasure Division (暴力+折半搜索)
- HDU 4499 Cannon (暴力搜索)
- hdu 1399 Starship Hakodate-maru (暴力搜索)
- 2013 多校第九场 hdu 4691 Front compression(暴力 + 剪枝 OR 后缀数组)
- HDU 5636 Shortest Path 分治+搜索剪枝
- HDU 4876 ZCC loves cards (2014多校联合训练第二场1005) 解题报告(暴力+剪枝)
- [HDU 1427]速度计算24点(DFS暴力搜索)
- hdu_4462 Scaring the Birds 暴力搜索
- poj 1416 (hdu 1539)Shredding Company:剪枝搜索
- 【HDU 5952】Counting Cliques 【最大团思想+搜索】