题解 CF1799H 【Tree Cutting】

· · 题解

设 e_i 表示删掉的第 i 条边,V_i 表示删完前 i 条边后的连通块点集为 V_i。

考虑钦定一个点 R 在最终的连通块 V_k 中。不妨以 R 为根,则所有被砍掉的边满足:

注意到 k 很小,考虑状压 dp。设 dp_{u, S} 表示 u 子树内有编号在 S 中的父向边被删掉的方案数。

转移:首先考虑子树内被删掉的边,其只需要满足 \cup_{v \in son_u} S_v= S 且 S_v 两两不交,方案数为 \displaystyle\prod_{v \in son_u} dp_{v, S_v}。

接下来考虑 u, fa_u 这条父向边是否删除。对于一个 S,若删除则一定会删除 \max(S)。

此时我们还需要判断当前删除的连通块的大小是否满足要求:令 \Delta_i = s_{i - 1} - s_i,则满足条件当且仅当 size_u = \displaystyle\sum_{i \in S} \Delta_i。感觉是最困难的一步。

但是直接枚举 u 把答案求和显然会算重。不过注意到对于一种合法方案,\forall u \in V_k 它都会被统计到,于是我们把答案除以 s_k 即可。

换根 dp 即可。时间复杂度为 O(n 3^k)。

代码:

#include <iostream>
#include <vector>
#include <cstring>

using namespace std;

typedef struct Node_tag {
    int n;
    int a[67];
    Node_tag(){
        memset(a, 0, sizeof(a));
    }
} Node;

const int mod = 998244353;
int diff[7], s[7], sum[67], size[5007];
Node dp1[5007], dp2[5007], pre[5007], dp3[5007], suf[5007];
vector<int> v[5007];

inline int highbit(int x){
    return 31 - __builtin_clz(x);
}

inline void add(int &x, int y){
    if ((x += y) >= mod) x -= mod;
}

inline Node conv(Node &a, Node &b){
    Node ans;
    ans.n = a.n;
    for (int i = 0; i <= a.n; i++){
        for (int j = i; ; j = i & (j - 1)){
            add(ans.a[i], 1ll * a.a[j] * b.a[i ^ j] % mod);
            if (j == 0) break;
        }
    }
    return ans;
}

inline Node cut(Node a, int size){
    Node ans;
    ans.n = a.n;
    for (int i = 0; i <= a.n; i++){
        ans.a[i] = a.a[i];
        if (sum[i] == size) add(ans.a[i], a.a[i ^ (1 << highbit(i))]);
    }
    return ans;
}

void dfs1(int u, int father, int full){
    int sz = v[u].size();
    size[u] = 1;
    for (int i = 0; i < sz; i++){
        if (v[u][i] == father){
            sz--;
            v[u].erase(v[u].begin() + i);
            break;
        }
    }
    dp1[u].n = full;
    dp1[u].a[0] = 1;
    for (int i = 0; i < sz; i++){
        int x = v[u][i];
        dfs1(x, u, full);
        size[u] += size[x];
        dp1[u] = conv(dp1[u], dp1[x]);
    }
    dp1[u] = cut(dp1[u], size[u]);
}

void dfs2(int u, int n, int full){
    int sz = v[u].size();
    for (int i = 0; i < sz; i++){
        pre[i] = dp1[v[u][i]];
        if (i > 0) pre[i] = conv(pre[i], pre[i - 1]);
    }
    dp3[u] = dp2[u];
    if (sz > 0) dp3[u] = conv(dp3[u], pre[sz - 1]);
    for (int i = sz - 1; i >= 0; i--){
        suf[i] = dp1[v[u][i]];
        if (i + 1 < sz) suf[i] = conv(suf[i], suf[i + 1]);
    }
    for (int i = 0; i < sz; i++){
        int x = v[u][i];
        dp2[x] = dp2[u];
        if (i > 0) dp2[x] = conv(dp2[x], pre[i - 1]);
        if (i + 1 < sz) dp2[x] = conv(dp2[x], suf[i + 1]);
        dp2[x] = cut(dp2[x], n - size[x]);
    }
    for (int i = 0; i < sz; i++){
        dfs2(v[u][i], n, full);
    }
}

inline int quick_pow(int x, int p){
    int ans = 1;
    while (p){
        if (p & 1) ans = 1ll * ans * x % mod;
        x = 1ll * x * x % mod;
        p >>= 1;
    }
    return ans;
}

int main(){
    int n, k, full, ans = 0;
    cin >> n;
    for (int i = 1; i < n; i++){
        int s, f;
        cin >> s >> f;
        v[s].push_back(f);
        v[f].push_back(s);
    }
    cin >> k;
    full = (1 << k) - 1;
    s[0] = n;
    for (int i = 1; i <= k; i++){
        cin >> s[i];
        diff[i] = s[i - 1] - s[i];
    }
    for (int i = 1; i <= full; i++){
        int high = highbit(i) + 1;
        sum[i] = sum[i ^ (1 << (high - 1))] + diff[high];
    }
    dfs1(1, 0, full);
    dp2[1].n = full;
    dp2[1].a[0] = 1;
    dfs2(1, n, full);
    for (int i = 1; i <= n; i++){
        add(ans, dp3[i].a[full]);
    }
    cout << 1ll * ans * quick_pow(s[k], mod - 2) % mod;
    return 0;
}