From 772fff1906efcb4755342b55dd4edfce15f7e959 Mon Sep 17 00:00:00 2001 From: Baoshuo Date: Thu, 7 Apr 2022 14:30:07 +0800 Subject: [PATCH] =?UTF-8?q?285.=20=E6=B2=A1=E6=9C=89=E4=B8=8A=E5=8F=B8?= =?UTF-8?q?=E7=9A=84=E8=88=9E=E4=BC=9A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit https://www.acwing.com/problem/content/submission/code_detail/13122554/ --- AcWing/285/285.cpp | 50 +++++++++++++++++++++++++++++++++++++++++++ AcWing/285/data/7.ans | 3 +++ AcWing/285/data/7.in | 3 +++ 3 files changed, 56 insertions(+) create mode 100644 AcWing/285/285.cpp create mode 100644 AcWing/285/data/7.ans create mode 100644 AcWing/285/data/7.in diff --git a/AcWing/285/285.cpp b/AcWing/285/285.cpp new file mode 100644 index 00000000..0f4d9586 --- /dev/null +++ b/AcWing/285/285.cpp @@ -0,0 +1,50 @@ +#include +#include + +using std::cin; +using std::cout; +const char endl = '\n'; + +const int N = 6005; + +int n, h[N], f[N][2], root; +std::vector g[N]; +bool vis[N]; + +void dfs(int u) { + f[u][0] = 0; + f[u][1] = h[u]; + + for (int v : g[u]) { + dfs(v); + f[u][0] += std::max(f[v][0], f[v][1]); + f[u][1] += f[v][0]; + } +} + +int main() { + std::ios::sync_with_stdio(false); + + cin >> n; + for (int i = 1; i <= n; i++) { + cin >> h[i]; + } + for (int i = 1, l, k; i < n; i++) { + cin >> l >> k; + vis[l] = true; + g[k].push_back(l); + } + + for (int i = 1; i <= n; i++) { + if (!vis[i]) { + root = i; + break; + } + } + + dfs(root); + + cout << std::max(f[root][0], f[root][1]) << endl; + + return 0; +} diff --git a/AcWing/285/data/7.ans b/AcWing/285/data/7.ans new file mode 100644 index 00000000..d595cdb8 --- /dev/null +++ b/AcWing/285/data/7.ans @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:7de1555df0c2700329e815b93b32c571c3ea54dc967b89e81ab73b9972b72d1d +size 2 diff --git a/AcWing/285/data/7.in b/AcWing/285/data/7.in new file mode 100644 index 00000000..80126c2e --- /dev/null +++ b/AcWing/285/data/7.in @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:d6e98d66a8a7b45c497b34232c11797b070c76b6f1a376a17e7a6acd903e470d +size 43