# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
242273 | tqbfjotld | Magic Tree (CEOI19_magictree) | C++14 | 86 ms | 8056 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.
/*
subtask 1 - brute force
subtask 2 - take all
subtask 3 - lds
subtask 4 - dp on tree, day1-day2
*/
#include <bits/stdc++.h>
using namespace std;
int n,m,k;
int p[100005];
int d[100005];
long long w[100005];
vector<int> adjl[100005];
long long tot[100005];
long long func(int node){
if (adjl[node].size()==0){
tot[node] = d[node]==1?w[node]:-w[node];
return max(0LL,tot[node]);
}
long long ans = 0;
for (auto x : adjl[node]){
ans += func(x);
tot[node] += tot[x];
}
tot[node] += d[node]==1?w[node]:-w[node];
return max(ans,tot[node]);
}
int main(){
scanf("%d%d%d",&n,&m,&k);
bool subtask3 = true;
for (int x = 1; x<n; x++){
scanf("%d",&p[x]);
p[x]--;
if (p[x]!=x-1) subtask3 = false;
adjl[p[x]].push_back(x);
}
long long temps=0;
for (int x = 0; x<m; x++){
int a,b;
long long c;
scanf("%d%d%lld",&a,&b,&c);
a--;
d[a] = b;
w[a] = c;
if (w[a]!=1) subtask3 = false;
if (d[x]==2) temps+=w[x];
}
if (k<=2){
printf("%lld",func(0)+temps);
}
else if (subtask3){
vector<int> v;
for (int x = 0; x<n; x++){
if (w[x]==0) continue;
if (v.empty()) v.push_back(-d[x]);
else if (-d[x]>=v[(int)v.size()-1]){
v.push_back(-d[x]);
}
else{
int pos = upper_bound(v.begin(),v.end(),-d[x])-v.begin();
v[pos] = -d[x];
}
}
printf("%d",v.size());
}
else{
long long ans = 0;
for (int x = 0; x<n; x++){
ans += w[x];
}
printf("%lld",ans);
}
}
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... |