跳到正文

题解

题解:CF1210F2 Marek and Matching (hard version)

AC_Lover约 7 分钟
原文
文章信息与版本

文章信息

收录
2025年12月12日
更新
2025年12月12日
篇幅
约 7 分钟
题目
CF1210F2

版本

  1. v2当前
  2. v1

CF1210F2

吐槽:NOIP模拟赛出了此题的加强版,其数据范围是 n≤9n\le 9,过于变态。

对于二分图完美(完备)匹配,套路地考虑Hall定理,令 N(S)={ v∣∃u∈S,(u,v)∈E }N(S)=\set{v\mid \exists u\in S,(u,v)\in E},则存在完美匹配等价于 ∀S,∣S∣≤∣N(S)∣\forall S,|S|\le |N(S)|。使得所有的 SS 都满足这个条件太难计数了,容斥一下,对存在 SS 满足 ∣N(S)∣>∣S∣|N(S)|>|S| 的图计数,考虑对每个这样的形式钦定 ∣N(S)∣−∣S∣|N(S)|-|S| 最大的集合中 ∣S∣|S| 最小的作为代表元,可以证明,每个不合法的图其代表元都是唯一的,那么考虑对代表元容斥。

令 gS,T (∣S∣>∣T∣)g_{S,T}\space (|S|>|T|) 表示考虑左部点集合为 SS,N(S)=TN(S)=T,其不存在完美匹配且 (S,T)(S,T) 是代表元的方案数,GS,TG_{S,T} 表示这个导出子图的匹配方案数,即 N(S)=TN(S)=T 的方案数。那么考虑容斥掉代表元不是 (S,T)(S,T),而是 S,TS,T 的子集 (S′,T′)(S^{\prime},T^{\prime}),其需要满足 ∣S′∣−∣T′∣≥∣S∣−∣T∣|S^{\prime}|-|T^{\prime}|\ge |S|-|T|,如图:

CF1210F2.png

首先 (S′,T′)(S^{\prime},T^{\prime}) 之间的方案数是 gS′,T′g_{S^{\prime},T^{\prime}},并且要强制钦定 N(S′)=T′N(S^{\prime})=T^{\prime},因此 S′→T∖T′S^{\prime}\to T\setminus T^{\prime} 之间不能有边,用 noS′,T∖T′no_{S^{\prime},T\setminus T^{\prime}} 表示这个方案数。对于 S∖S′→T∖T′S\setminus S^{\prime}\to T\setminus T^{\prime} 之间的连边,我们发现其一定要形成完美匹配,否则可以并上其中的某个不合法点集,这样 ∣N(S′)∣−∣S′∣|N(S^{\prime})|-|S^{\prime}| 就变大了,不应在此处计数,因此我们还需要记 fS,Tf_{S,T} 表示 S,TS,T 之前有完美匹配的方案数,于是不难得到转移:

gS,T=GS,T−∑S′⊆S,T′⊆T[∣S′∣−∣T′∣≥∣S∣−∣T∣]×gS′,T′×noS′,T∖T′×fS∖S′,T∖T′g_{S,T}=G_{S,T}-\sum_{S^{\prime}\subseteq S,T^{\prime}\subseteq T} [|S^{\prime}|-|T^{\prime}|\ge |S|-|T|]\times g_{S^{\prime},T^{\prime}}\times no_{S^{\prime},T\setminus T^{\prime}}\times f_{S\setminus S^{\prime},T\setminus T^{\prime}}

我们还需要求出 fS,T (∣S∣≤∣T∣)f_{S,T}\space (|S|\le |T|),可以枚举子集 (S′,T′)(S^{\prime},T^{\prime}) 表示其作为不合法位置且其作为代表元,即使用 gS′,T′g_{S^{\prime},T^{\prime}} 进行容斥:

fS,T=GS,T−∑S′⊆S,T′⊆TgS′,T′×noS′,T∖T′×fS∖S′,T∖T′f_{S,T}=G_{S,T}-\sum_{S^{\prime}\subseteq S,T^{\prime}\subseteq T} g_{S^{\prime},T^{\prime}}\times no_{S^{\prime},T\setminus T^{\prime}}\times f_{S\setminus S^{\prime},T\setminus T^{\prime}}

最终答案就是 fU,Uf_{U,U}。时间复杂度可以分 S,TS,T 计算量分别看,是 O(3n×3n)=O(9n)O(3^n\times 3^n)=O(9^n)。

代码
C++
#include <bits/stdc++.h>
#define popc(x) __builtin_popcount(x)

using namespace std;

typedef long long ll;
const int mod=1e9+7;
inline int Mod(int x) { return x<0 ? x+mod : (x>=mod ? x-mod : x); }
inline void adm(int &x,int y) { x=Mod(x+y); }
inline void dec(int &x,int y) { x=Mod(x-y); }
inline void pro(int &x,int y) { x=(ll)x*y%mod; }
inline int qmi(ll a,int b)
{
	ll res=1;
	for (;b;b>>=1,a=a*a%mod) if (b&1) res=res*a%mod;
	return res;
}
const int N=9;
int n;
int p[N][N];
int G[1<<N][1<<N],no[1<<N][1<<N];
int g[1<<N][1<<N],f[1<<N][1<<N];

int main()
{
	freopen("magician.in","r",stdin);
	freopen("magician.out","w",stdout);
	ios::sync_with_stdio(false);
	cin.tie(0),cout.tie(0);

	cin >> n;
	int inv=qmi(100,mod-2);
	for (int i=0;i<n;i++)
	{
		for (int j=0;j<n;j++)
		{
			cin >> p[i][j];
			pro(p[i][j],inv);
		}
	}

	for (int S=0;S<(1<<n);S++)
	{
		for (int T=0;T<(1<<n);T++)
		{
			G[S][T]=1;
			for (int y=0;y<n;y++)
			{
				if (!(T>>y&1)) continue; int pr=1;
				for (int x=0;x<n;x++) if (S>>x&1) pro(pr,Mod(1-p[x][y]));
				pro(G[S][T],Mod(1-pr));
			}
			no[S][T]=1;
			for (int x=0;x<n;x++) if (S>>x&1) for (int y=0;y<n;y++) if (T>>y&1) pro(no[S][T],Mod(1-p[x][y]));
		}
	}

	for (int S=0;S<(1<<n);S++)
	{
		for (int T=0;T<(1<<n);T++)
		{
			if (popc(S)<=popc(T))
			{
				f[S][T]=G[S][T];
				for (int s=S;;s=(s-1)&S)
				{
					for (int t=T;;t=(t-1)&T)
					{
						if (s || t)
							if (g[s][t] && no[s][T^t] && f[S^s][T^t])
								dec(f[S][T],(ll)g[s][t]*no[s][T^t]%mod*f[S^s][T^t]%mod);
						if (!t) break;
					}
					if (!s) break;
				}
			}
			else
			{
				g[S][T]=G[S][T];
				for (int s=S;;s=(s-1)&S)
				{
					for (int t=T;;t=(t-1)&T)
					{
						if (s!=S || t!=T)
							if (popc(s)-popc(t)>=popc(S)-popc(T))
								if (g[s][t] && no[s][T^t] && f[S^s][T^t])
									dec(g[S][T],(ll)g[s][t]*no[s][T^t]%mod*f[S^s][T^t]%mod);
						if (!t) break;
					}
					if (!s) break;
				}
			}
		}
	}
	cout << f[(1<<n)-1][(1<<n)-1] << "\n";

	return 0;
}
原文