[ZJOI2016]小星星

[ZJOI2016]小星星

题目描述

luogu题面
给定一个n个点的树和n个点m条边的无向图,求将树嵌入图的方案数。
其中 n ≤ 17 , m ≤ n ∗ ( n − 1 ) 2 n \leq 17,m \leq \frac{n*(n-1)}{2} n17,m2n(n1)

Solution

点数很少,考虑状压DP。

f [ i ] [ j ] [ k ] f[i][j][k] f[i][j][k]表示以 i i i结点为根的子树, i i i号结点对应图上的 j j j号结点, k k k集合中的结点已经被使用了。

转移就是枚举 i i i的儿子 s o n son son,枚举儿子对应的结点为 t t t,枚举 k k k的子集 s s s f [ i ] [ j ] [ k ] = ∑ s o n , t , s f [ s o n ] [ t ] [ s ] ∗ f [ i ] [ j ] [ k − s ] f[i][j][k]=\sum_{son,t,s} f[son][t][s]*f[i][j][k-s] f[i][j][k]=son,t,sf[son][t][s]f[i][j][ks]

时间复杂度为 O ( 3 n n b a l a b a l a . . . ) O(3^nn^{balabala...}) O(3nnbalabala...),TLE。

考虑如何优化DP中枚举集合的状态。
有一种巧妙的容斥方法。

我们思考哪一些方案是不合法的,显然倘若选出的若干树上的结点对应了相同的图上结点,这一种方案不合法。

我们不在DP中枚举集合,而是让他任意选择,允许重复选同一个图上节点。
枚举可用的图上结点集合s,用 f [ i ] [ j ] f[i][j] f[i][j]表示以 i i i结点为根的子树, i i i号结点对应图上的 j j j号结点,只能选择图上s集合包含的结点的方案数。

此时的最终答案 A n s = ∑ s ∑ j f [ 1 ] [ j ] ( − 1 ) p o p c o u n t ( 2 n − 1 − s ) Ans=\sum_{s} \sum_{j} f[1][j](-1)^{popcount(2^n-1-s)} Ans=sjf[1][j](1)popcount(2n1s)
时间复杂度 O ( 2 n n 3 ) O(2^nn^3) O(2nn3) l u o g u      O 2 luogu\;\;O2 luoguO2可过。

#include <vector>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <queue>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <cctype>
#include <string>
#include <cstring>
#include <ctime>
#include <cassert>
#include <string.h>
//#include <unordered_set>
//#include <unordered_map>
//#include <bits/stdc++.h>

#define MP(A,B) make_pair(A,B)
#define PB(A) push_back(A)
#define SIZE(A) ((int)A.size())
#define LEN(A) ((int)A.length())
#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define fi first
#define se second

using namespace std;

template<typename T>inline bool upmin(T &x,T y) { return y<x?x=y,1:0; }
template<typename T>inline bool upmax(T &x,T y) { return x<y?x=y,1:0; }

typedef long long ll;
typedef unsigned long long ull;
typedef long double lod;
typedef pair<int,int> PR;
typedef vector<int> VI;

const lod eps=1e-11;
const lod pi=acos(-1);
const int oo=1<<30;
const ll loo=1ll<<62;
const int mods=998244353;
const int MAXN=600005;
const int INF=0x3f3f3f3f;//1061109567
/*--------------------------------------------------------------------*/
inline int read()
{
	int f=1,x=0; char c=getchar();
	while (c<'0'||c>'9') { if (c=='-') f=-1; c=getchar(); }
	while (c>='0'&&c<='9') { x=(x<<3)+(x<<1)+(c^48); c=getchar(); }
	return x*f;
}
vector<int> e[20];
ll f[20][20];
int pick[20],flag[20][20],n,m;
void tree_dp(int x,int father)
{
	for (auto v:e[x])
	{
		if (v==father) continue;
		tree_dp(v,x);
	}
	for (int i=1;i<=n;i++) f[x][i]=pick[i];
	for (auto v:e[x])
	{
		if (v==father) continue;
		for (int j=1;j<=n;j++)
		if (pick[j])
		{
			ll sum=0;
			for (int k=1;k<=n;k++)
				if (flag[j][k]&&pick[k]) sum+=f[v][k];
			f[x][j]*=sum;
		}
	}
}
int main()
{
	n=read(),m=read();
	for (int i=1;i<=m;i++)
	{
		int u=read(),v=read();
		flag[u][v]=flag[v][u]=1;
	}
	for (int i=1;i<n;i++)
	{
		int u=read(),v=read();
		e[u].PB(v),e[v].PB(u);
	}
	ll ans=0;
	for (int i=1;i<1<<n;i++)
	{
		int opt=((n-__builtin_popcount(i))&1)?-1:1;
		for (int j=1;j<=n;j++) pick[j]=(i>>(j-1))&1;
		tree_dp(1,0);
		for (int j=1;j<=n;j++) ans+=f[1][j]*opt;
	}
	printf("%lld\n",ans);
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值