Counting Cliques-vector矩陣同建圖
阿新 • • 發佈:2018-12-09
- 題意:
- 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.
- 思路:
- 構建mmp[][]矩陣以便於查詢各個點之間的關係。vector建圖減少查詢此點所能到達的點的時間 。
- dfs遞迴進行搜尋只要限制下一個進入集合的點比當前的點大就無需考慮搜尋重複。
-
#include <bits/stdc++.h> using namespace std; #define maxn 1100 #define ll long long ll t,n,m,ans,u,v,a[maxn]; ll mmp[maxn][maxn],sum; vector<int>gra[maxn]; bool check(int c,int s) { for(int i=1; i<=s; i++) if(mmp[c][a[i]]!=1) return false; return true; } void dfs(int c,int s) { if(s==ans) { sum++; return; } int len=gra[c].size(); for(int j=0; j<len; j++) { if(gra[c][j]<c)continue; if(check(gra[c][j],s)) { a[s+1]=gra[c][j]; dfs(gra[c][j],s+1); } } } int main() { scanf("%lld",&t); while(t--) { sum=0; scanf("%lld%lld%lld",&n,&m,&ans); for(int i=1; i<=n; i++) { gra[i].clear(); for(int j=1; j<=m; j++) mmp[i][j]=0; } while(m--) { scanf("%lld%lld",&u,&v); mmp[u][v]=mmp[v][u]=1; gra[u].push_back(v); gra[v].push_back(u); } for(int i=1; i<=n; i++) { a[1]=i; dfs(i,1); } printf("%lld\n",sum); } return 0; }