如题= =自用模板

补图的最大独立集为原图的最大团 反之原图的最大独立集为补图的最大团
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
import java.io.*;
import java.util.*;
public class Main {

public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
PrintWriter pw=new PrintWriter(System.out);
MaxClique mc=new MaxClique();
while(sc.hasNextInt()){
int n=sc.nextInt();
if(n==0)break;
for(int i=0;i<n;i++){
for(int j=0;j<n;j++){
int k=sc.nextInt();
if(k>0)mc.G[i][j]=true;
else mc.G[i][j]=false;
}
}
mc.calMaxClique(n);
pw.println(mc.ans);
//for(int i=0;i<mc.ans;i++)pw.print(mc.path[i]+" ");
pw.flush();
}
}

}
class MaxClique{
boolean[][]G; //存图下标为0~n-1
int ans;
int maxn=60;
int[]dp;
boolean[]use;
int[]pre;
int[]path;
int[]a,id;
MaxClique(){
G=new boolean[maxn][maxn];
dp=new int[maxn];
//pre=new int[maxn];
//path=new int[maxn];
a=new int[maxn];
id=new int[maxn];
}
void calMaxClique(int n){ //下标从0~n-1
ans=0;
for(int i=n-1;i>=0;i--){
int top=0;
for(int j=i+1;j<n;j++){
if(G[i][j])id[top++]=j;
}
//pre[0]=i;
dfs(id,top,1);
dp[i]=ans;
}
}
boolean dfs(int[]id,int top,int cnt){
if(top==0){
if(cnt>ans){
ans=cnt;
//for(int i=0;i<cnt;i++)path[i]=pre[i];
return true;
}
return false;
}
a=new int[maxn];
for(int i=0;i<top;i++){
if(cnt+top-i<=ans)return false;
if(cnt+dp[id[i]]<=ans)return false;
//pre[cnt] = id[i];
int k = 0;
for(int j=i+1;j<top;j++)
if(G[id[i]][id[j]])
a[k++] = id[j];
if(dfs(a,k,cnt+1))return true;
}
return false;
}

}