Submission #413506

#TimeUsernameProblemLanguageResultExecution timeMemory
413506nicolaalexandraMagic Tree (CEOI19_magictree)C++14
37 / 100
2071 ms38408 KiB
#include <bits/stdc++.h> #define DIM 100010 using namespace std; int v[DIM],cost[DIM]; long long dp[DIM][30]; vector <int> L[DIM]; int n,m,k,i,j,x,y,w,d; void dfs (int nod, int tata){ int ok = 0; for (auto vecin : L[nod]) if (vecin != tata){ ok = 1; dfs (vecin,nod); } if (!ok){ if (v[nod]){ for (int i=v[nod];i<=k;i++) dp[nod][i] = cost[nod]; } } else { for (auto vecin : L[nod]){ if (vecin == tata) continue; for (int i=1;i<=k;i++){ dp[nod][i] += dp[vecin][i]; } } if (v[nod]){ long long val = cost[nod]; for (auto vecin : L[nod]) if (vecin != tata) val += dp[vecin][v[nod]]; dp[nod][v[nod]] = max (dp[nod][v[nod]],val); } for (int i=1;i<=k;i++) dp[nod][i] = max (dp[nod][i],dp[nod][i-1]); } } int main (){ //ifstream cin ("date.in"); //ofstream cout ("date.out"); cin>>n>>m>>k; for (i=2;i<=n;i++){ cin>>x; L[x].push_back(i); L[i].push_back(x); } int ok = 1; long long sum = 0; for (i=1;i<=m;i++){ cin>>x>>d>>w; v[x] = d, cost[x] = w; if (L[x].size() > 1) ok = 0; sum += w; } if (ok){ cout<<sum; return 0; } dfs (1,0); long long sol = 0; for (i=1;i<=k;i++) sol = max (sol,dp[1][i]); /*for (i=1;i<=n;i++,cout<<"\n") for (j=1;j<=k;j++) cout<<dp[i][j]<<" "; */ cout<<sol; return 0; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...