题目链接: Counting Cliques
题意:一个有N个点M条边的图,球其中由S个点构成的团的个数。一个团是一个完全子图。
题解:拿到这题想了好久。。没想到dfs就完事了。就dfs一下,回溯一下就ok了,铜牌题。
#include <cstdio> #include <iostream> #include <algorithm> #include <cstring> #include <vector> #include <queue> #include <stack> #include <set> #include <map> #include <math.h> using namespace std; #define LL long long #define fi first #define se second #define pb(a) push_back(a) const int MAX_N = 1e2+7; const LL mod = 1e9+7; const int inf = 0x3f3f3f3f; int N,M,T,S; int ans; vector<int> vec[MAX_N]; int mp[MAX_N][MAX_N]; void dfs(int pos,int *sta ,int num){if(num == S){ans ++;return ;}for(int i=0;i<vec[pos].size();i++){int t = vec[pos][i];bool f = true;for(int j=0;j<num;j++){if(mp[t][sta[j]] == 0){f = false;break;}}if(f){sta[num ++] = t;dfs(t,sta,num);sta[num-1] = 0;num --;}} } int main() {cin>>T;while(T--){ans = 0;for(int i=0;i<MAX_N;i++){vec[i].clear();}memset(mp,0,sizeof(mp));scanf("%d%d%d",&N,&M,&S);for(int i=0;i<M;i++){int a,b;scanf("%d%d",&a,&b);if(a > b) swap(a,b);vec[a].push_back(b);mp[a][b] = mp[b][a] = 1;}for(int i=1;i<=N;i++){int sta[MAX_N];sta[0] = i;dfs(i,sta,1);}printf("%d\n",ans);}return 0; }