题解:P5841 [CTSC2011] 字符串重排

· · 题解

感觉不是很难,评黑是因为代码细节多而已。

先考虑求 W_{max}。

受 70\% 数据启发,令每个字符串末尾都加入字符 char(96),由于没有相同串,这样做不会影响 \operatorname{lcp},也可以保证串之间没有包含。此时,每个串与字典树的叶子一一对应。

注意到,当且仅当排列 P 对应字典树的某个 dfs 序中的叶结点子序列,W(P)=W_{max}。

:::info[证明] 使用归纳法。

对于点 u,它有 cson_u 个儿子,假设以它为根的子树在排列中连续,那么 dep_u(即这段 \operatorname{lcp})贡献 cson_u-1 次最优,因为次数不可能更少,如果更多必然可以调整为更优的以它的后代为 \operatorname{lcp}。

所以,它的各个儿子的子树也是在 P 中连续,即 P 对应 dfs 序。

现在证明了充分性“如果 W 最大,那么 P 对应 dfs 序”,必要性同理。

再考虑第二问,要满足最大 W 的情况下尽可能地满足更往后的条件。

在字典树上看,每个条件等价于:钦定一对兄弟结点 dfn 连续,并令它们分别取特定的一个儿子,到各自的特定后代的链上的结点为该结点所有兄弟中第一个或最后一个访问。

对于每个结点,对它所有儿子维护链表,每个条件的两个结点慢慢往上跳。这样复杂度有点高,但是特判相同的 (X,Y) 后,每个结点只能被钦定一次作为链表头、一次作为链表尾,所以加入这条剪枝即可。

同时代码有很多细节,比如:链表不能成环、不能一条链表同时被钦定表头和表尾并且还留有其他兄弟没位置。

巨丑代码。

