题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1530
Given a graph G(V, E), a clique is a sub-graph g(v, e), so that for all vertex pairs v1, v2 in v, there exists an edge (v1, v2) in e. Maximum clique is the clique that has maximum number of vertex.
Input
Input contains multiple tests. For each test:
The first line has one integer n, the number of vertex. (1 < n <= 50)
The following n lines has n 0 or 1 each, indicating whether an edge exists between i (line number) and j (column number).
A test with n = 0 signals the end of input. This test should not be processed.
Output
One number for each test, the number of vertex in maximum clique.
Sample Input
5 0 1 1 0 1 1 0 1 1 1 1 1 0 1 1 0 1 1 0 1 1 1 1 1 0 0
Sample Output
4
给出一个n*n的矩阵,如果点是1,则i->j有边存在,求最大团数量。
裸了一个模板过的。
#pragma GCC optimize(2) #include<stdio.h> #include<algorithm> #include<string.h> #include<set> #include<vector> #include<queue> using namespace std; const int maxn = 500; const int inf = 0x3f3f3f3f; typedef long long ll; int g[maxn][maxn], stk[maxn][maxn]; int dp[maxn], mx; int n; int dfs(int n, int ns, int dep) { if (ns == 0) { if (dep > mx) { mx = dep; } return 1; } int i, j, k, p, cnt; for (int i = 0; i < ns; i++) { k = stk[dep][i]; cnt = 0; if (dep + n - k <= mx) { return 0; } if (dep + dp[k] <= mx) { return 0; } for (j = i + 1; j < ns; j++) { p = stk[dep][j]; if (g[k][p]) { stk[dep + 1][cnt++] = p; } } dfs(n, cnt, dep + 1); } return 1; } int clique(int n) { int i, j, ns; for (mx = 0, i = n - 1; i >= 0; i--) { for (ns = 0, j = i + 1; j < n; j++) { if (g[i][j]) { stk[1][ns++] = j; } } dfs(n, ns, 1); dp[i] = mx; } return mx; } int main() { //freopen("C://input.txt", "r", stdin); while (scanf("%d", &n) != EOF) { if (n == 0) { break; } for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { scanf("%d", &g[i][j]); } } int res = clique(n); printf("%d\n", res); } return 0; }
hdu 1530 Maximum Clique(最大团基础题)
最新推荐文章于 2021-05-29 18:49:16 发布