From 105ce7cac4882d8861a8b35bbee693c36981c118 Mon Sep 17 00:00:00 2001 From: Ren Baoshuo Date: Thu, 1 Jul 2021 14:15:07 +0800 Subject: [PATCH] =?UTF-8?q?#440.=20=E5=A4=A7=E7=99=BD=E5=85=94=E7=9A=84?= =?UTF-8?q?=E8=81=9A=E4=BC=9A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit https://sjzezoj.com/submission/12241 --- S2OJ/440/440.cpp | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 S2OJ/440/440.cpp diff --git a/S2OJ/440/440.cpp b/S2OJ/440/440.cpp new file mode 100644 index 00000000..f9dab6d1 --- /dev/null +++ b/S2OJ/440/440.cpp @@ -0,0 +1,33 @@ +#include + +using namespace std; + +int n, l, k, root, dp[6005][2], p[6005]; + +void dfs(int x) { + for (int i = 1; i <= n; i++) { + if (p[i] == x) { + dfs(i); + dp[x][0] += max(dp[i][0], dp[i][1]); + dp[x][1] += dp[i][0]; + } + } +} + +int main() { + cin >> n; + for (int i = 1; i <= n; i++) { + cin >> dp[i][1]; + } + while (cin >> l >> k && (l != 0 && k != 0)) { + p[l] = k; + } + for (int i = 1; i <= n; i++) { + if (!p[i]) { + root = i; + } + } + dfs(root); + cout << max(dp[root][0], dp[root][1]) << endl; + return 0; +}