#include <bits/stdc++.h>
#define fi first
#define se second
#define mid ((l+r)>>1)
#define bmid ((l+r+1)>>1)
#define pb push_back
#define eb emplace_back
#define mp make_pair
#define fswap(a,b) ((a)^=(b)^=(a)^=(b))
using namespace std;
using ll= long long;
#ifndef ONLINE_JUDGE
template <typename tp>
void _debug(const tp& t) {cerr<<t<<'\n';}
template <typename tp,typename... args>
void _debug(const tp& t, const args&... rest) {cerr<<t<<' ';_debug(rest...);}
#define debug(...) _debug(#__VA_ARGS__ " =", __VA_ARGS__)
#else
#define debug(...) 0
#endif
constexpr int N=100005,H=200005+N,inf=1000000000,mod=1000000007;
namespace bcj {
    int fa[H],siz[H];
    inline void init() {
        for(int i=1;i<H;i++) fa[i]=i,siz[i]=1;
    }
    inline int find(int u) {
        return fa[u]==u?u:fa[u]=find(fa[u]);
    }
    inline void mg(int u,int v) {
        u=find(u),v=find(v);
        if(u==v) return;
        if(siz[u]<siz[v]) fswap(u,v);
        siz[u]+=siz[v],fa[v]=u;
    }
}
using bcj::siz;
using bcj::find;
using bcj::mg;
int tot=1,id[N],di[H],fa[H],dep[H],ch[H][27];
int pre[H],nxt[H],be[H],en[H],cson[H];
inline void ins(const string& s,const int& i) {
    int u=1;
    for(char c: s) {
        if(!ch[u][c-96]) {
            ch[u][c-96]=++tot;
            fa[tot]=u,dep[tot]=dep[u]+1,cson[u]++;
        }
        u=ch[u][c-96];
    }
    id[i]=u,di[u]=i;
}
set<pair<int,int> > bst;
int n,q,tot2,x[N],y[N],ans2[N];
string s[N];
int tot3,ans3[N];
set<int> tmp;
void dfs(int u) {
    // debug(u);
    if(di[u]) ans3[++tot3]=di[u];
    for(int t=be[u];t&&~t;t=nxt[t])
        dfs(t);
    int v=0;
    if(en[u]) {
        v=en[u];
        while(pre[v]) v=pre[v];
        if(v==be[u]) v=0;
    }
    for(int i=0;i<27;i++)
        if(ch[u][i]&&ch[u][i]!=v&&!pre[ch[u][i]]) {
            for(int t=ch[u][i];t;t=nxt[t])
                dfs(t);
        }
    for(;v&&~v;v=nxt[v])
        dfs(v);
}
void dfs1(int u) {
    debug(u);
    if(di[u]) ans3[++tot3]=di[u];
    for(int i=0;i<27;i++)
        if(ch[u][i])
            dfs1(ch[u][i]);
}
int main() {
    bcj::init();
    cin.tie(nullptr)->sync_with_stdio(false);
    cin>>n>>q;
    for(int i=1;i<=n;i++) {
        cin>>s[i],s[i]+=char(96);
        ins(s[i],i);
    }
    for(int i=1;i<=q;i++) cin>>x[i]>>y[i];
    for(int i=q;i>=1;i--) {
        int u=id[x[i]],v=id[y[i]];
        if(bst.count(mp(x[i],y[i]))) {ans2[++tot2]=i;continue;}
        // debug(i,u,v);
        bool no=0;
        while(dep[u]>dep[v]&&!no) {
            if(en[fa[u]]||nxt[u]) no=1;
            if(be[fa[u]]&&find(be[fa[u]])==find(u)&&siz[find(u)]<cson[fa[u]]) no=1;
            u=fa[u];
        }
        while(dep[u]<dep[v]&&!no) {
            if(be[fa[v]]||pre[v]) no=1;
            if(en[fa[v]]&&find(en[fa[v]])==find(v)&&siz[find(v)]<cson[fa[v]]) no=1;
            v=fa[v];
        }
        while(fa[u]!=fa[v]&&!no) {
            if(en[fa[u]]||nxt[u]) no=1;
            if(be[fa[u]]&&find(be[fa[u]])==find(u)&&siz[find(u)]<cson[fa[u]]) no=1;
            if(be[fa[v]]||pre[v]) no=1;
            if(en[fa[v]]&&find(en[fa[v]])==find(v)&&siz[find(v)]<cson[fa[v]]) no=1;
            u=fa[u],v=fa[v];
        }
        if(nxt[u]||pre[v]||find(u)==find(v)) no=1;
        if((find(u)==find(be[fa[u]])||find(u)==find(en[fa[u]]))&&
            (find(v)==find(be[fa[u]])||find(v)==find(en[fa[u]]))&&
            siz[find(u)]+siz[find(v)]<cson[fa[u]]) no=1;
        if(no) continue;
        // ---
        u=id[x[i]],v=id[y[i]];
        while(dep[u]>dep[v]) {
            en[fa[u]]=u,nxt[u]=-1;
            u=fa[u];
        }
        while(dep[u]<dep[v]) {
            be[fa[v]]=v,pre[v]=-1;
            v=fa[v];
        }
        while(fa[u]!=fa[v]) {
            en[fa[u]]=u,nxt[u]=-1;
            u=fa[u];
            be[fa[v]]=v,pre[v]=-1;
            v=fa[v];
        }
        nxt[u]=v,pre[v]=u,mg(u,v);
        bst.emplace(x[i],y[i]);
        ans2[++tot2]=i;
    }
    sort(ans2+1,ans2+tot2+1);
    ll ans1=0;
    dfs1(1);
    for(int i=1;i<n;i++) {
        ll lcp=0;
        while(s[ans3[i]][lcp]==s[ans3[i+1]][lcp]) lcp++;
        ans1+=lcp*lcp;
    }
    tot3=0;dfs(1);
    assert(n<=tot3);
    cout<<ans1<<'\n'<<tot2;
    for(int i=1;i<=tot2;i++)
        cout<<' '<<ans2[i];
    cout<<'\n';
    for(int i=1;i<=n;i++)
        cout<<ans3[i]<<' ';
    return 0;
}