Skip to content

Commit 4adb675

Browse files
authored
[BOJ] 14267 회사 문사 1 (G4)
1 parent 066aea4 commit 4adb675

File tree

1 file changed

+47
-0
lines changed

1 file changed

+47
-0
lines changed

박예진/0주차/DFS/14267.cpp

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
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+
}

0 commit comments

Comments
 (0)