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. 

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

  1. 3
  2. 4 3 2
  3. 1 2
  4. 2 3
  5. 3 4
  6. 5 9 3
  7. 1 3
  8. 1 4
  9. 1 5
  10. 2 3
  11. 2 4
  12. 2 5
  13. 3 4
  14. 3 5
  15. 4 5
  16. 6 15 4
  17. 1 2
  18. 1 3
  19. 1 4
  20. 1 5
  21. 1 6
  22. 2 3
  23. 2 4
  24. 2 5
  25. 2 6
  26. 3 4
  27. 3 5
  28. 3 6
  29. 4 5
  30. 4 6
  31. 5 6

Sample Output

  1. 3
  2. 7
  3. 15
  4.  
  5. 思路:
    如何找到一个k阶的完全图?如果一个图是完全图,那么引入一个新的点,这个点与原图中的每一点都有边相连,新图还是完全图。用了num数组来记录原图上的点。建图时,、只建了从编号小的点到编号大的点之间的边。
    这是由于,每次没有必要建立反向边。反而不建反向边的话,会少了去重的过程。
    代码:
  1. #include<iostream>
  2. #include<algorithm>
  3. #include<vector>
  4. #include<stack>
  5. #include<queue>
  6. #include<map>
  7. #include<set>
  8. #include<cstdio>
  9. #include<cstring>
  10. #include<cmath>
  11. #include<ctime>
  12. #define fuck(x) cout<<#x<<" = "<<x<<endl;
  13. #define ls (t<<1)
  14. #define rs ((t<<1)+1)
  15. using namespace std;
  16. typedef long long ll;
  17. typedef unsigned long long ull;
  18. const int maxn = ;
  19. const int inf = 2.1e9;
  20. const ll Inf = ;
  21. const int mod = ;
  22. const double eps = 1e-;
  23. const double pi = acos(-);
  24. vector<int>u[maxn];
  25. int n,m,k;
  26. int ans;
  27. int top;
  28. int num[maxn];
  29. bool mp[maxn][maxn];
  30. void dfs(int t,int d)
  31. {
  32. if(d==k){ans++;return;}
  33. int siz = u[t].size();
  34. bool flag = false;
  35. for(int i=;i<siz;i++){
  36. int cnt = u[t][i];
  37. flag = false;
  38. for(int j=;j<=top;j++){
  39. if(!mp[cnt][num[j]]){flag = true;break;}
  40. }
  41. if(flag){continue;}
  42.  
  43. num[++top]=cnt;
  44. dfs(cnt,d+);
  45. top--;
  46. }
  47. }
  48.  
  49. int main()
  50. {
  51. int T;
  52. scanf("%d",&T);
  53. while(T--){
  54. ans = ;
  55. scanf("%d%d%d",&n,&m,&k);
  56. memset(mp,,sizeof(mp));
  57. for(int i=;i<=n;i++){
  58. u[i].clear();
  59. }
  60. int x,y;
  61. for(int i=;i<=m;i++){
  62. scanf("%d%d",&x,&y);
  63. if(x>y){swap(x,y);}
  64. u[x].push_back(y);
  65. mp[x][y]=mp[y][x]=true;
  66. }
  67.  
  68. for(int i=;i<=n;i++){
  69. num[++top]=i;
  70. dfs(i,);
  71. top--;
  72. }
  73. printf("%d\n",ans);
  74. }
  75. return ;
  76. }
  1.  

