[Link] https://www.acmicpc.net/problem/14267
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#include <algorithm>
#include <iostream>
#include <vector>
#include <map>
#include <set>
#include <queue>
// #include <bits/stdc++.h>
using namespace std;
#define for0(i, n) for(int i = 0; i < n; i++)
#define for1(i, n) for(int i = 1; i < n; i++)
#define fori(s, e) for(int i = s; i < e; i++)
#define endl "\n"
typedef long long ll;
const int INF = 987654321;
const int MAX = 100;
int n, m, ans = 0;
vector<vector<int>> sub;
vector<int> sum;
void propagate(int idx, int inc) {
sum[idx] += inc;
for(int sub_idx : sub[idx]) {
propagate(sub_idx, sum[idx]);
}
}
int main() {
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
cin >> n >> m;
sub.resize(n);
sum.resize(n);
fill(sum.begin(), sum.end(), 0);
for0(i, n) {
int super; cin >> super;
if(super == -1) continue;
super--;
sub[super].push_back(i);
}
for0(i, m) {
int emp, compli; cin >> emp >> compli;
sum[--emp] += compli;
}
propagate(0, 0);
for(int v : sum) cout << v << " ";
return 0;
}