Submission #242265

#TimeUsernameProblemLanguageResultExecution timeMemory
242265lycMagic Tree (CEOI19_magictree)C++14
34 / 100
2008 ms791996 KiB
#include <bits/stdc++.h> using namespace std; #define TRACE(x) cerr << #x << " :: " << x << endl #define _ << " " << #define SZ(x) (int)(x).size() #define ALL(x) (x).begin(),(x).end() #define FOR(i,a,b) for(int i=(a);i<=(b);++i) #define RFOR(i,a,b) for (int i=(a);i>=(b);--i) using ll=long long; using ii=pair<int,int>; using pll=pair<ll,ll>; const int mxN = 1e5+5; const int mxM = mxN; const int mxK = 1e5+5; int N, M, K; vector<int> al[mxN]; ii fruit[mxN]; vector<int> vals; ll dp[mxN][1005]; void dfs(int u) { for (int v : al[u]) { dfs(v); } FOR(k,0,SZ(vals)-1){ dp[u][k] = 0; for (int v : al[u]) dp[u][k] += dp[v][k]; if (fruit[u].first <= k) { ll sum = fruit[u].second; for (int v : al[u]) sum += dp[v][fruit[u].first]; dp[u][k] = max(dp[u][k],sum); } } } int main() { ios::sync_with_stdio(false); cin.tie(0); cin >> N >> M >> K; FOR(i,2,N){ int P; cin >> P; al[P].push_back(i); } FOR(i,1,N) fruit[i] = ii(-1,-1); FOR(i,1,M){ int V, D, W; cin >> V >> D >> W; fruit[V] = ii(D,W); vals.push_back(D); } vals.push_back(mxK); sort(ALL(vals)); vals.resize(unique(ALL(vals))-vals.begin()); fruit[0] = ii(mxK,0); FOR(i,0,N) if (fruit[i] != ii(-1,-1)) { fruit[i].first = lower_bound(ALL(vals),fruit[i].first)-vals.begin(); } dfs(1); cout << dp[1][SZ(vals)-1] << '\n'; }
#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...