首页 > 代码库 > hdu1530 Maximum Clique,最大团 , DP,邻接矩阵
hdu1530 Maximum Clique,最大团 , DP,邻接矩阵
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.
#include <cstdio> #include <cstring> #include <algorithm> using namespace std; const int maxv = 60; int g[maxv][maxv], dp[maxv], stk[maxv][maxv], mx; int dfs(int n, int ns, int dep) { if (0 == ns) { if (dep > mx) mx = dep; return 1; } int i, j, k, p, cnt; for (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--) { // vertex: 0 ~ n-1 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() { int n; while(~scanf("%d", &n),n) { for(int i=0; i<n; ++i) for(int j=0; j<n; ++j) scanf("%d", &g[i][j]); int ans = clique(n); printf("%d\n", ans); } return 0; }
hdu1530 Maximum Clique,最大团 , DP,邻接矩阵
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。