結果
問題 | No.2115 Making Forest Easy |
ユーザー | ygussany |
提出日時 | 2022-10-29 00:03:29 |
言語 | C (gcc 12.3.0) |
結果 |
TLE
|
実行時間 | - |
コード長 | 2,548 bytes |
コンパイル時間 | 303 ms |
コンパイル使用メモリ | 33,864 KB |
実行使用メモリ | 86,592 KB |
最終ジャッジ日時 | 2024-07-06 03:20:20 |
合計ジャッジ時間 | 3,949 ms |
ジャッジサーバーID (参考情報) |
judge2 / judge3 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 1 ms
13,888 KB |
testcase_01 | AC | 2 ms
6,944 KB |
testcase_02 | AC | 77 ms
80,208 KB |
testcase_03 | TLE | - |
testcase_04 | -- | - |
testcase_05 | -- | - |
testcase_06 | -- | - |
testcase_07 | -- | - |
testcase_08 | -- | - |
testcase_09 | -- | - |
testcase_10 | -- | - |
testcase_11 | -- | - |
testcase_12 | -- | - |
testcase_13 | -- | - |
testcase_14 | -- | - |
testcase_15 | -- | - |
testcase_16 | -- | - |
testcase_17 | -- | - |
testcase_18 | -- | - |
testcase_19 | -- | - |
testcase_20 | -- | - |
testcase_21 | -- | - |
testcase_22 | -- | - |
testcase_23 | -- | - |
testcase_24 | -- | - |
testcase_25 | -- | - |
testcase_26 | -- | - |
testcase_27 | -- | - |
testcase_28 | -- | - |
testcase_29 | -- | - |
testcase_30 | -- | - |
testcase_31 | -- | - |
testcase_32 | -- | - |
testcase_33 | -- | - |
testcase_34 | -- | - |
testcase_35 | -- | - |
testcase_36 | -- | - |
testcase_37 | -- | - |
testcase_38 | -- | - |
testcase_39 | -- | - |
testcase_40 | -- | - |
testcase_41 | -- | - |
testcase_42 | -- | - |
testcase_43 | -- | - |
testcase_44 | -- | - |
testcase_45 | -- | - |
testcase_46 | -- | - |
testcase_47 | -- | - |
testcase_48 | -- | - |
testcase_49 | -- | - |
testcase_50 | -- | - |
testcase_51 | -- | - |
ソースコード
#include <stdio.h> const int Mod = 998244353; typedef struct Edge { struct Edge *next; int v, id; long long dp[1001]; } edge; 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++) { e[i].dp[k] = e[i].dp[0]; 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; } } 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; continue; } for (p = adj[u], sum = 1; p != NULL; p = p->next) { if (p->id == (i ^ 1)) 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++) { e[i].dp[k] = e[i].dp[0]; for (p = adj[u], sum = 1; p != NULL; p = p->next) { if (p->id == (i ^ 1)) continue; sum = sum * p->dp[k] % Mod; } e[i].dp[k] = (e[i].dp[0] + sum) % Mod; } } long long ans = 0, total[1001]; for (u = 1; u <= N; u++) { for (k = 0; k <= 1000; k++) { for (p = adj[u], total[k] = 1; p != NULL; p = p->next) total[k] = total[k] * p->dp[k] % Mod; if (k > 0) ans += (total[k] - total[k-1] + Mod) * ((k >= A[u])? k: A[u]) % Mod; } ans += total[0] * A[u] % Mod; } printf("%lld\n", ans % Mod); fflush(stdout); return 0; }