[CF2069F] Graph Inclusion 题解

· · 题解

Preface

赛时因为撤销没弹栈导致没过,我是奶龙/ll

Solution

以下定义 A_u,B_u 分别表示图 A,B 中的编号为 u 的点。

先不考虑删除边的操作,发现直接维护联通块判断 A 要连多少条边才能满足 B \subseteq A 并不好处理,考虑如下转化:

  1. 初始情况下答案 ans=0;

  2. 某个操作为连接 B_u,B_v:若 B_u,B_v 已经联通则忽略该操作,否则连接 B_u,B_v,同时将 ans 加 1;

  3. 某个操作为连接 A_u,A_v:若 A_u,A_v 已经联通则忽略该操作,否则同时连接 A_u,A_v 和 B_u,B_v,若 B_u,B_v 已经联通(必然是在某个操作 1 中联通)则将 ans 减 1。

这样做的好处在于任意时刻,实际维护的图 A,B 都满足 A\subseteq B,从而转化为动态图连通性问题,可以直接用并查集来维护图的联通块。

接下来考虑删除边的操作,可以把普通并查集换成可撤销并查集来删边。但是注意到给定的加边与删边的操作不一定连续,无法直接撤销,因此需要将询问离线,预处理每条边实际存在的时刻范围,线段树分治即可。

具体实现请看代码,时间复杂度 O(n\log^2n)。

Code

注意直接用 std::stack 极大概率会直接 MLE;至于具体原因......std::stack 的默认容器是 std::deque,后面忘了。/qd

#include<bits/stdc++.h>
#define loop(x,l,r) for(ll (x) = (l);(x)<=(r);++(x))
#define rloop(x,l,r) for(ll (x) = (r);(x)>=(l);--(x))
#define elif else if
using namespace std;
using ll = int;
using pll = pair<ll,ll>;
inline ll read()
{
    ll x=0,f=1;char ch=getchar();
    while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
    while(ch>='0'&&ch<='9'){x*=10;x+=ch-'0';ch=getchar();}
    return x*f;
}
inline void write(ll x)
{
    if(x<0){putchar('-');x=-x;}
    if(x%10==x){putchar(x+'0');return;}
    write(x/10);putchar(x%10+'0');
}
struct dsu
{
    vector<ll> fa,sz;
    stack<pll,vector<pll>> ops;
    dsu(){}
    dsu(ll n):fa(vector<ll>(n+2)),sz(vector<ll>(n+2,1))
    {
        iota(fa.begin(),fa.end(),0);
        ops=stack<pll,vector<pll>>{};
    }
    ll find(ll x)
    {
        if(fa[x]==x)return x;
        else return find(fa[x]);
    }
    void merge(ll u,ll v)
    {
        ll fu=find(u),fv=find(v);
        if(fu==fv)
        {
            ops.push({-1,-1});
            return ;
        }
        if(sz[fu]<sz[fv])swap(fu,fv);
        fa[fv]=fu,sz[fu]+=sz[fv];
        ops.push({fu,fv});
    }
    void undo()
    {
        if(!ops.size())return;
        auto [u,v]=ops.top();
        ops.pop();
        if(u==-1)return;
        fa[v]=v,sz[u]-=sz[v];
    }
}dsuA,dsuB;
const ll N=4e5+5;
ll rans=0;
struct sgt
{
    #define L tr[rt].l
    #define R tr[rt].r
    #define ls (rt<<1)
    #define rs (rt<<1|1)
    struct node
    {
        ll l,r;
        vector<tuple<ll,ll,ll>> ops;
        stack<ll,vector<ll>> opps;
    }tr[N<<2];
    ll ans[N];
    void build(ll l,ll r,ll rt)
    {
        L=l,R=r;
        if(l==r)return;
        ll mid=l+r>>1;
        build(l,mid,ls);
        build(mid+1,r,rs);
    }
    void modify(ll l,ll r,const tuple<ll,ll,ll>& x,ll rt)
    {
        if(L>r||l>R)return;
        if(l<=L&&R<=r)return tr[rt].ops.push_back(x);
        modify(l,r,x,ls);
        modify(l,r,x,rs);
    }
    void dfs(ll rt)
    {
        for(auto &[opt,opu,opv]:tr[rt].ops)
        {
            if(opt==1)
            {
                ll fub=dsuB.find(opu),fvb=dsuB.find(opv);
                if(fub^fvb)
                {
                    rans++;
                    tr[rt].opps.push(2);
                }
                dsuB.merge(opu,opv);
                tr[rt].opps.push(1);
            }
            else if(opt==0)
            {
                ll fua=dsuA.find(opu),fva=dsuA.find(opv);
                ll fub=dsuB.find(opu),fvb=dsuB.find(opv);
                if(fua^fva)
                {
                    dsuA.merge(fua,fva);
                    tr[rt].opps.push(0);
                    if(fub==fvb)rans--,tr[rt].opps.push(3);
                    dsuB.merge(opu,opv);
                    tr[rt].opps.push(1);
                }
            }
        }
        if(L==R)ans[L]=rans;
        else dfs(ls),dfs(rs);
        while(tr[rt].opps.size())
        {
            auto op=tr[rt].opps.top();
            tr[rt].opps.pop();
            if(op==0)dsuA.undo();
            else if(op==1)dsuB.undo();
            else if(op==2)rans--;
            else if(op==3)rans++;
        }
    }
    #undef L
    #undef R
    #undef ls
    #undef rs
}tree;
ll n,q;
char str[10];
map<tuple<ll,ll,ll>,ll> mpv;
tuple<ll,ll,ll> qs[N];
signed main()
{
    n=read(),q=read();
    dsuA=dsu(n),dsuB=dsu(n);
    tree.build(1,q,1);
    loop(i,1,q)
    {
        scanf("%s",str);
        ll u=read(),v=read();
        if(u>v)swap(u,v);
        qs[i]={*str=='B',u,v};
        auto it=mpv.find(qs[i]);
        if(it==mpv.end())mpv.insert({qs[i],i});
        else tree.modify(it->second,i-1,qs[i],1),mpv.erase(it);
    }
    for(auto &[x,y]:mpv)tree.modify(y,q,x,1);
    tree.dfs(1);
    loop(i,1,q)printf("%lld\n",tree.ans[i]);
    return 0;
}

Postscript

感谢 @murder_drones 的大力支持/bx/bx/bx