#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll,ll> ii;
typedef long double ld;
#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define ALL(x) x.begin(),x.end()
#define SZ(x) (int)x.size()
const int INF=2012345678;
const ll LLINF=4012345678012345678LL;
const ll MOD=1000000007; //998244353; //
const ld PI=3.1415926535898;
const ld EPS=1e-9;
ll gcd(ll a,ll b){if(a<b)swap(a,b);if(b==0)return a;return gcd(b,a%b);}
inline ll lcm(ll a,ll b){return a/gcd(a,b)*b;}
inline ll expo(ll b,ll p,ll m){ll res=1; while(p){if(p&1)res=(res*b)%m; b=(b*b)%m; p>>=1;} return res;}
inline ll modinv(ll a,ll m){return expo(a,m-2,m);}
int n,m,k;
vector<int> adj[100005];
pair<int,int> r[100005];
ll memo[100005][25][2];
ll dfs(int x,int t,int f){
if(memo[x][t][f]!=-1)return memo[x][t][f];
if(f){
ll ans=(r[x].fi<=t?r[x].se:0);
for(int i=0;i<SZ(adj[x]);i++){
ans+=max(dfs(adj[x][i],r[x].fi,0),dfs(adj[x][i],r[x].fi,1));
}
//printf("%d %d %d %lld\n",x,t,f,ans);
return memo[x][t][f]=ans;
}else{
ll ans=0;
for(int i=0;i<SZ(adj[x]);i++){
ans+=max(dfs(adj[x][i],t,0),dfs(adj[x][i],t,1));
}
//printf("%d %d %d %lld\n",x,t,f,ans);
return memo[x][t][f]=ans;
}
}
int main(){
scanf("%d%d%d",&n,&m,&k);
int p,v,d,w;
for(int i=1;i<n;i++){
scanf("%d",&p);p--;
adj[p].pb(i);
}
bool st2=1;
for(int i=0;i<m;i++){
scanf("%d%d%d",&v,&d,&w);v--;
r[v]=mp(d,w);
if(SZ(adj[v])!=0)st2=0;
}
if(st2){
ll ans=0;
for(int i=0;i<n;i++)ans+=r[i].se;
printf("%lld",ans);
}else{
memset(memo,-1,sizeof(memo));
printf("%lld",dfs(0,k,0));
}
}
Compilation message
magictree.cpp: In function 'int main()':
magictree.cpp:48:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d%d%d",&n,&m,&k);
~~~~~^~~~~~~~~~~~~~~~~~~
magictree.cpp:51:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d",&p);p--;
~~~~~^~~~~~~~~
magictree.cpp:56:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d%d%d",&v,&d,&w);v--;
~~~~~^~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
31 ms |
41848 KB |
Output is correct |
2 |
Correct |
34 ms |
41856 KB |
Output is correct |
3 |
Correct |
26 ms |
41856 KB |
Output is correct |
4 |
Correct |
26 ms |
41856 KB |
Output is correct |
5 |
Incorrect |
26 ms |
41856 KB |
Output isn't correct |
6 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
45 ms |
4856 KB |
Output is correct |
2 |
Correct |
36 ms |
5376 KB |
Output is correct |
3 |
Correct |
62 ms |
4220 KB |
Output is correct |
4 |
Correct |
66 ms |
3960 KB |
Output is correct |
5 |
Correct |
63 ms |
4196 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
27 ms |
41856 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
119 ms |
44284 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
31 ms |
41848 KB |
Output is correct |
2 |
Correct |
34 ms |
41856 KB |
Output is correct |
3 |
Correct |
26 ms |
41856 KB |
Output is correct |
4 |
Correct |
26 ms |
41856 KB |
Output is correct |
5 |
Incorrect |
26 ms |
41856 KB |
Output isn't correct |
6 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
38 ms |
42248 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
31 ms |
41848 KB |
Output is correct |
2 |
Correct |
34 ms |
41856 KB |
Output is correct |
3 |
Correct |
26 ms |
41856 KB |
Output is correct |
4 |
Correct |
26 ms |
41856 KB |
Output is correct |
5 |
Incorrect |
26 ms |
41856 KB |
Output isn't correct |
6 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
31 ms |
41848 KB |
Output is correct |
2 |
Correct |
34 ms |
41856 KB |
Output is correct |
3 |
Correct |
26 ms |
41856 KB |
Output is correct |
4 |
Correct |
26 ms |
41856 KB |
Output is correct |
5 |
Incorrect |
26 ms |
41856 KB |
Output isn't correct |
6 |
Halted |
0 ms |
0 KB |
- |