This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <iostream>
#include <fstream>
#include <iomanip>
#include <cmath>
#include <cassert>
#include <cstring>
#include <vector>
#include <algorithm>
#include <deque>
#include <set>
#include <utility>
#include <array>
#include <complex>
using u32 = unsigned;
using i32 = int;
using i64 = long long;
using u64 = unsigned long long;
using f64 = double;
using f80 = long double;
#define int long long
using namespace std;
using pt = complex<f80>;
#define ALL(x) begin(x), end(x)
#define ShinLena cin.tie(nullptr)->sync_with_stdio(false);
#define N 100005
multiset<pair<int, i64>> s[N];
int n, m, k;
i64 dp[N];
basic_string<int> g[N];
void dfs(int u)
{
i64 nocur = 0, cur = 0, fruit = s[u].size(), d;
if (fruit) tie(d, cur) = *s[u].begin();
for (auto v : g[u])
{
dfs(v);
nocur += dp[v];
for (auto [x, y] : s[v])
{
if (fruit and x <= d) cur += y;
}
}
if (fruit and cur > nocur)
{
dp[u] = cur;
for (auto v : g[u])
{
for (auto [x, y] : s[v])
{
if (x <= d) s[u].insert({x, y});
}
}
}
else
{
dp[u] = nocur;
s[u].clear();
for (auto v : g[u])
{
for (auto [x, y] : s[v])
s[u].insert({x, y});
}
}
}
signed main()
{
ShinLena;
cin >> n >> m >> k;
for (int i = 2, p; i <= n; ++i) cin >> p, g[p].push_back(i);
for (int v, d, w, i = 0; i < m; ++i)
{
cin >> v >> d >> w;
s[v].emplace(d, w);
}
dfs(1);
cout << dp[1];
return 0;
}
Compilation message (stderr)
magictree.cpp: In function 'void dfs(long long int)':
magictree.cpp:49:23: warning: 'd' may be used uninitialized in this function [-Wmaybe-uninitialized]
49 | if (fruit and x <= d) cur += y;
| ~~~~~~^~~~~~~~~~
# | 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... |