-
Notifications
You must be signed in to change notification settings - Fork 0
/
14267.cpp
50 lines (46 loc) · 872 Bytes
/
14267.cpp
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
48
49
50
#include <stdio.h>
#include <vector>
#include <queue>
#include <utility>
using namespace std;
typedef pair<int, int> ii;
int n, m;
vector<vector<int>> g;
int arr[100001];
void dfs(int v) {
for (int next : g[v]) {
arr[next] += arr[v];
dfs(next);
}
}
int main() {
scanf("%d%d", &n, &m);
g.resize(n + 1);
for (int i = 1; i <= n; i++) {
int tmp; scanf("%d", &tmp);
if (tmp != -1) g[tmp].push_back(i);
}
for (int i = 0; i < m; i++) {
int a, b; scanf("%d%d", &a, &b);
arr[a] += b;
}
//dfs(1);
queue<int>q;
q.push(1);
while (!q.empty()) {
int Size = q.size();
for (int i = 0; i < Size; i++) {
int curr = q.front();
q.pop();
int und = g[curr].size();
for (int j = 0; j < und; j++) {
arr[g[curr][j]] += arr[curr];
q.push(g[curr][j]);
}
}
}
for (int i = 1; i <= n; i++)
printf("%d ", arr[i]);
printf("\n");
return 0;
}