# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
240678 | karma | Magic Tree (CEOI19_magictree) | C++14 | 165 ms | 36984 KiB |
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 <bits/stdc++.h>
#define pb emplace_back
#define ll long long
#define fi first
#define se second
#define mp make_pair
//#define int int64_t
using namespace std;
const int N = int(1e5) + 10;
typedef pair<int, int> pii;
int n, m, k, p;
vector<int> adj[N];
pii fr[N];
map<int, ll> f[N];
void dfs(int u) {
for(int v: adj[u]) {
dfs(v);
if(f[u].size() < f[v].size()) f[u].swap(f[v]);
for(auto& ff: f[v]) f[u][ff.fi] += ff.se;
}
if(fr[u].fi) {
ll tmp = 0;
while(1) {
auto it = f[u].upper_bound(fr[u].fi);
if(it == f[u].end()) break;
if(it->se + tmp > fr[u].se) {
it->se += (tmp - fr[u].se);
break;
}
tmp += it->se;
f[u].erase(it);
}
f[u][fr[u].fi] += fr[u].se;
}
//cout << u << '\n';
//for(auto ff: f[u]) cout << ff.fi << ' ' << ff.se << '\n';
}
int32_t main() {
ios_base::sync_with_stdio(0);
cin.tie(0), cout.tie(0);
#define Task "test"
if(fopen(Task".inp", "r")) {
freopen(Task".inp", "r", stdin);
freopen(Task".out", "w", stdout);
}
cin >> n >> m >> k;
for(int i = 2; i <= n; ++i) {
cin >> p;
adj[p].pb(i);
}
for(int v, d, w, i = 1; i <= m; ++i) {
cin >> v >> d >> w;
fr[v] = mp(d, w);
}
dfs(1);
ll res = 0;
for(auto ff: f[1]) res += ff.se;
cout << res;
}
Compilation message (stderr)
# | 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... |