File tree Expand file tree Collapse file tree 1 file changed +47
-0
lines changed
Expand file tree Collapse file tree 1 file changed +47
-0
lines changed Original file line number Diff line number Diff line change 1+ // https://www.acmicpc.net/problem/14267
2+ #include < iostream>
3+ #include < vector>
4+ #include < algorithm>
5+ using namespace std ;
6+
7+ int n, m;
8+ int parent[100001 ];
9+ int res[100001 ];
10+ vector<vector<int >> graph;
11+
12+ void dfs (int n) {
13+ for (int i = 0 ; i < graph[n].size (); i++){
14+ int next = graph[n][i];
15+ res[next] += res[n];
16+ dfs (next);
17+ }
18+ }
19+
20+ int main (){
21+ ios_base::sync_with_stdio (false );
22+ cin.tie (NULL ); cout.tie (NULL );
23+
24+ cin >> n >> m;
25+ graph.resize (n + 1 );
26+
27+ int root = 1 ;
28+ for (int i = 1 ; i <= n; i++){
29+ cin >> parent[i];
30+ if (parent[i] == -1 ) root = i;
31+ else graph[parent[i]].push_back (i);
32+ }
33+ for (int i = 0 ; i < m; i++){
34+ int idx, w;
35+ cin >> idx >> w;
36+ res[idx] += w;
37+ }
38+
39+ dfs (root);
40+
41+ for (int i = 1 ; i <= n; i++){
42+ cout << res[i] << " " ;
43+ }
44+
45+
46+ return 0 ;
47+ }
You can’t perform that action at this time.
0 commit comments