HDU - 5952 Counting Cliques(DFS)
InputThe 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.OutputFor each test case, output the number of cliques with size S in the graph.Sample Input
- 3
- 4 3 2
- 1 2
- 2 3
- 3 4
- 5 9 3
- 1 3
- 1 4
- 1 5
- 2 3
- 2 4
- 2 5
- 3 4
- 3 5
- 4 5
- 6 15 4
- 1 2
- 1 3
- 1 4
- 1 5
- 1 6
- 2 3
- 2 4
- 2 5
- 2 6
- 3 4
- 3 5
- 3 6
- 4 5
- 4 6
- 5 6
Sample Output
- 3
- 7
- 15
- 思路:
如何找到一个k阶的完全图?如果一个图是完全图,那么引入一个新的点,这个点与原图中的每一点都有边相连,新图还是完全图。用了num数组来记录原图上的点。建图时,、只建了从编号小的点到编号大的点之间的边。
这是由于,每次没有必要建立反向边。反而不建反向边的话,会少了去重的过程。
代码:
- #include<iostream>
- #include<algorithm>
- #include<vector>
- #include<stack>
- #include<queue>
- #include<map>
- #include<set>
- #include<cstdio>
- #include<cstring>
- #include<cmath>
- #include<ctime>
- #define fuck(x) cout<<#x<<" = "<<x<<endl;
- #define ls (t<<1)
- #define rs ((t<<1)+1)
- using namespace std;
- typedef long long ll;
- typedef unsigned long long ull;
- const int maxn = ;
- const int inf = 2.1e9;
- const ll Inf = ;
- const int mod = ;
- const double eps = 1e-;
- const double pi = acos(-);
- vector<int>u[maxn];
- int n,m,k;
- int ans;
- int top;
- int num[maxn];
- bool mp[maxn][maxn];
- void dfs(int t,int d)
- {
- if(d==k){ans++;return;}
- int siz = u[t].size();
- bool flag = false;
- for(int i=;i<siz;i++){
- int cnt = u[t][i];
- flag = false;
- for(int j=;j<=top;j++){
- if(!mp[cnt][num[j]]){flag = true;break;}
- }
- if(flag){continue;}
- num[++top]=cnt;
- dfs(cnt,d+);
- top--;
- }
- }
- int main()
- {
- int T;
- scanf("%d",&T);
- while(T--){
- ans = ;
- scanf("%d%d%d",&n,&m,&k);
- memset(mp,,sizeof(mp));
- for(int i=;i<=n;i++){
- u[i].clear();
- }
- int x,y;
- for(int i=;i<=m;i++){
- scanf("%d%d",&x,&y);
- if(x>y){swap(x,y);}
- u[x].push_back(y);
- mp[x][y]=mp[y][x]=true;
- }
- for(int i=;i<=n;i++){
- num[++top]=i;
- dfs(i,);
- top--;
- }
- 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. Give ...
- 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 ...
- HDU5952 Counting Cliques计算完全图的个数 巧妙构图+dfs
题目传送门 题目大意:给出n个点,m条无向边,让你计算这幅母图中有几个大小为s的完全图. 完全图的意思是任意一个点都和其他点直接相连,完全图的大小指的就是完全图点的个数. 思路:比较巧妙的构图方式.我 ...
随机推荐
- vue axios 封装(二)
封装二: http.js import axios from 'axios' import storeHelper from './localstorageHelper' // 全局设置 const ...
- 实验吧 WEB 貌似有点难
错误!你的IP不在允许列表之内! 提示:代码审计 这个提示可谓是非常良心了,一看源代码是一个识别ip地址的东西,如果IP为1.1.1.1那么就会得到KEY. 第一个if是判断是否有client-ip ...
- HDU1800 字典树写法
题意:高级魔法师可以教低级魔法师 魔法扫把技能,同时教会了的低级魔法师又可以教比他更低级是,是传递的关系 同时如果教会了的话,他们可以同时坐一个扫把 问最少需要多少个扫把 思路:就是判断相同的数字最多 ...
- 【UOJ349】【WC2018】即时战略 LCT 动态点分治
这是一道交互题 题目大意 有一棵\(n\)个点的树.最开始\(1\)号点是白的,其他点是黑的. 每次你可以执行一个操作:\(explore(x,y)\).要求\(x\)是一个白点.该函数会返回从\(x ...
- bzoj 3674: 可持久化并查集加强版 (启发式合并+主席树)
Description Description:自从zkysb出了可持久化并查集后……hzwer:乱写能AC,暴力踩标程KuribohG:我不路径压缩就过了!ndsf:暴力就可以轻松虐!zky:…… ...
- MT【270】含参绝对值函数最大之二
已知$f(x)=2ax\cos^2x+(a-1)\cos x-1,a>0$,记$|f(x)|$的最大值为$A$,1)求A.2)证明:$|-2a\sin 2x+(1-a)\sin x|\le 2A ...
- 【cf842C】 Ilya And The Tree(dfs、枚举因子)
C. Ilya And The Tree 题意 给一棵树求每个点到根的路上允许修改一个为0,gcd的最大值. 题解 g是从根到当前点允许修改的最大gcd,gs为不修改的最大gcd.枚举当前点的因子,更 ...
- 自学华为IoT物联网_03 公共事业物联网常见问题及解决方案
点击返回自学华为IoT物流网 自学华为IoT物联网_03 公共事业物联网常见问题及解决方案 本文从以下六项公共事业,看看物联网的解决方案: 停车问题 路灯管理问题 消防栓管理问题 井盖管理问题 水表管 ...
- [2017-7-26]Android Learning Day4
RecycleView 恩,学习Fragment的过程中的一个小实践居然用到了RecycleView!坑了我好久有木有!!好气哦,从昨晚到现在.(现在也还是一头雾水,不过照搬也会用了) 这是第一版的代 ...
- luogu1983 车站分级 (拓扑排序)
对每趟车建一个虚点p,对于不停车的x,连边(x,p,1):对于停车的y,连边(p,y,0)有一条边(a,b,l)就是说b-a>=l由于题目保证一定能走,直接拓扑序dp算最大的就行了 #inclu ...