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
| #include <cstdio> #include <vector> using namespace std; template <class Type> void read (Type &x) { char c; bool flag = false; while ((c = getchar()) < '0' || c > '9') flag |= c == '-'; x = c - '0'; while ((c = getchar()) >= '0' && c <= '9') x = (x << 3) + (x << 1) + c - '0'; if (flag) x = ~x + 1; } template <class Type, class ...Rest> void read (Type &x, Rest &...y) { read(x); read(y...); } template <class Type> void write (Type x) { if (x < 0) putchar('-'), x = ~x + 1; if (x > 9) write(x / 10); putchar('0' + x % 10); } typedef long long LL; const int N = 5e3 + 10, mod = 998244353; vector <int> e[N]; bool col[N]; int n, sz[N][2], f[N][N], g[N]; int inv[N], fact[N], ifact[N]; void init () { inv[1] = 1; for (int i = 2; i < N; ++i) inv[i] = -(LL)(mod / i) * inv[mod % i] % mod; fact[0] = ifact[0] = 1; for (int i = 1; i < N; ++i) { fact[i] = (LL)fact[i - 1] * i % mod; ifact[i] = (LL)ifact[i - 1] * inv[i] % mod; } } int C (int a, int b) { return a < b ? 0 : (LL)fact[a] * ifact[b] % mod * ifact[a - b] % mod; } void dfs (int u, int fa) { f[u][0] = 1; for (int v : e[u]) if (v ^ fa) { dfs(v, u); for (int i = 0; i <= n >> 1; ++i) g[i] = 0; for (int i = 0; i <= min(sz[u][0] + sz[u][1], n >> 1); ++i) for (int j = 0; j <= min(sz[v][0] + sz[v][1], (n >> 1) - i); ++j) (g[i + j] += (LL)f[u][i] * f[v][j] % mod) %= mod; for (int i = 0; i <= n >> 1; ++i) f[u][i] = g[i]; sz[u][0] += sz[v][0], sz[u][1] += sz[v][1]; } for (int i = n >> 1; i; --i) (f[u][i] += (LL)f[u][i - 1] * (sz[u][col[u] ^ 1] - i + 1) % mod) %= mod; ++sz[u][col[u]]; } int main () { init(); read(n); for (int i = 1; i <= n; ++i) { char c = getchar(); while (c < '0' || c > '1') c = getchar(); col[i] = c == '1'; } for (int i = 1, a, b; i < n; ++i) { read(a, b); e[a].push_back(b); e[b].push_back(a); } dfs(1, 0); for (int i = 0; i <= n >> 1; ++i) f[1][i] = (LL)f[1][i] * fact[(n >> 1) - i] % mod; for (int i = 0; i <= n >> 1; ++i) { int res = 0; for (int j = i; j <= n >> 1; ++j) (res += (j - i & 1 ? -1ll : 1ll) * C(j, i) * f[1][j] % mod) %= mod; write((res + mod) % mod), puts(""); } return 0; }
|