제출 #834357

#제출 시각아이디문제언어결과실행 시간메모리
834357vqpahmadMagic Tree (CEOI19_magictree)C++14
100 / 100
163 ms107980 KiB
#include<bits/stdc++.h>
using namespace std;
#define int long long 
#define ll long long
#define pii pair<int,int>
#define F first
#define S second
#define endl '\n'
#define pb push_back
#define sz(a) (int)a.size()
#define all(a) a.begin(),a.end()
const int mod = 1e9 + 7;
const int N = 1e6 + 15;
const ll inf = 1e12;
vector<int> adj[N];
int d[N], w[N];
map<int,int> mp[N];
int ans = 0;
void dfs(int node){
	for (auto to : adj[node]){
		dfs(to);
		if (sz(mp[to]) > sz(mp[node])){
			swap(mp[to], mp[node]);
		}
		for (auto it : mp[to]){
			mp[node][it.F] += it.S;
		}
	}
	int cur = w[node];
	while (cur){
		auto up = mp[node].upper_bound(d[node]);
		int x = min(cur, up->second);
		cur -= x;
		mp[node][d[node]] += x;
		mp[node][up->first] -= x;
		if (cur >= up->second){
			mp[node].erase(up);
		}
	}
	if (node==1){
		for (auto it : mp[node]){
			if (it.first == inf) {
				break;
			}
			ans += it.S;
		}
	}
	return;
}
int32_t main(){
	ios_base::sync_with_stdio(0);cin.tie(0);
	int n, m, k;
	cin >> n >> m >> k;
	for (int i=1;i<=n;i++) mp[i][inf] = inf;
	for (int i=2;i<=n;i++){
		int u;
		cin >> u;
		adj[u].pb(i);
	}
	for (int i=0;i<m;i++){
		int node;
		cin >> node;
		cin >> d[node] >> w[node];
	}
	dfs(1);
	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...