[BOJ] 14267 회사 문화1

회사 문화1

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
#include <bits/stdc++.h>
using namespace std;
void helper(int u, vector<int>& res, int sum, vector<int>& me, vector<vector<int>>& adj) {
res[u] = sum += me[u];
for(auto& v : adj[u])
helper(v, res, sum, me, adj);
}
vector<int> solve(vector<int>& p, vector<int>& a) {
int n = p.size(), root;
vector<int> res(p.size());
vector<vector<int>> adj(n);

for(int i = 0; i < p.size(); i++)
if(p[i] == -1) root = i;
else adj[p[i]].push_back(i);
helper(root, res, 0, a, adj);
return res;
}

int main() {
int n, m;
cin>>n>>m;
vector<int> p(n), a(n);
for(int i = 0; i < n; i++) {
cin>>p[i];
if(p[i] != -1)
p[i]--;
}
for(int i = 0, w, num; i < m; i++) {
cin>>w>>num;
a[w-1] += num;
}
auto res = solve(p,a);
for(auto& n : res) cout<<n<<' ';
return 0;
}
Author: Song Hayoung
Link: https://songhayoung.github.io/2022/04/19/PS/BOJ/14267/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.