题解:P13804 [SWERC 2023] In-order

· · 题解

题意

给定二叉树的先序遍历和后序遍历,以及中序遍历的一个区间,求合法二叉树个数。

如果 c_i = 0

我们先考虑一下如果中序遍历没有任何限制怎么去做。

考虑用先序和后序先搞一棵树出来,我们可以胡出来一个递归建树的方式:

乍一看这种建树方式是唯一的,我们似乎把树的形态定死了啊,为什么还会有很多方案?

然后你就会看到那些只有一个儿子的节点,因为这棵二叉树是区分了左右儿子的,所以在这里会有不同方案。

如果中序遍历没有限制的话,答案就是 2^{cnt},cnt 为只有一个儿子的节点个数。

如果只有一个 c_i \ne 0

我们关心那些只有一个儿子的节点,不妨将其先全部放到左儿子上,先处理出一个当前中序遍历,然后考虑如何通过调整一些左儿子到右儿子上,使其到目标位置。

我们手玩一下,发现对于那些不在祖先链上的节点,其翻转与否不会影响到这个目标点的中序遍历,原因很简单,dfs 时走进一个子树就会将所有子树内节点遍历完再出来,这些点不管被不被翻转,要么都会先遍历到,要么都不会先遍历到,所以这些点是任意选择的。

考虑在祖先链上的只有一个儿子的节点,我们翻转了它,相当于后序遍历的编号加一,因为它只有一个儿子,就是目标点所在的子树,翻转只会让这个点被提前遍历。

所以我们设当前中序遍历中目标点的编号与目标中序遍历中的编号差值为 dlt,祖先链上的一个儿子的点有 tot 个,总共有 cnt 个,则答案为 \binom{tot}{dlt} 2^{cnt-tot}。

注意有一个情况需要特判,如果这个目标点只有一个儿子,我们需要枚举它翻不翻转,因为它的儿子放左儿子就会提前遍历,放右儿子就不会提前遍历。

如果有多于一个 c_i \ne 0

延续刚才的思路,由于保证了有解,所以我们找到所有目标点,则它们的中序遍历一定得挨在一块,并且这些目标点的 LCA 一定也是目标点,不然就不能挨到一块了嘛,非常好理解。

进一步地,我们发现所有目标点到这个 LCA 的路径上的点,方案都已经固定了,也就是方案树唯一,具体可以构造性证明一下,这里不多赘述。

然后我们仍然考虑 LCA 的祖先链上的点,将 LCA 的编号归位,考虑方案数和上一个情况是一模一样的,仍然是一个组合数的形式,然后考虑不在任何一个目标点的祖先链上的点,仍然不会产生影响,是一个 2 的幂次的形式。

式子和上面的情况一样,这里就懒得写了。

Code

#include <bits/stdc++.h>
#define ll long long
#define ull unsigned long long
using namespace std;
namespace Yukina{
    #define getchar() (p1==p2&&(p2=(p1=buf)+fread(buf,1,1<<20,stdin),p1==p2)?EOF:*p1++)
    char buf[1<<20],*p1=buf,*p2=buf;
    inline int read(){
        int ans=0,w=1;
        char ch=getchar();
        while(ch<48||ch>57){
           if(ch=='-')w=-1;
           ch=getchar();
        }
        while(ch>=48&&ch<=57){
           ans=(ans<<1)+(ans<<3)+ch-48;
           ch=getchar();
        }
        return w*ans;
    }
    const int mod=999999937; 
    ll Pow(ll x,int y){
        ll ans=1,push=x;
        while(y){
            if(y&1)ans=ans*push%mod;
            push=push*push%mod;
            y>>=1;
        }
        return ans;
    }
    ll P[2000005],inv[2000005];
    ll C(int x,int y){
        if(y<0||y>x)return 0;
        return P[x]*inv[x-y]%mod*inv[y]%mod;
    }
    int n;
    int a[2000005],b[2000005],c[2000005];
    int id[2000005];
    int lson[2000005],rson[2000005],fa[2000005],sz[2000005];
    int dfn[2000005],tim;
    void build(int al,int ar,int bl,int br){
        int rt=a[al];
        dfn[rt]=++tim;
        sz[rt]=ar-al+1;
        if(al==ar)return;
        int ls=a[al+1];
        int pos=id[ls];
        lson[rt]=ls;
        fa[ls]=rt;
        build(al+1,al+(pos-bl+1),bl,pos);
        if(pos!=br-1){
            int rs=a[al+(pos-bl+2)];
            rson[rt]=rs;
            fa[rs]=rt;
            build(al+(pos-bl+2),ar,pos+1,br-1);
        }
    }   
    int tmp[2000005],top;
    void get(int x){
        if(!x)return ;
        get(lson[x]);
        tmp[++top]=x;
        get(rson[x]);
    }
    bool check_A(){
        for(int i=1;i<=n;i++)if(c[i])return 0;
        return 1;
    }
    bool check_B(){
        int cnt=0;
        for(int i=1;i<=n;i++)if(c[i])++cnt;
        return (cnt==1);
    }

