Submission #477987

#TimeUsernameProblemLanguageResultExecution timeMemory
477987zerverMagic Tree (CEOI19_magictree)C++14
100 / 100
156 ms33416 KiB
#include <bits/stdc++.h> #define FAST_IO ios::sync_with_stdio(0); cin.tie(nullptr) #define rall(x) (x).rbegin(), (x).rend() #define all(x) (x).begin(), (x).end() #define sz(v) int(v.size()) #define ss second #define ff first #define endl '\n' using namespace std; typedef long long Long; typedef pair<int, int> ii; const Long INF = 1e13; const int N = 1e5 + 7; vector<int> adj[N]; multiset<ii> dif[N]; int dia[N], peso[N], pt[N]; void dfs(int from){ int big = 0; for(int to: adj[from]){ dfs(to); if(big == 0 || sz(dif[pt[to]]) > sz(dif[pt[big]])) big = to; } if(big == 0){ if(dia[from] == 0) return; dif[pt[from]].insert({dia[from], peso[from]}); return; } pt[from] = pt[big]; for(int to: adj[from]){ if(to == big) continue; for(auto p: dif[pt[to]]) dif[pt[from]].insert(p); } if(dia[from] != 0){ ii par = {dia[from] + 1, 0}; auto it = dif[pt[from]].lower_bound(par); ii ins = {dia[from], peso[from]}; ii gua = ins; for(; it != dif[pt[from]].end(); ){ ii npar = *(it); if(npar.ss <= ins.ss){ ins.ss -= npar.ss; it = dif[pt[from]].erase(it); } else{ npar.ss -= ins.ss; dif[pt[from]].erase(it); dif[pt[from]].insert(npar); break; } } dif[pt[from]].insert(gua); } } void solve(){ int n, m, k; cin >> n >> m >> k; for(int i = 2; i <= n; i++){ int p; cin >> p; adj[p].push_back(i); } for(int i = 0; i < m; i++){ int v; cin >> v; cin >> dia[v] >> peso[v]; } for(int i = 1; i <= n; i++) pt[i] = i; dfs(1); Long ans = 0; for(auto p: dif[pt[1]]) ans += p.ss; cout << ans << endl; } int main(){ FAST_IO; int tt = 1; // cin >> tt; while(tt--) solve(); 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...