题解:CF1210F2 Marek and Matching (hard version)

· · 题解

CF1210F2

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

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

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

首先 (S^{\prime},T^{\prime}) 之间的方案数是 g_{S^{\prime},T^{\prime}},并且要强制钦定 N(S^{\prime})=T^{\prime},因此 S^{\prime}\to T\setminus T^{\prime} 之间不能有边,用 no_{S^{\prime},T\setminus T^{\prime}} 表示这个方案数。对于 S\setminus S^{\prime}\to T\setminus T^{\prime} 之间的连边,我们发现其一定要形成完美匹配,否则可以并上其中的某个不合法点集,这样 |N(S^{\prime})|-|S^{\prime}| 就变大了,不应在此处计数,因此我们还需要记 f_{S,T} 表示 S,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}}

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

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}}

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

:::info[代码]

#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;
}

:::