HDU5952 Counting Cliques (暴力深搜+剪枝) (2016ACM/ICPC亚洲赛区沈阳站 Problem E)

时间:2022-08-04 11:36:58

题目链接:传送门

题目:

Counting Cliques
Time Limit: / MS (Java/Others) Memory Limit: / K (Java/Others)
Total Submission(s): Accepted Submission(s): 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 integers N,M and S (N ≤ ,M ≤ , ≤ S ≤ ), each of the following M lines contains integers u and v ( ≤ 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 . Output
For each test case, output the number of cliques with size S in the graph. Sample Input Sample Output

题目大意:

  给定一个有N个点,M条边的图,求有S个点的完全子图的数量。

  (N ≤ 100,M ≤ 1000,2 ≤ S ≤ 10)

思路:

  一个个点加入完全子图,大小达到S时ans++。

  纯暴力好像会在4000ms上下徘徊,随便剪一下就过了。

代码:

#include <bits/stdc++.h>

using namespace std;
const int MAX_N = 1e2 + ;
const int MAX_M = 1e3 + ; int N, M, S;
int ans;
bool edge[MAX_N][MAX_N];
vector <int> Edge[MAX_N];
int vis[MAX_N]; int read() {
int res = ;
char ch = getchar();
while (!isdigit(ch)) ch = getchar();
while (isdigit(ch)) {
res *= ;
res += ch - '';
ch = getchar();
}
return res;
} void dfs(int dep, int u)
{
if (Edge[u].size() < S-)
return;
if (S-dep + u > N)
return;
if (dep == S) {
ans++;
return;
} int Elen = Edge[u].size();
for (int i = ; i < Elen; i++) {
int v = Edge[u][i];
if (v < u)
continue;
if (Edge[v].size() < dep)
continue;
bool ok = true;
for (int j = ; j <= dep; j++){
int w = vis[j];
if (!edge[v][w]) {
ok = false;
break;
}
} if (ok) {
vis[dep+] = v;
dfs(dep+, v);
}
}
} void addEdge(int u, int v) {
edge[u][v] = edge[v][u] = true;
Edge[u].push_back(v);
Edge[v].push_back(u);
} int main()
{
int T;
cin >> T;
while (T--) {
N = read();
M = read();
S = read();
for (int i = ; i <= N; i++) {
Edge[i].clear();
for (int j = ; j <= N; j++)
edge[i][j] = false;
}
int u, v;
for (int i = ; i <= M; i++) {
u = read();
v = read();
addEdge(u, v);
}
ans = ;
for (int i = ; i <= N; i++) {
vis[] = i;
dfs(, i);
}
printf("%d\n", ans);
}
return ;
}