其他分享
首页 > 其他分享> > 「ACM Shenyang Onsite 2016」E.Counting Cliques(dfs暴力+优化)

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

作者:互联网

描述

传送门:我是传送门

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