ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

「ACM Shenyang Onsite 2016」E.Counting Cliques(dfs暴力+优化)

2021-01-20 21:35:10  阅读:210  来源: 互联网

标签:Onsite const Cliques int graph 31 MAXN Counting define


描述

传送门:我是传送门

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. 

输入

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. 

输出

For each test case, output the number of cliques with size S in the graph.

样例

输入

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

输出

3
7
15

思路

直接暴力搜索能求得正确答案,但是不优化的话肯定会超时。比赛结束我也没能优化出来…..

赛后看了看EaShion1994的题解才过。最主要的地方还是在其中的2、5两点我没能想到。优化过后便AC了,用了1400+ms

1.限定按照顶点序号上升dfs(完全图性质);

2.存储边从数组转换为vector(题目说顶点的度最多为20,而顶点数最多为100);

3.剪枝N-pre < S-deep,当剩下的点不足以构成定点数为S的完全图;(没太大用)

4.for循环初始化改为memset;(挣扎…)

5.path数组由bool改为int,因为每次递归前都要判断当前点是否和所有已经在路径里面点有边相连,所以要挨个去判断,如果path[i]表示点i是否在路径中,每次都需要执行一个100,如果将path[i]更改为路径中第i个点,每次最多执行10;(关键)

6.删掉了之前用来表示点是否加入路径的数组,因为顶点是严格升序加入路径的,所以这一步判断没必要。

代码

  1 /*
  2  * =============================================
  3  *
  4  *       Filename:  hdu5952.cpp
  5  *
  6  *           Link:  http://acm.hdu.edu.cn/showproblem.php?pid=5952
  7  *
  8  *        Version:  1.0
  9  *        Created:  2018/10/05 18时29分42秒
 10  *       Revision:  none
 11  *       Compiler:  g++
 12  *
 13  *         Author:  杜宁元 (https://duny31030.top/), duny31030@126.com
 14  *   Organization:  QLU_浪在ACM
 15  *
 16  * =============================================
 17  */
 18 #include <bits/stdc++.h>
 19 using namespace std;
 20 #define clr(a, x) memset(a, x, sizeof(a))
 21 #define rep(i,a,n) for(int i=a;i<=n;i++)
 22 #define pre(i,a,n) for(int i=n;i>=a;i--)
 23 #define ll long long
 24 #define max3(a,b,c) fmax(a,fmax(b,c))
 25 #define ios ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
 26 const double eps = 1e-6;
 27 const int INF = 0x3f3f3f3f;
 28 const int mod = 1e9 + 7;
 29 const int MAXN = 105;
 30 vector<int> edge[MAXN];
 31 bool m[MAXN][MAXN];
 32 int T,N,M,S,u,v,p;
 33 int flag;
 34 int tot = 1;
 35 int path[MAXN];
 36 bool check(int now,int step)
 37 {
 38     for(int i = 1;i <= step;i++)
 39     {
 40             if(!m[path[i]][now])
 41                 return false;
 42     }
 43     return true;
 44 }
 45 
 46 void dfs(int st,int step)
 47 {
 48     if(step == S)
 49     {
 50         p++;
 51         return ;
 52     }
 53     if(N-st < S-step)
 54         return ;
 55     int len = edge[st].size();
 56     for(int j = 0;j < len;j++)
 57     {
 58         if(check(edge[st][j],step))
 59         {
 60             path[tot++] = edge[st][j];
 61             dfs(edge[st][j],step+1);
 62             --tot;
 63         }
 64     }
 65 }
 66 
 67 int main()
 68 {
 69     ios
 70 #ifdef ONLINE_JUDGE 
 71 #else 
 72         freopen("in.txt","r",stdin);
 73     // freopen("out.txt","w",stdout); 
 74 #endif
 75     scanf("%d",&T);
 76     while(T--)
 77     {
 78         for(int i = 1;i <= N;i++)
 79             edge[i].clear();
 80         memset(m,0,sizeof m);
 81         scanf("%d %d %d",&N,&M,&S);
 82         for(int i = 1;i <= M;i++)
 83         {
 84             scanf("%d %d",&u,&v);
 85             if(u > v)
 86                 swap(u,v);
 87             edge[u].push_back(v);
 88             m[u][v] = 1;
 89             m[v][u] = 1;
 90         }
 91         p = 0;
 92         int tmp = N-S+1;
 93         for(flag = 1;flag <= tmp;++flag)
 94         {
 95             path[tot++] = flag;
 96             dfs(flag,1);
 97             --tot;
 98         }
 99         printf("%d\n",p);
100     }
101     fclose(stdin);
102     // fclose(stdout);
103     return 0;
104 }

 

标签:Onsite,const,Cliques,int,graph,31,MAXN,Counting,define
来源: https://www.cnblogs.com/duny31030/p/14305159.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有