题目描述
求无向图的所有导出子图的最大独立集大小的和.
导出子图:若图 G ′ G' G′ 是图 G G G 的导出子图,则
-
图 G ′ G' G′ 的点集是图 G G G 的点集的子集;
-
G ′ G' G′ 中存在边 ( a , b ) (a,b) (a,b),当且仅当点 a a a, b b b 在 G ′ G' G′ 中,且 G G G 中存在边 ( a , b ) (a,b) (a,b).
独立集:图中两两互不相邻的顶点构成的集合.
最大独立集:点数最大的独立集.
数据范围
对 100 % 100\% 100% 的数据, 2 ≤ n ≤ 26 2\le n\le 26 2≤n≤26, m ≤ n × ( n − 1 ) 2 m\le \frac{n\times (n-1)}{2} m≤2n×(n−1).
题解
考虑新加入一个节点对原来的图的影响
所以设 f s f_s fs 表示选择了 s s s 状态的点的最大独立集点数
假设新加入一个节点 i i i ,那对于原来的图的状态 j ∈ [ 0 , 2 i − 1 ] j∈[0,2^i-1] j∈[0,2i−1] ,可以列出 d p dp dp式子:
f j ∣ ( 2 i ) = m a x ( f j , f j & ( ( 2 n − 1 ) ⊕ a i ) + 1 ) f_{j|(2^i)}=max(f_j,f_{j \& ((2^n-1) \oplus a_i)}+1) fj∣(2i)=max(fj,fj&((2n−1)⊕ai)+1)
其中 a i a_i ai 为与 i i i 相连的点的状态
代码
#include <bits/stdc++.h>
using namespace std;
int n,m,a[27],f[1<<26],ans;
int main(){
scanf("%d%d",&n,&m);
for (int u,v,i=0;i<m;i++)
scanf("%d%d",&u,&v),
a[v]|=(1<<u),a[u]|=(1<<v);
for (int s=1,i=0;i<n;i++,s<<=1)
for (int j=0;j<s;j++)
f[j|s]=max(f[j],f[j&(~a[i])]+1);
for (int i=(1<<n)-1;~i;i--) ans+=f[i];
return printf("%d\n",ans),0;
}