HDU - 5952 Counting Cliques(DFS)的更多相关文章

  1. HDU - 5952 Counting Cliques(dfs搜索)

    题目: A clique is a complete graph, in which there is an edge between every pair of the vertices. Give ...

  2. HDU 5952 Counting Cliques 【DFS+剪枝】 (2016ACM/ICPC亚洲区沈阳站)

    Counting Cliques Time Limit: 8000/4000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) ...

  3. HDU 5952 Counting Cliques(dfs)

    Counting Cliques Time Limit: 8000/4000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) ...

  4. HDU - 5952 Counting Cliques

    Counting Cliques HDU - 5952 OJ-ID: hdu-5952 author:Caution_X date of submission:20191110 tags:dfs,gr ...

  5. hdu 5952 Counting Cliques 求图中指定大小的团的个数 暴搜

    题目链接 题意 给定一个\(n个点,m条边\)的无向图,找出其中大小为\(s\)的完全图个数\((n\leq 100,m\leq 1000,s\leq 10)\). 思路 暴搜. 搜索的时候判断要加进 ...

  6. Counting Cliques HDU - 5952 单向边dfs

    题目:题目链接 思路:这道题vj上Time limit:4000 ms,HDU上Time Limit: 8000/4000 MS (Java/Others),且不考虑oj测评机比现场赛慢很多,但10月 ...

  7. hdu 3887 Counting Offspring dfs序+树状数组

    Counting Offspring Time Limit: 15000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Othe ...

  8. HDU 2952 Counting Sheep(DFS)

    题目链接 Problem Description A while ago I had trouble sleeping. I used to lie awake, staring at the cei ...

  9. HDU5952 Counting Cliques计算完全图的个数 巧妙构图+dfs

    题目传送门 题目大意:给出n个点,m条无向边,让你计算这幅母图中有几个大小为s的完全图. 完全图的意思是任意一个点都和其他点直接相连,完全图的大小指的就是完全图点的个数. 思路:比较巧妙的构图方式.我 ...

随机推荐

  1. vue axios 封装(二)

    封装二: http.js import axios from 'axios' import storeHelper from './localstorageHelper' // 全局设置 const ...

  2. 实验吧 WEB 貌似有点难

    错误!你的IP不在允许列表之内! 提示:代码审计 这个提示可谓是非常良心了,一看源代码是一个识别ip地址的东西,如果IP为1.1.1.1那么就会得到KEY. 第一个if是判断是否有client-ip ...

  3. HDU1800 字典树写法

    题意:高级魔法师可以教低级魔法师 魔法扫把技能,同时教会了的低级魔法师又可以教比他更低级是,是传递的关系 同时如果教会了的话,他们可以同时坐一个扫把 问最少需要多少个扫把 思路:就是判断相同的数字最多 ...

  4. 【UOJ349】【WC2018】即时战略 LCT 动态点分治

    这是一道交互题 题目大意 有一棵\(n\)个点的树.最开始\(1\)号点是白的,其他点是黑的. 每次你可以执行一个操作:\(explore(x,y)\).要求\(x\)是一个白点.该函数会返回从\(x ...

  5. bzoj 3674: 可持久化并查集加强版 (启发式合并+主席树)

    Description Description:自从zkysb出了可持久化并查集后……hzwer:乱写能AC,暴力踩标程KuribohG:我不路径压缩就过了!ndsf:暴力就可以轻松虐!zky:…… ...

  6. 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 ...

  7. 【cf842C】 Ilya And The Tree(dfs、枚举因子)

    C. Ilya And The Tree 题意 给一棵树求每个点到根的路上允许修改一个为0,gcd的最大值. 题解 g是从根到当前点允许修改的最大gcd,gs为不修改的最大gcd.枚举当前点的因子,更 ...

  8. 自学华为IoT物联网_03 公共事业物联网常见问题及解决方案

    点击返回自学华为IoT物流网 自学华为IoT物联网_03 公共事业物联网常见问题及解决方案 本文从以下六项公共事业,看看物联网的解决方案: 停车问题 路灯管理问题 消防栓管理问题 井盖管理问题 水表管 ...

  9. [2017-7-26]Android Learning Day4

    RecycleView 恩,学习Fragment的过程中的一个小实践居然用到了RecycleView!坑了我好久有木有!!好气哦,从昨晚到现在.(现在也还是一头雾水,不过照搬也会用了) 这是第一版的代 ...

  10. luogu1983 车站分级 (拓扑排序)

    对每趟车建一个虚点p,对于不停车的x,连边(x,p,1):对于停车的y,连边(p,y,0)有一条边(a,b,l)就是说b-a>=l由于题目保证一定能走,直接拓扑序dp算最大的就行了 #inclu ...