P8867 [NOIP2022] 建造军营 个人题解

· · 题解

前言

考场上没做出来,一直想推导各种选点选边情况的个数最后再乘 2 的幂。

后来才发现直接 DP 挺好写。

分析

注意到只有桥边被切断才会导致不连通,于是先边双缩点,记录每个 e-DCC 的 siz。

设 f_{x,0/1},f_{x,1} 表示仅 x 的子树中有建造兵营并且建造的兵营和 x 联通的道路上的路都被保护的方案数,f_{x,0} 表示 x 子树中完全不建造兵营的方案数。

首先每个 e-DCC 内部的边随便断并不影响,所以可以留到最后一起计算,DP 只需要考虑桥边。

转移如下:

f_{x,0} \leftarrow f_{x,0} \times f_{y,0} \times 2 f_{x,1} \leftarrow f_{x,1} \times f_{y,0} \times 2 f_{x,1} \leftarrow f_{x,1} \times f_{y,1} f_{x,1} \leftarrow f_{x,0} \times f_{y,1}

这四个更新是同时进行的,可以用两个变量记录增加的值,扫完四个更新再加上。

初始化: f_{x,0}=1 ,f_{x,1}=2^{siz_x}-1

表示 x 点上的 e-DCC 中任意选只有 1 种方案为空。

计算答案:

对于每颗子树 x,记录它在缩点后树上子树的大小 newsiz_x,由于 x 节点的父边(如果存在)必须不被保护(否则就应该记录到父亲节点的答案中),同时由定义子树外的点也不能选,注意到根节点(记为 root)没有父亲,所以答案应为:

\sum_{x=1,x \neq root }^{n}{f_{x,1} \times 2^{m-newsiz_x}}+f_{root,1} \times 2^{m-(newsiz_{root}-1)}

代码里写的是预处理 2 的幂次,注意因为次数和 m 有关所以应预处理到 10^6 级别。

代码

代码中的 nsiz 数组表示上文所说的 newsiz 数组。

#include<bits/stdc++.h>
using namespace std;
using ll = long long;
const int N = 500010;
const int M = 2000010;
const int mod = 1e9+7;
ll read(){
    char ch=getchar(); ll res=0;
    while(!isdigit(ch)){ch=getchar();}
    while(isdigit(ch)){res=(res<<1)+(res<<3)+ch-'0';ch=getchar();}
    return res;
}
int n, m;
int head1[N], pre1[M], ver1[M], tot1=1, from1[M];
void add1(int x,int y){
    ver1[++tot1]=y; pre1[tot1]=head1[x]; head1[x]=tot1; from1[tot1]=x;
}
int head[N], pre[M], ver[M], tot;
void add(int x,int y){
    ver[++tot]=y; pre[tot]=head[x]; head[x]=tot;
}
int dfn[N], low[N], tp, dc, dcc[N], siz[N];
bool bri[M];
void tarjan(int x,int in_edge){
    dfn[x]=low[x]=++tp;
    for (int i=head1[x]; i; i=pre1[i]){
        int v=ver1[i];
        if (!dfn[v]){
            tarjan(v,i);
            low[x]=min(low[x],low[v]);
            if (dfn[x]<low[v]) bri[i]=bri[i^1]=1;
        }
        else if (i!=(in_edge^1)) low[x]=min(low[x],dfn[v]);
    }
}
void dfs1(int x){
    dcc[x]=dc; siz[dc]++;
    for (int i=head1[x]; i; i=pre1[i]){
        int v=ver1[i];
        if (!bri[i] && !dcc[v]) dfs1(v);
    }
}
ll nsiz[N], f[N][2], pw[M];
ll ans;
void dfs(int x,int fa){
    nsiz[x]=1;
    f[x][0]=1; f[x][1]=pw[siz[x]]-1;
    for (int i=head[x]; i; i=pre[i]){
        int y=ver[i];
        if (y==fa) continue;
        dfs(y,x);
        nsiz[x]+=nsiz[y];
        ll f0=0, f1=0;
        f0=f[x][0]*2%mod*f[y][0]%mod;
        f1=f[x][1]*2%mod*f[y][0]%mod;
        f1=(f1+f[x][1]*f[y][1]%mod)%mod;
        f1=(f1+f[x][0]*f[y][1]%mod)%mod;
        f[x][0]=f0; f[x][1]=f1;
    }
    int base=nsiz[x];
    if (x==1) base--;
    ans=(ans+f[x][1]*pw[m-base]%mod)%mod;
}
int main(){
    //freopen("barrack.in","r",stdin);
    //freopen("barrack.out","w",stdout);
    //freopen("barrack4.in","r",stdin);
    n=read(); m=read();
    for (int i=1;i<=m;++i){
        int x=read(), y=read();
        add1(x,y); add1(y,x);
    }

    for (int i=1;i<=n;++i){
        if (!dfn[i]) tarjan(i,0);
    }
    for (int i=1;i<=n;++i){
        if (!dcc[i]) {
            dc++;
            dfs1(i);
        }
    }
    for (int i=2;i<=tot1;++i){
        if (bri[i]) add(dcc[from1[i]],dcc[ver1[i]]);
    }
    pw[0]=1;
    for (int i=1;i<=M/2;++i) pw[i]=pw[i-1]*2%mod;
    dfs(1,0);
    cout<<ans;
    return 0;
}
/*
6 7
1 2
2 3
1 3
2 4
4 5
4 5
3 6

3968
*/