이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
vector<ll> graph[101010];
pair<ll, ll> fruit[101010];
ll n, m, k, ans;
void merge(map<ll, ll> &X, map<ll, ll> &Y) {
if (X.size() < Y.size()) swap(X, Y);
for (pair<ll, ll> t : Y) X[t.first] += t.second;
}
void dfs(ll p, map<ll, ll> &X) {
ll s, t, x, y;
for (ll &t : graph[p]) {
map<ll, ll> Y;
dfs(t, Y);
merge(X, Y);
}
if (fruit[p].second) {
tie(x, y) = fruit[p];
X[x] += y;
auto it = next(X.find(x));
for (s = 0; it != X.end(); it = next(X.find(x))) {
s += it->second;
t = it->first;
X.erase(it);
if (s > y) {
X[t] = s - y;
break;
}
}
}
}
int main() {
ll i, p, d, w;
cin >> n >> m >> k;
for (i = 2; i <= n; i++) {
cin >> p;
graph[p].push_back(i);
}
for (i = 1; i <= m; i++) {
cin >> p >> d >> w;
fruit[p] = pair<ll, ll>(d, w);
}
map<ll, ll> X;
dfs(1, X);
for (pair<ll, ll> t : X) {
ans += t.second;
}
cout << ans;
return 0;
}
# | 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... |