题目链接:https://codeforces.com/gym/104787/problem/M
由于没有找到相关的题解,并且官方的题解没有给出树形dp的具体描述,于是有了这一篇博客。
这里直接给我的提交。
gym上accepted后就可以看其他vp人的代码了,所以直接给代码。
参考代码(使用ModInt):
#include <bits/stdc++.h>
using namespace std;
#define all(a) a.begin(), a.end()
#define graph(G, n, m) vector<vector<int>> G(n); for (int i = 0; i < m; i++) { int x, y; cin >> x >> y; --x; --y; G[x].push_back(y); G[y].push_back(x); }
template <class T> istream& operator>>(istream& is, vector<T>& vec) { for (T& v : vec) is >> v; return is; }
// https://github.com/hos-lyric/libra/blob/master/algebra/modint.h
template <unsigned M_> struct ModInt {
static constexpr unsigned M = M_;
unsigned x;
constexpr ModInt() : x(0U) {}
constexpr ModInt(unsigned x_) : x(x_ % M) {}
constexpr ModInt(unsigned long long x_) : x(x_ % M) {}
constexpr ModInt(int x_) : x(((x_ %= static_cast<int>(M)) < 0) ? (x_ + static_cast<int>(M)) : x_) {}
constexpr ModInt(long long x_) : x(((x_ %= static_cast<long long>(M)) < 0) ? (x_ + static_cast<long long>(M)) : x_) {}
ModInt &operator+=(const ModInt &a) { x = ((x += a.x) >= M) ? (x - M) : x; return *this; }
ModInt &operator-=(const ModInt &a) { x = ((x -= a.x) >= M) ? (x + M) : x; return *this; }
ModInt &operator*=(const ModInt &a) { x = (static_cast<unsigned long long>(x) * a.x) % M; return *this; }
ModInt &operator/=(const ModInt &a) { return (*this *= a.inv()); }
ModInt pow(long long e) const {
if (e < 0) return inv().pow(-e);
ModInt a = *this, b = 1U; for (; e; e >>= 1) { if (e & 1) b *= a; a *= a; } return b;
}
ModInt inv() const {
unsigned a = M, b = x; int y = 0, z = 1;
for (; b; ) { const unsigned q = a / b; const unsigned c = a - q * b; a = b; b = c; const int w = y - static_cast<int>(q) * z; y = z; z = w; }
assert(a == 1U); return ModInt(y);
}
ModInt operator+() const { return *this; }
ModInt operator-() const { ModInt a; a.x = x ? (M - x) : 0U; return a; }
ModInt operator+(const ModInt &a) const { return (ModInt(*this) += a); }
ModInt operator-(const ModInt &a) const { return (ModInt(*this) -= a); }
ModInt operator*(const ModInt &a) const { return (ModInt(*this) *= a); }
ModInt operator/(const ModInt &a) const { return (ModInt(*this) /= a); }
template <class T> friend ModInt operator+(T a, const ModInt &b) { return (ModInt(a) += b); }
template <class T> friend ModInt operator-(T a, const ModInt &b) { return (ModInt(a) -= b); }
template <class T> friend ModInt operator*(T a, const ModInt &b) { return (ModInt(a) *= b); }
template <class T> friend ModInt operator/(T a, const ModInt &b) { return (ModInt(a) /= b); }
explicit operator bool() const { return x; }
bool operator==(const ModInt &a) const { return (x == a.x); }
bool operator!=(const ModInt &a) const { return (x != a.x); }
friend std::ostream &operator<<(std::ostream &os, const ModInt &a) { return os << a.x; }
};
using mint = ModInt<998244353>;
template <class T> ostream& operator<<(ostream& os, const vector<T>& vec) { for (const T& v : vec) os << v << ' '; return os; }
int main () {
ios_base::sync_with_stdio(0); cin.tie(0);
int n;
cin >> n;
graph(G, n, n-1)
vector<mint> exp2(n+1);
exp2[0] = 1;
for (int i = 0; i < n; i++) exp2[i+1] = exp2[i]*2;
vector<int> type(n);
vector<int> queries(n-1);
for (int& x : queries) {
cin >> x;
--x;
type[x] = 1;
vector<array<mint, 2>> dp(n);
vector<int> vis(n);
function<void(int, int)> dfs = [&](int v, int p) {
vis[v] = 1;
int x = 0;
for (int u : G[v]) {
if (u == p) continue;
if (type[u]) {
dfs(u, v);
} else {
x++;
}
}
dp[v][0] = exp2[x];
dp[v][1] = x*(x-1 >= 0 ? exp2[x-1] : -1);
for (int u : G[v]) {
if (u == p) continue;
if (!type[u]) continue;
array<mint, 2> dpv = dp[v];
dp[v][0] = dpv[0]*(dp[u][0]+dp[u][1]*2);
dp[v][1] = dpv[0]*dp[u][1]+dpv[1]*(dp[u][0]+dp[u][1]*2);
}
};
mint ans = 1;
for (int i = 0; i < n; i++) {
if (type[i] && !vis[i]) {
dfs(i, -1);
ans *= dp[i][1];
}
}
cout << ans << '\n';
}
}