【CodeChef】Painting Tree

【题目链接】

【思路要点】

  • 我们发现直接解决问题难以入手。
  • 回忆期望的定义,有 E = ∑ i = 1 V P ( x = V ) ∗ V = ∑ i = 1 V P ( x ≥ i ) E=\sum_{i=1}^{V}P(x=V)*V=\sum_{i=1}^{V}P(x≥i) E=i=1VP(x=V)V=i=1VP(xi)
  • 记树上不同的路径数为 c n t cnt cnt ,注意到若操作步数确定为 i i i ,那么 P ( x ≥ i ) P(x≥i) P(xi) 即为在树上选择 i i i 条有序的不相交路径的方案数除以 c n t i cnt^i cnti ,可以通过简单树形 d p dp dp 做到。
  • 时间复杂度 O ( N 2 ) O(N^2) O(N2)

【代码】

#include<bits/stdc++.h>
using namespace std;
const int MAXN = 2005;
const int P = 998244353;
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
template <typename T> void chkmax(T &x, T y) {x = max(x, y); }
template <typename T> void chkmin(T &x, T y) {x = min(x, y); } 
template <typename T> void read(T &x) {
	x = 0; int f = 1;
	char c = getchar();
	for (; !isdigit(c); c = getchar()) if (c == '-') f = -f;
	for (; isdigit(c); c = getchar()) x = x * 10 + c - '0';
	x *= f;
}
template <typename T> void write(T x) {
	if (x < 0) x = -x, putchar('-');
	if (x > 9) write(x / 10);
	putchar(x % 10 + '0');
}
template <typename T> void writeln(T x) {
	write(x);
	puts("");
}
int n, size[MAXN], dp[MAXN][MAXN][3];
vector <int> a[MAXN];
void update(int &x, int y) {
	x += y;
	if (x >= P) x -= P;
}
void work(int pos, int fa) {
	size[pos] = 1;
	dp[pos][1][0] = dp[pos][0][2] = 1;
	for (auto x : a[pos])
		if (x != fa) {
			work(x, pos);
			static int res[MAXN][3];
			for (int i = 1; i <= size[pos] + size[x]; i++)
				res[i][0] = res[i][1] = res[i][2] = 0;
			for (int i = 0; i <= size[pos]; i++)
			for (int j = 0; j <= size[x]; j++) {
				update(res[i + j][0], 1ll * dp[pos][i][0] * dp[x][j][0] % P);
				if (i + j) update(res[i + j - 1][1], 1ll * dp[pos][i][0] * dp[x][j][0] % P);
				update(res[i + j][0], 1ll * dp[pos][i][0] * dp[x][j][1] % P);
				if (i + j) update(res[i + j - 1][1], 1ll * dp[pos][i][0] * dp[x][j][1] % P);
				update(res[i + j][0], 1ll * dp[pos][i][0] * dp[x][j][2] % P);
				
				update(res[i + j][1], 1ll * dp[pos][i][1] * dp[x][j][0] % P);
				if (i + j) update(res[i + j - 1][2], 1ll * dp[pos][i][1] * dp[x][j][0] % P);
				update(res[i + j][1], 1ll * dp[pos][i][1] * dp[x][j][1] % P);
				if (i + j) update(res[i + j - 1][2], 1ll * dp[pos][i][1] * dp[x][j][1] % P);
				update(res[i + j][1], 1ll * dp[pos][i][1] * dp[x][j][2] % P);
				
				update(res[i + j][2], 1ll * dp[pos][i][2] * dp[x][j][0] % P);
				update(res[i + j][2], 1ll * dp[pos][i][2] * dp[x][j][1] % P);
				update(res[i + j][2], 1ll * dp[pos][i][2] * dp[x][j][2] % P);
			}
			for (int i = 1; i <= size[pos] + size[x]; i++) {
				dp[pos][i][0] = res[i][0];
				dp[pos][i][1] = res[i][1];
				dp[pos][i][2] = res[i][2];
			}
			size[pos] += size[x];
		}
}
int power(int x, int y) {
	if (y == 0) return 1;
	int tmp = power(x, y / 2);
	if (y % 2 == 0) return 1ll * tmp * tmp % P;
	else return 1ll * tmp * tmp % P * x % P;
}
int main() {
	read(n);
	for (int i = 1; i <= n - 1; i++) {
		int x, y; read(x), read(y);
		a[x].push_back(y);
		a[y].push_back(x);
	}
	work(1, 0);
	int ans = 0, tot = ((dp[1][1][1] + dp[1][1][2]) % P + dp[1][1][0]) % P;
	int fac = 1, frac = 1;
	for (int i = 1; i <= n; i++) {
		fac = 1ll * fac * i % P;
		frac = 1ll * frac * tot % P;
		int now = ((dp[1][i][1] + dp[1][i][2]) % P + dp[1][i][0]) % P;
		update(ans, 1ll * now * fac % P * power(frac, P - 2) % P);
	}
	writeln(ans);
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值