#include <bits/stdc++.h>
#define int long long
using namespace std;
class Solver
{
public:
Solver(int n, int m, int k)
: n(n), m(m), k(k), listAdj(n + 1), fruitDays(n + 1, -1), fruitAmount(n + 1, 0) {}
void addEdge(int parent, int child)
{
listAdj[parent].push_back(child);
}
void addFruit(int node, int day, int quantity)
{
fruitDays[node] = day;
fruitAmount[node] = quantity;
}
int compute()
{
auto remainingJuice = dfs(1);
int total = 0;
for (auto &[day, amount] : remainingJuice)
total += amount;
return total;
}
private:
int n, m, k;
vector<vector<int>> listAdj;
vector<int> fruitDays;
vector<int> fruitAmount;
map<int, int> dfs(int node)
{
map<int, int> juiceMap;
for (int child : listAdj[node])
{
auto childMap = dfs(child);
if (childMap.size() > juiceMap.size())
swap(childMap, juiceMap);
for (auto &[d, a] : childMap)
{
juiceMap[d] += a;
}
}
int day = fruitDays[node];
int qty = fruitAmount[node];
if (qty > 0)
{
juiceMap[day] += qty;
int toRemove = qty;
auto it = juiceMap.upper_bound(day);
while (it != juiceMap.end() && toRemove > 0)
{
if (toRemove >= it->second)
{
toRemove -= it->second;
it = juiceMap.erase(it);
}
else
{
it->second -= toRemove;
break;
}
}
}
return juiceMap;
}
};
signed main()
{
int n, m, k;
scanf("%lld%lld%lld", &n, &m, &k);
Solver solver(n, m, k);
for (int i = 2; i <= n; ++i)
{
int parent;
scanf("%lld", &parent);
solver.addEdge(parent, i);
}
for (int i = 0; i < m; ++i)
{
int node, day, qty;
scanf("%lld%lld%lld", &node, &day, &qty);
solver.addFruit(node, day, qty);
}
printf("%lld\n", solver.compute());
}
Compilation message (stderr)
magictree.cpp: In function 'int main()':
magictree.cpp:86:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
86 | scanf("%lld%lld%lld", &n, &m, &k);
| ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~
magictree.cpp:93:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
93 | scanf("%lld", &parent);
| ~~~~~^~~~~~~~~~~~~~~~~
magictree.cpp:100:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
100 | scanf("%lld%lld%lld", &node, &day, &qty);
| ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |