Counting Cliques

来源:互联网 发布:2017中甲数据 编辑:程序博客网 时间:2024/05/29 10:12
Problem Description
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. 
 

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

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

Sample Input
34 3 21 22 33 45 9 31 31 41 52 32 42 53 43 54 56 15 41 21 31 41 51 62 32 42 52 63 43 53 64 54 65 6


题意:给定n个顶点,m条边,求顶点数为S的完全图,并且每个点的度为最大为20,就是说这个点最多连接20条边,完全图的重要特性:边数 = S*(S-1)/2条。

思路:直接暴力搜索,这里很容易想到就是直接去搜所有的点,找到满足S大小的图,这里肯定会TLE,因为每次就在n里面找,这题的巧妙之处就在,这个度为20,说明这是一个稀疏图,那么我们就用vector存图了,这样我们每次搜索的时候就最多只搜20个点而不是n个点,复杂度减低了不少,再用一个二维数组来显示边的信息(有还是没有),而不是用朴素的数组来存图(这种适合于稠密图)这种搜索的时候相当于在多有的点中搜索;

哈哈,先附一份朴素的TLE 代码:

#include <iostream>#include <cstdio>#include <cmath>#include <cstring>#include <algorithm>#include <queue>#include <vector>#include <set>#include <map>using namespace std;const int MAX_N = 105;int gra[MAX_N][MAX_N];int n,m,s;int cnt;int v[15];void dfs(int ct,int init) {    if(ct == s) {        cnt++;        return;    }    if(ct+n+1-init < s) {        return;    }    for(int i = init; i <= n; i++) {//这里是直接用了n个点去搜,怎么可能不TLE呢!!        v[ct] = i;        int flag = 0;        for(int j = 0 ; j < ct; j++) {            if(!gra[v[j]][i]) {                flag =1;                break;            }        }        if(!flag) {            v[ct] = i;            dfs(ct+1,i+1);        }    }}int main() {    int cas;    scanf("%d",&cas);    while(cas--) {        memset(gra,0,sizeof(gra));        scanf("%d%d%d",&n,&m,&s);        int a,b;        for(int i = 0; i < m; i++) {            scanf("%d%d",&a,&b);            gra[b][a] = 1;            gra[a][b] = 1;        }        cnt = 0;        dfs(0,1);        printf("%d\n",cnt);    }    return 0;}

很朴素吧,时间复杂度相当的高,但是用稀疏图来写果断AC代码:

#include <iostream>#include <cstdio>#include <cmath>#include <cstring>#include <algorithm>#include <queue>#include <vector>#include <set>#include <map>using namespace std;const int MAX_N = 105;vector<int>G[MAX_N];bool used[MAX_N][MAX_N];int n,m,s;int cnt;int v[12];void dfs(int ct,int init) {    if(ct == s) {        cnt++;        return;    }    if(ct+n+1-init < s) {        return;    }    for(int i = 0; i < G[init].size(); i++) {//这里只搜和这个点相连的点吗,是不是用到了最大的度为20,是不是减少很多时间        int flag = 0;        int index = G[init][i];        for(int j = 0 ; j < ct; j++) {            if(!used[v[j]][index]) {                flag = 1;                break;            }        }        if(!flag) {            v[ct] = index;            dfs(ct+1,index);        }    }}int main() {    int cas;    scanf("%d",&cas);    while(cas--) {        memset(used,0,sizeof(used));        scanf("%d%d%d",&n,&m,&s);        int a,b;        for(int i = 0; i < m; i++) {            scanf("%d%d",&a,&b);            if(a > b) {                swap(a,b);            }            G[a].push_back(b);            used[a][b] = 1;            used[b][a] = 1;        }        cnt = 0;        for(int i = 1; i <= n; i++) {            v[0] = i;            dfs(1,i);        }        printf("%d\n",cnt);        for(int i = 0; i <= n; i++) {            G[i].clear();        }    }    return 0;}

ps:收获不小,什么稀疏图,稠密图。借学长的话说:数据结构都是拿来用的,有时候要考虑时间上谁合适,有时候要考虑空间上谁合适。


0 0