From 43af4da056f7fc668da9e8a7adea78a474585047 Mon Sep 17 00:00:00 2001 From: LittleSheep Date: Sun, 14 Apr 2024 15:32:42 +0800 Subject: [PATCH] =?UTF-8?q?:white=5Fcheck=5Fmark:=20=E6=B4=BB=E5=8A=A8?= =?UTF-8?q?=E4=BA=BA=E6=95=B0=20=E5=8D=81=E5=9B=9B=E5=B1=8A=E8=93=9D?= =?UTF-8?q?=E6=A1=A5=E6=9D=AF=E9=9D=92=E5=B0=91=E5=B9=B4=E7=BB=84=E7=9C=81?= =?UTF-8?q?=E8=B5=9B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- counting-people/main.cc | 46 +++++++++++++++++++++++++++++++++++++++++ counting-people/main.in | 7 +++++++ 2 files changed, 53 insertions(+) create mode 100644 counting-people/main.cc create mode 100644 counting-people/main.in diff --git a/counting-people/main.cc b/counting-people/main.cc new file mode 100644 index 0000000..0cc60ad --- /dev/null +++ b/counting-people/main.cc @@ -0,0 +1,46 @@ +#include +#include +#include +using namespace std; + +const int MAXN = 1e5 + 5; + +vector tree[MAXN]; +int amount[MAXN]; +int mem[MAXN][2]; +int N, Rt; + +int dfs(int node, int flag) { + if (mem[node][flag] > -1) { + return mem[node][flag]; + } + + mem[node][0] = 0; + mem[node][1] = amount[node]; + + for (int i = 0; i < tree[node].size(); i++) { + int leaf = tree[node][i]; + mem[node][0] += max(dfs(leaf, 0), dfs(leaf, 1)); + mem[node][1] += dfs(leaf, 0); + } + + return mem[node][flag]; +} + +int main() { + // Init + cin >> N; + memset(mem, -1, sizeof mem); + for (int i = 0; i < N; i++) { + int depends, idx, c; + cin >> depends >> idx >> c; + amount[idx] = c; + if (depends == 0) { + Rt = idx; + } else { + tree[depends].push_back(idx); + } + } + // Calculating! + printf("%d", max(dfs(Rt, 1), dfs(Rt, 0))); +} diff --git a/counting-people/main.in b/counting-people/main.in new file mode 100644 index 0000000..0c9f062 --- /dev/null +++ b/counting-people/main.in @@ -0,0 +1,7 @@ +6 +0 1 2 +1 2 4 +1 3 3 +2 4 3 +3 5 2 +3 6 4