結果

問題 No.2115 Making Forest Easy
ユーザー ygussanyygussany
提出日時 2022-10-29 00:47:30
言語 C
(gcc 12.3.0)
結果
AC  
実行時間 1,529 ms / 2,000 ms
コード長 2,723 bytes
コンパイル時間 1,122 ms
コンパイル使用メモリ 34,176 KB
実行使用メモリ 119,480 KB
最終ジャッジ日時 2024-07-06 03:51:37
合計ジャッジ時間 27,443 ms
ジャッジサーバーID
(参考情報)
judge3 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
6,816 KB
testcase_01 AC 1 ms
6,940 KB
testcase_02 AC 1,489 ms
119,208 KB
testcase_03 AC 348 ms
119,364 KB
testcase_04 AC 1,121 ms
119,092 KB
testcase_05 AC 808 ms
102,016 KB
testcase_06 AC 1,495 ms
119,160 KB
testcase_07 AC 322 ms
119,212 KB
testcase_08 AC 1,529 ms
119,220 KB
testcase_09 AC 258 ms
119,436 KB
testcase_10 AC 1,478 ms
119,428 KB
testcase_11 AC 242 ms
119,248 KB
testcase_12 AC 253 ms
119,480 KB
testcase_13 AC 166 ms
107,500 KB
testcase_14 AC 262 ms
119,264 KB
testcase_15 AC 326 ms
119,404 KB
testcase_16 AC 235 ms
119,348 KB
testcase_17 AC 15 ms
14,316 KB
testcase_18 AC 239 ms
21,912 KB
testcase_19 AC 747 ms
67,316 KB
testcase_20 AC 271 ms
99,024 KB
testcase_21 AC 74 ms
11,464 KB
testcase_22 AC 56 ms
26,540 KB
testcase_23 AC 973 ms
92,392 KB
testcase_24 AC 247 ms
119,252 KB
testcase_25 AC 364 ms
54,052 KB
testcase_26 AC 252 ms
119,244 KB
testcase_27 AC 143 ms
66,136 KB
testcase_28 AC 303 ms
119,216 KB
testcase_29 AC 150 ms
16,548 KB
testcase_30 AC 335 ms
119,392 KB
testcase_31 AC 1,099 ms
119,068 KB
testcase_32 AC 1,520 ms
119,320 KB
testcase_33 AC 1,499 ms
119,144 KB
testcase_34 AC 23 ms
17,144 KB
testcase_35 AC 317 ms
29,308 KB
testcase_36 AC 1,513 ms
119,184 KB
testcase_37 AC 122 ms
62,620 KB
testcase_38 AC 46 ms
34,616 KB
testcase_39 AC 596 ms
51,020 KB
testcase_40 AC 304 ms
119,300 KB
testcase_41 AC 247 ms
119,268 KB
testcase_42 AC 212 ms
119,260 KB
testcase_43 AC 145 ms
56,540 KB
testcase_44 AC 46 ms
34,384 KB
testcase_45 AC 249 ms
91,620 KB
testcase_46 AC 177 ms
119,060 KB
testcase_47 AC 130 ms
49,860 KB
testcase_48 AC 1,033 ms
119,248 KB
testcase_49 AC 1,143 ms
119,324 KB
testcase_50 AC 127 ms
74,796 KB
testcase_51 AC 97 ms
50,104 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <stdio.h>

const int Mod = 998244353;

typedef struct Edge {
	struct Edge *next;
	int v, id;
	long long dp[1001];
} edge;

long long div_mod(long long x, long long y, long long z)
{
	if (x % y == 0) return x / y;
	else return (div_mod((1 + x / y) * y - x, (z % y), y) * z + x) / y;
}

int main()
{
	int i, N, M, u, w, A[5001];
	edge *adj[5001] = {}, e[10001], *p;
	scanf("%d", &N);
	for (u = 1; u <= N; u++) scanf("%d", &(A[u]));
	for (i = 0; i < N - 1; i++) {
		scanf("%d %d", &u, &w);
		e[i*2].v = w;
		e[i*2+1].v = u;
		e[i*2].id = i * 2;
		e[i*2+1].id = i * 2 + 1;
		e[i*2].next = adj[u];
		e[i*2+1].next = adj[w];
		adj[u] = &(e[i*2]);
		adj[w] = &(e[i*2+1]);
	}
	
	int par[5001] = {}, pred[5001], q[5001], head, tail;
	q[0] = 1;
	par[1] = 1;
	pred[1] = -1;
	for (head = 0, tail = 1; head < tail; head++) {
		u = q[head];
		for (p = adj[u]; p != NULL; p = p->next) {
			w = p->v;
			if (par[w] == 0) {
				par[w] = u;
				pred[w] = p->id;
				q[tail++] = w;
			}
		}
	}
	
	int k;
	long long sum;
	for (head--; head >= 1; head--) {
		u = q[head];
		i = pred[u];
		
		if (adj[u]->next == NULL) {
			for (k = 0; k < A[u]; k++) e[i].dp[k] = 1;
			for (k = A[u]; k <= 1000; k++) e[i].dp[k] = 2;
			continue;
		}
		
		for (p = adj[u], sum = 1; p != NULL; p = p->next) {
			if (p->v == par[u]) continue;
			sum = sum * p->dp[1000] % Mod;
		}
		e[i].dp[0] = sum;
		for (k = 1; k < A[u]; k++) e[i].dp[k] = e[i].dp[0];
		for (k = A[u]; k <= 1000; k++) {
			for (p = adj[u], sum = 1; p != NULL; p = p->next) {
				if (p->v == par[u]) continue;
				sum = sum * p->dp[k] % Mod;
			}
			e[i].dp[k] = (e[i].dp[0] + sum) % Mod;
		}
	}
	
	long long ans = 0;
	static long long prod[5001][1001];
	for (k = 0; k <= 1000; k++) {
		for (p = adj[1], prod[1][k] = 1; p != NULL; p = p->next) prod[1][k] = prod[1][k] * p->dp[k] % Mod;
		if (k > 0) ans += (prod[1][k] - prod[1][k-1] + Mod) * ((k >= A[1])? k: A[1]) % Mod;
	}
	ans += prod[1][0] * A[1] % Mod;
	for (head = 1; head < tail; head++) {
		w = q[head];
		i = pred[w] ^ 1;
		u = par[w];
		
		if (adj[u]->next == NULL) {
			for (k = 0; k < A[u]; k++) e[i].dp[k] = 1;
			for (k = A[u]; k <= 1000; k++) e[i].dp[k] = 2;
		} else {
			e[i].dp[0] = div_mod(prod[u][1000], e[i^1].dp[1000], Mod);
			for (k = 1; k < A[u]; k++) e[i].dp[k] = e[i].dp[0];
			for (k = A[u]; k <= 1000; k++) e[i].dp[k] = (e[i].dp[0] + div_mod(prod[u][k], e[i^1].dp[k], Mod)) % Mod;
		}

		for (k = 0; k <= 1000; k++) {
			for (p = adj[w], prod[w][k] = 1; p != NULL; p = p->next) prod[w][k] = prod[w][k] * p->dp[k] % Mod;
			if (k > 0) ans += (prod[w][k] - prod[w][k-1] + Mod) * ((k >= A[w])? k: A[w]) % Mod;
		}
		ans += prod[w][0] * A[w] % Mod;
	}
	printf("%lld\n", ans % Mod);
	fflush(stdout);
	return 0;
}
0