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