Skip to content

Commit 8f82075

Browse files
committed
加入最大团。
1 parent 2ba902a commit 8f82075

File tree

1 file changed

+43
-0
lines changed

1 file changed

+43
-0
lines changed

src/1 Graph/Maximum Clique.cpp

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<TeX>最大团,复杂度上限大概是 $ O(n^{\frac{n}{3}}) $ 左右。G是邻接矩阵,$dp_i$表示由$i$到$n-1$的子图构成的最大团点数,剪枝用。adj中存放的是与i邻接且标号比i大的顶点。
2+
3+
顺便可以保证方案的字典序最小。对adj进行压位有非常良好的效果。</TeX>
4+
const int MAXN = 55;
5+
int G[MAXN][MAXN];
6+
7+
int ans = 0;
8+
int plan[MAXN]; int cur[MAXN]; int dp[MAXN];
9+
bool dfs(int* adj,int adjCnt,int curClique) {
10+
int nextAdj[MAXN];
11+
if(!adjCnt) {
12+
if(curClique > ans) {
13+
ans = curClique;
14+
memcpy(plan,cur,sizeof(plan[0])*ans);
15+
return true;
16+
}
17+
return false;
18+
}
19+
for(int i = 0;i < adjCnt;i++) {
20+
if(curClique + adjCnt - i < ans) return false;
21+
if(curClique + dp[adj[i]] < ans) return false;
22+
cur[curClique] = adj[i];
23+
int nextAdjCnt = 0;
24+
for(int j = i+1;j < adjCnt;j++)
25+
if(G[adj[i]][adj[j]]) nextAdj[nextAdjCnt++] = adj[j];
26+
if(dfs(nextAdj,nextAdjCnt,curClique+1)) return true;
27+
}
28+
return false;
29+
}
30+
31+
int maximumClique(int n) {
32+
ans = 0; memset(dp,0,sizeof(dp));
33+
34+
int adj[MAXN];
35+
for(int i = n-1;i >= 0;i--) {
36+
cur[0] = i;
37+
int adjCnt = 0;
38+
for(int j = i+1;j < n;j++) if(G[i][j]) adj[adjCnt++] = j;
39+
dfs(adj,adjCnt,1);
40+
dp[i] = ans;
41+
}
42+
return ans;
43+
}

0 commit comments

Comments
 (0)