【CodeChef】Painting Tree

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_39972971/article/details/83713842

【题目链接】

【思路要点】

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

【代码】

#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;
}
阅读更多
相关热词
换一批

Egg Painting

08-24

Marjar wants to play egg-painting. It needs to peel off some part of the egg shell and take out the yolk, and then paint a picture on the egg shell. But Marjar don't want to destroy the yolk. This seems quite difficult for him to take the yolk out. He decides to make a hole that is a spherical polygon consisting of n arcs of the great circles connecting n vertices. But he didn't know whether the yolk could come out or not from the hole he created on the egg shell. Your task is helping him to determine whether the hole is big enough. His egg is a sphere with radius R while the yolk is also a sphere with radius r inside the egg.nnInputnnThere are multiple test cases. The first line of input contains an integer T (0 < T ≤ 50) indicating the number of test cases. Then T test cases follow.nnThe first line of each test case contains 3 integers n, R and r(0 < n ≤ 50, 0 < r ≤ R ≤ 10000). Then n lines follow, telling the vertices of the hole in counterclockwise order when viewing from outside the egg. Each of the n lines contains 2 real numbers, each of which contains at most 2 digits after the decimal point, indicating the longitude and the latitude of the vertex. The longitude and the latitude are measured in degree. The longitude will be in (180, 180] while the latitude will be in [-90, 90]. It is guaranteed that the spherical polygon will not cross itself.nnOutputnnFor each test case, output "Yes" if it is possible to take out the yolk from through the hole in one line. Otherwise output "No".nnSample Inputnn1n3 10 1n0 0n90 0n0 90nSample OutputnnYesn

Painting A Board

09-15

The CE digital company has built an Automatic Painting Machine (APM) to paint a flat board fully covered by adjacent non-overlapping rectangles of different sizes each with a predefined color.n![](http://acm.zju.edu.cn/onlinejudge/showImage.do?name=0000%2F1424%2F1424.gif)nnTo color the board, the APM has access to a set of brushes. Each brush has a distinct color C. The APM picks one brush with color C and paints all possible rectangles having predefined color C with the following restrictions:nnTo avoid leaking the paints and mixing colors, a rectangle can only be painted if all rectangles immediately above it have already been painted. For example rectangle labeled F in Figure 1 is painted only after rectangles C and D are painted. Note that each rectangle must be painted at once, i.e. partial painting of one rectangle is not allowed.nnYou are to write a program for APM to paint a given board so that the number of brush pick-ups is minimum. Notice that if one brush is picked up more than once, all pick-ups are counted.nnnInputnnThe first line of the input file contains an integer M which is the number of test cases to solve (1 <= M <= 10). For each test case, the first line contains an integer N, the number of rectangles, followed by N lines describing the rectangles. Each rectangle R is specified by 5 integers in one line: the y and x coordinates of the upper left corner of R, the y and x coordinates of the lower right corner of R, followed by the color-code of R.nnNote that: nn1. Color-code is an integer in the range of 1 .. 20.nn2. Upper left corner of the board coordinates is always (0,0).nn3. Coordinates are in the range of 0 .. 99.nn4. N is in the range of 1..15.nnnOutputnnOne line for each test case showing the minimum number of brush pick-ups.nnnSample Inputnn1n7n0 0 2 2 1n0 2 1 6 2n2 0 4 2 1n1 2 4 4 2n1 4 3 6 1n4 0 6 4 1n3 4 6 6 2nnnSample Outputnn3

没有更多推荐了,返回首页