From a62e9cae8024c75ab26ffd4caa91dce49827b3c9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BB=84=E6=B5=B7?= <10402852@qq.com> Date: Wed, 10 Jan 2024 08:43:43 +0800 Subject: [PATCH] 'commit' --- TangDou/Topic/HuanGenDp/CF1324F.cpp | 28 ++++++++++++++-------------- 1 file changed, 14 insertions(+), 14 deletions(-) diff --git a/TangDou/Topic/HuanGenDp/CF1324F.cpp b/TangDou/Topic/HuanGenDp/CF1324F.cpp index 8917e1a..ac84b45 100644 --- a/TangDou/Topic/HuanGenDp/CF1324F.cpp +++ b/TangDou/Topic/HuanGenDp/CF1324F.cpp @@ -2,39 +2,39 @@ using namespace std; const int N = 200010, M = N << 1; -int n, a[N], f1[N], f2[N]; -int head[N << 1], nxt[N << 1], to[N << 1], tot = 0; -void add(int u, int v) { - to[++tot] = v; - nxt[tot] = head[u]; - head[u] = tot; +// 链式前向星 +int e[M], h[N], idx, w[M], ne[M]; +void add(int a, int b, int c = 0) { + e[idx] = b, ne[idx] = h[a], w[idx] = c, h[a] = idx++; } +int n, a[N], f1[N], f2[N]; + void dfs1(int u, int fa) { if (a[u]) f1[u] = 1; else f1[u] = -1; - for (int i = head[u]; i; i = nxt[i]) { - int v = to[i]; - if (v == fa) - continue; + for (int i = h[u]; ~i; i = ne[i]) { + int v = e[i]; + if (v == fa) continue; dfs1(v, u); f1[u] += max(f1[v], 0); } } void dfs2(int u, int fa) { - for (int i = head[u]; i; i = nxt[i]) { - int v = to[i]; - if (v == fa) - continue; + for (int i = h[u]; ~i; i = ne[i]) { + int v = e[i]; + if (v == fa) continue; f2[v] = max(f2[u] + f1[u] - max(f1[v], 0), 0); dfs2(v, u); } } int main() { + // 初始化链式前向星 + memset(h, -1, sizeof h); cin >> n; for (int i = 1; i <= n; i++) cin >> a[i];