跳转至

Components

原题链接

TAG:树形DP,模运算

思路

定义状态:\(dp[i][j][0/1]\) 表示在以 \(i\) 为根的子数,其中有 \(j\) 个连通块的方案个数。其中第三维 \(0\) 表示 不选当前的 \(i\) 点,\(1\) 表示选择 \(i\) 点。

状态转移:当 \(V_i\)\(V_j\) 之间连边且都被选的时候,连通块的总数需要 \(-1\),其余情况连通块数量不变。

代码

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
#include <bits/stdc++.h>
using i64 = long long;

const int P = 998244353;

// assume -P <= x < 2P
int norm(int x) {
    if (x < 0) {
        x += P;
    }
    if (x >= P) {
        x -= P;
    }
    return x;
}
template<class T>
T power(T a, i64 b) {
    T res = 1;
    for (; b; b /= 2, a *= a) {
        if (b % 2) {
            res *= a;
        }
    }
    return res;
}
struct Z {
    int x;
    Z(int x = 0) : x(norm(x)) {}
    Z(i64 x) : x(norm(x % P)) {}
    int val() const {
        return x;
    }
    Z operator-() const {
        return Z(norm(P - x));
    }
    Z inv() const {
        assert(x != 0);
        return power(*this, P - 2);
    }
    Z &operator*=(const Z &rhs) {
        x = i64(x) * rhs.x % P;
        return *this;
    }
    Z &operator+=(const Z &rhs) {
        x = norm(x + rhs.x);
        return *this;
    }
    Z &operator-=(const Z &rhs) {
        x = norm(x - rhs.x);
        return *this;
    }
    Z &operator/=(const Z &rhs) {
        return *this *= rhs.inv();
    }
    friend Z operator*(const Z &lhs, const Z &rhs) {
        Z res = lhs;
        res *= rhs;
        return res;
    }
    friend Z operator+(const Z &lhs, const Z &rhs) {
        Z res = lhs;
        res += rhs;
        return res;
    }
    friend Z operator-(const Z &lhs, const Z &rhs) {
        Z res = lhs;
        res -= rhs;
        return res;
    }
    friend Z operator/(const Z &lhs, const Z &rhs) {
        Z res = lhs;
        res /= rhs;
        return res;
    }
    friend std::istream &operator>>(std::istream &is, Z &a) {
        i64 v;
        is >> v;
        a = Z(v);
        return is;
    }
    friend std::ostream &operator<<(std::ostream &os, const Z &a) {
        return os << a.val();
    }
};

int main() {
    std::ios::sync_with_stdio(0);
    std::cin.tie(0);
    std::cout.tie(0);

    int n;
    std::cin >> n;
    std::vector<std::vector<int>> adj(n);
    for (int i = 0; i < n - 1; i++) {
        int u, v;
        std::cin >> u >> v;
        u--, v--;
        adj[u].push_back(v);
        adj[v].push_back(u);
    }

    std::vector<std::vector<std::array<Z, 2>>> dp(n, std::vector<std::array<Z, 2>> (n + 1, std::array<Z, 2> {}));
    std::vector<int> siz(n);

    auto dfs = [&](auto self, int x, int pa) -> void {
        siz[x] = 1;
        dp[x][0][0] = dp[x][1][1] = 1;
        for (auto y : adj[x]) {
            if (y == pa) continue;
            self(self, y, x);
            // start dp
            std::vector<std::array<Z, 2>> g(n + 1, std::array<Z, 2> {});
            for (int i = 0; i <= siz[x]; i++) {
                for (int j = 0; j <= siz[y]; j++) {
                    g[i + j][0] += dp[x][i][0] * dp[y][j][0];
                    g[i + j][0] += dp[x][i][0] * dp[y][j][1];
                    g[i + j][1] += dp[x][i][1] * dp[y][j][0];
                    if (i + j > 0) {
                        g[i + j - 1][1] += dp[x][i][1] * dp[y][j][1];
                    }
                }
            }
            std::swap(dp[x], g);
            siz[x] += siz[y];
        }
    };

    dfs(dfs, 0, -1);

    for (int i = 1; i <= n; i++) {
        std::cout << dp[0][i][0] + dp[0][i][1] << "\n";
    }

    return 0;
}
回到页面顶部