    bool bj[2000005];
    int sum[2000005];
    ll P2[2000005];
    void get_sum(int x){
        if(!x)return;
        get_sum(lson[x]),get_sum(rson[x]);
        bj[x]|=bj[lson[x]]|bj[rson[x]];
        sum[x]=sum[lson[x]]+sum[rson[x]]+(lson[x]&&!rson[x]);
    }

    signed main(){
        n=read();
        P2[0]=1;
        for(int i=1;i<=n;i++)P2[i]=P2[i-1]*2%mod;
        P[0]=1;
        for(int i=1;i<=n;i++)P[i]=P[i-1]*i%mod;
        inv[n]=Pow(P[n],mod-2);
        for(int i=n-1;i>=0;i--)inv[i]=inv[i+1]*(i+1)%mod;
        for(int i=1;i<=n;i++)a[i]=read();
        for(int i=1;i<=n;i++)b[i]=read(),id[b[i]]=i;
        for(int i=1;i<=n;i++)c[i]=read();
        build(1,n,1,n);
        int Root=a[1];

        if(check_A()){//全 0
            int cnt=0;
            for(int i=1;i<=n;i++)if(lson[i]&&!rson[i])++cnt;
            ll ans=1;
            while(cnt--)ans=ans*2%mod;
            cout<<ans<<'\n';
            return 0;
        }

        if(check_B()){//一个非 0
            int id=0,k=0;
            for(int i=1;i<=n;i++)if(c[i])id=c[i],k=i;
            int y=0;
            get(Root);
            for(int i=1;i<=top;i++)if(tmp[i]==id)y=i;
            int cnt=0;
            for(int i=1;i<=n;i++)if(lson[i]&&!rson[i])++cnt;
            int now=id;
            int tot=0;
            while(fa[now])now=fa[now],tot+=(lson[now]&&!rson[now]);
            ll ans=0;
            if(lson[id]&&!rson[id])--cnt;//如果下面的可以交换,我们先减去,一会分讨即可 
            ans=(ans+P2[cnt-tot]*C(tot,k-y)%mod)%mod;
            if(lson[id]&&!rson[id]){//交换了过后,下面的中序全部到后面去了 
                y-=sz[id]-1;
                ans=(ans+P2[cnt-tot]*C(tot,k-y)%mod)%mod;
            }
            cout<<ans<<'\n';
            return 0;
        }

        int now=0;
        for(int i=1;i<=n;i++){
            if(c[i]){
                bj[c[i]]=1;
                if(!now)now=c[i];
                else{
                    while(dfn[c[i]]<dfn[now]||dfn[c[i]]>=dfn[now]+sz[now])now=fa[now];
                }
            }
        }
        int L=0;
        for(int i=1;i<=n;i++)if(c[i]){
            L=c[i];
            break;
        }
        if(now==L&&lson[now]&&!rson[now])swap(lson[now],rson[now]);
        get_sum(Root);

        ll ans=1;
        for(int i=1;i<=n;i++){
            if(!bj[i]&&bj[fa[i]])ans=ans*P2[sum[i]]%mod;
        }
        int k=0;
        for(int i=1;i<=n;i++)if(c[i]==now)k=i;
        int y=0;
        get(Root);
        for(int i=1;i<=top;i++)if(tmp[i]==now)y=i;
        int tot=0;
        while(now)now=fa[now],tot+=(lson[now]&&!rson[now]);
        ans=ans*C(tot,k-y)%mod;
        cout<<ans<<'\n';
        return 0;
    }
}
signed main(){
    Yukina::main();
    return 0;
}