Skip to content

Instantly share code, notes, and snippets.

@degurii
Created July 25, 2018 18:22
#include <iostream>
#include <vector>
using namespace std;
int n, m, lazy[100001];
vector<vector<int>> p;
void dfs(int now) {
for (int next : p[now]) {
lazy[next] += lazy[now];
dfs(next);
}
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cin >> n >> m;
p.resize(n + 1);
int x; cin >> x;
for (int i = 2; i < n + 1; i++) {
cin >> x;
p[x].push_back(i);
}
int y;
for (int i = 0; i < m; i++) {
cin >> x >> y;
lazy[x] += y;
}
dfs(1);
for (int i = 1; i < n + 1; i++) {
cout << lazy[i] << ' ';
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment