跳到正文

题解

题解:P6846 [CEOI2019] Amusement Park

Richard_Whr约 3 分钟
原文
文章信息与版本

文章信息

发布
2025年1月6日
收录
2025年12月12日
更新
2026年11月4日
篇幅
约 3 分钟
获赞
4
题目
P6846

版本

  1. v1当前

为啥都用到了什么 DAG 反向还是 DAG,因此平均每两次花费 mm 这个神秘结论啊。

直接做他不好吗?

设 fsf_s 表示集合 ss 中的点构成 DAG 的方案数,gsg_s 表示 ss 中的点的答案。

考虑钦定入度为 00 的集合 tt,集合反演一下你会发现容斥系数是 opt=(−1)∣t∣+1op_t=(-1)^{|t|+1}。

写出转移方程:

fs=∑t⊆sopt×ftf_s=\sum\limits_{t \subseteq s}op_t \times f_t gs=∑t⊆sopt×(ft×E(s−t,t)+gt)g_s=\sum\limits_{t \subseteq s}op_t \times (f_t \times E(s-t,t)+g_t)

其中 E(A,B)E(A,B) 表示集合 AA 连向集合 BB 的边的数量。

这个稍微上点手法就能在 O(3n)O(3^n) 的复杂度内求出来了。

因此整体复杂度 O(3n)O(3^n)。

C++
#include<bits/stdc++.h>
#define int long long
using namespace std;
const int N=18,mod=998244353;
int In[N],Out[N];
int val[1<<N];
int f[1<<N],g[1<<N];
bool st[1<<N];
int n,m;

signed main()
{
	cin>>n>>m;
	for(int i=1,a,b;i<=m;i++)
	{
		cin>>a>>b;
		a--,b--;
		Out[a]|=(1<<b);
		In[b]|=(1<<a);
	}
	
	for(int s=1;s<(1<<n);s++)
	{
		int cnt=0;
		for(int i=0;i<n;i++)
		{
			if((s>>i)&1) cnt+=(In[i]&s),cnt+=(Out[i]&s);
		}	
		if(cnt) st[s]=false;
		else st[s]=true;
	}	
	
	f[0]=1,g[0]=0;
	for(int s=1;s<(1<<n);s++)
	{
		for(int t=s;t;t=(t-1)&s)
		{
			if(t==s) val[t]=0;
			else
			{
				int j=__builtin_ctz(s^t);
				int s1=s^t^(1<<j);
				val[t]=val[t|(1<<j)]-__builtin_popcount(In[j]&s1)+__builtin_popcount(Out[j]&t);	
			}
			if(!st[t]) continue;
			int op=(__builtin_popcount(t)&1)?1:-1;
			f[s]=(f[s]+op*f[s^t])%mod;
			g[s]=(g[s]+op*(f[s^t]*val[t]%mod+g[s^t]))%mod;
		}
	}
	
	cout<<(g[(1<<n)-1]+mod)%mod<<"\n";
	
	return 0; 
}
原文
存档评论3 条抓取于