#4507. Independent set 1

题目描述

求无向图的所有导出子图的最大独立集大小的和.

导出子图:若图 G G' 是图 G G 的导出子图,则

  • G G' 的点集是图 G G 的点集的子集;

  • G G' 中存在边 ( a , b ) (a,b) ,当且仅当点 a a , b b G G' 中,且 G G 中存在边 ( a , b ) (a,b) .

独立集:图中两两互不相邻的顶点构成的集合.

最大独立集:点数最大的独立集.

数据范围

100 % 100\% 的数据, 2 n 26 2\le n\le 26 , m n × ( n 1 ) 2 m\le \frac{n\times (n-1)}{2} .

题解

考虑新加入一个节点对原来的图的影响

所以设 f s f_s 表示选择了 s s 状态的点的最大独立集点数

假设新加入一个节点 i i ,那对于原来的图的状态 j [ 0 , 2 i 1 ] j∈[0,2^i-1] ,可以列出 d p 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)

其中 a i a_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;
}
发布了107 篇原创文章 · 获赞 5 · 访问量 8017

猜你喜欢

转载自blog.csdn.net/Johnny817/article/details/99697319
今日推荐