Submission #841964

#TimeUsernameProblemLanguageResultExecution timeMemory
841964browntoadMagic Tree (CEOI19_magictree)C++14
16 / 100
99 ms19536 KiB
#include <bits/stdc++.h> #pragma GCC optimize ("Ofast", "unroll-loops") using namespace std; #define ll long long #define int ll #define FOR(i,a,b) for (int i = (a); i<(b); i++) #define REP(i,n) FOR(i,0,n) #define REP1(i,n) FOR(i,1,n+1) #define RREP(i,n) for (int i=(n)-1; i>=0; i--) #define RREP1(i, n) for (int i=(n); i>=1; i--) #define f first #define s second #define pb push_back #define ALL(x) x.begin(),x.end() #define SZ(x) (int)(x.size()) #define SQ(x) (x)*(x) #define pii pair<int, int> #define pip pair<int, pii> #define ppi pair<pii, int> #define ppp pair<pii, pii> #define pdd pair<double ,double> #define pcc pair<char, char> #define endl '\n' //#define TOAD #ifdef TOAD #define bug(x) cerr<<__LINE__<<": "<<#x<<" is "<<x<<endl #define IOS() #else #define bug(...) #define IOS() ios::sync_with_stdio(0), cin.tie(0), cout.tie(0) #endif const ll inf = 1ll<<60; const ll iinf = 2147483647; const ll mod = 998244353; const ll maxn=1e5+5; const double PI=acos(-1); ll pw(ll x, ll p, ll m=mod){ ll ret=1; while (p>0){ if (p&1){ ret*=x; ret%=m; } x*=x; x%=m; p>>=1; } return ret; } ll inv(ll a, ll m=mod){ return pw(a,m-2,m); } int n, m, k; set<pii> dp[maxn]; vector<int> graph[maxn]; vector<pii> froot(maxn); // first is time, second is weight void dfs(int x){ pii lg = {0, -1}; REP(i, SZ(graph[x])){ dfs(graph[x][i]); if (SZ(dp[graph[x][i]]) > lg.f) { lg.f = SZ(dp[graph[x][i]]); lg.s = i; } } if (lg.s != -1) dp[x].swap(dp[graph[x][lg.s]]); REP(i, SZ(graph[x])){ if (i == lg.s) continue; while(dp[graph[x][i]].size()){ dp[x].insert(*(dp[graph[x][i]].begin())); dp[graph[x][i]].erase(dp[graph[x][i]].begin()); } } if (froot[x].f == 0) return; if (SZ(dp[x])){ auto it = dp[x].upper_bound({froot[x].f+1, 0}); int cur = -(froot[x].s); while(it != dp[x].end()){ cur += (*it).s; if (cur > 0){ dp[x].insert({(*it).f, cur}); dp[x].erase(it); break; } it = dp[x].erase(it); } } dp[x].insert(froot[x]); } signed main(){ IOS(); cin>>n>>m>>k; FOR(i, 2, n+1){ int p; cin>>p; graph[p].pb(i); } REP(i, m){ int x; cin>>x; cin>>froot[x].f>>froot[x].s; } dfs(1); int ans = 0; while(dp[1].size()){ ans += (*(dp[1].begin())).s; dp[1].erase(dp[1].begin()); } cout<<ans<<endl; }
#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...