#include<bits/stdc++.h>
#define ll long long
using namespace std;
const int max_N=1e5+5;
map<int,ll> update[max_N];
vector<int> stablo[max_N],Brisi;
int N,M,K,rod[max_N],vred[max_N],kad[max_N];
void dfs(int gde){
if(stablo[gde].size()==0){
if(kad[gde]==-1)
return;
update[gde][kad[gde]]=vred[gde];
return;
}
for(int i=0;i<stablo[gde].size();i++)
dfs(stablo[gde][i]);
int najv=stablo[gde][0];
for(int i=1;i<stablo[gde].size();i++)
if(update[stablo[gde][i]].size()>update[najv].size())
najv=stablo[gde][i];
swap(update[gde],update[najv]);
for(int i=0;i<stablo[gde].size();i++){
if(stablo[gde][i]==najv)
continue;
int koji=stablo[gde][i];
for(auto it=update[koji].begin();it!=update[koji].end();it++)
update[gde][it->first]+=it->second;
}
if(kad[gde]!=-1){
update[gde][kad[gde]]+=vred[gde];
auto it=update[gde].upper_bound(kad[gde]);
ll vel=vred[gde];
Brisi.clear();
Brisi.shrink_to_fit();
for(it;it!=update[gde].end();it++){
if(it->second<=vel){
vel-=it->second;
Brisi.push_back(it->first);
}
else{
it->second-=vel;
break;
}
}
for(int i=0;i<Brisi.size();i++)
update[gde].erase(Brisi[i]);
}
for(int i=0;i<stablo[gde].size();i++){
update[stablo[gde][i]].clear();
}
return;
}
int main(){
ios_base::sync_with_stdio(false);
cin.tie(0);
cin>>N>>M>>K;
for(int i=2;i<=N;i++){
cin>>rod[i];
stablo[rod[i]].push_back(i);
}
for(int i=1;i<=N;i++)
kad[i]=-1;
int a;
for(int i=1;i<=M;i++){
cin>>a;
cin>>kad[a]>>vred[a];
}
dfs(1);
ll res=0;
for(auto it=update[1].begin();it!=update[1].end();it++)
res+=it->second;
cout<<res<<"\n";
return 0;
}
Compilation message
magictree.cpp: In function 'void dfs(int)':
magictree.cpp:15:17: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
15 | for(int i=0;i<stablo[gde].size();i++)
| ~^~~~~~~~~~~~~~~~~~~
magictree.cpp:18:17: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
18 | for(int i=1;i<stablo[gde].size();i++)
| ~^~~~~~~~~~~~~~~~~~~
magictree.cpp:22:17: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
22 | for(int i=0;i<stablo[gde].size();i++){
| ~^~~~~~~~~~~~~~~~~~~
magictree.cpp:35:11: warning: statement has no effect [-Wunused-value]
35 | for(it;it!=update[gde].end();it++){
| ^~
magictree.cpp:45:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
45 | for(int i=0;i<Brisi.size();i++)
| ~^~~~~~~~~~~~~
magictree.cpp:48:17: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
48 | for(int i=0;i<stablo[gde].size();i++){
| ~^~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
7380 KB |
Output is correct |
2 |
Correct |
4 ms |
7380 KB |
Output is correct |
3 |
Correct |
3 ms |
7252 KB |
Output is correct |
4 |
Correct |
3 ms |
7368 KB |
Output is correct |
5 |
Correct |
3 ms |
7372 KB |
Output is correct |
6 |
Correct |
3 ms |
7252 KB |
Output is correct |
7 |
Correct |
3 ms |
7380 KB |
Output is correct |
8 |
Correct |
4 ms |
7380 KB |
Output is correct |
9 |
Correct |
3 ms |
7380 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
41 ms |
12840 KB |
Output is correct |
2 |
Correct |
30 ms |
18040 KB |
Output is correct |
3 |
Correct |
89 ms |
16848 KB |
Output is correct |
4 |
Correct |
59 ms |
19532 KB |
Output is correct |
5 |
Correct |
65 ms |
15508 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
7508 KB |
Output is correct |
2 |
Correct |
4 ms |
7508 KB |
Output is correct |
3 |
Correct |
3 ms |
7516 KB |
Output is correct |
4 |
Correct |
42 ms |
24840 KB |
Output is correct |
5 |
Correct |
58 ms |
28904 KB |
Output is correct |
6 |
Correct |
58 ms |
24908 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
48 ms |
10916 KB |
Output is correct |
2 |
Correct |
46 ms |
10876 KB |
Output is correct |
3 |
Correct |
41 ms |
16732 KB |
Output is correct |
4 |
Correct |
32 ms |
15296 KB |
Output is correct |
5 |
Correct |
40 ms |
24920 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
7380 KB |
Output is correct |
2 |
Correct |
4 ms |
7380 KB |
Output is correct |
3 |
Correct |
3 ms |
7252 KB |
Output is correct |
4 |
Correct |
3 ms |
7368 KB |
Output is correct |
5 |
Correct |
3 ms |
7372 KB |
Output is correct |
6 |
Correct |
3 ms |
7252 KB |
Output is correct |
7 |
Correct |
3 ms |
7380 KB |
Output is correct |
8 |
Correct |
4 ms |
7380 KB |
Output is correct |
9 |
Correct |
3 ms |
7380 KB |
Output is correct |
10 |
Correct |
52 ms |
10824 KB |
Output is correct |
11 |
Correct |
50 ms |
10916 KB |
Output is correct |
12 |
Correct |
40 ms |
16768 KB |
Output is correct |
13 |
Correct |
32 ms |
15292 KB |
Output is correct |
14 |
Correct |
38 ms |
24860 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
8020 KB |
Output is correct |
2 |
Correct |
26 ms |
10716 KB |
Output is correct |
3 |
Correct |
21 ms |
10708 KB |
Output is correct |
4 |
Correct |
22 ms |
10692 KB |
Output is correct |
5 |
Correct |
11 ms |
9184 KB |
Output is correct |
6 |
Correct |
20 ms |
13020 KB |
Output is correct |
7 |
Correct |
20 ms |
16652 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
7380 KB |
Output is correct |
2 |
Correct |
4 ms |
7380 KB |
Output is correct |
3 |
Correct |
3 ms |
7252 KB |
Output is correct |
4 |
Correct |
3 ms |
7368 KB |
Output is correct |
5 |
Correct |
3 ms |
7372 KB |
Output is correct |
6 |
Correct |
3 ms |
7252 KB |
Output is correct |
7 |
Correct |
3 ms |
7380 KB |
Output is correct |
8 |
Correct |
4 ms |
7380 KB |
Output is correct |
9 |
Correct |
3 ms |
7380 KB |
Output is correct |
10 |
Correct |
4 ms |
7508 KB |
Output is correct |
11 |
Correct |
4 ms |
7508 KB |
Output is correct |
12 |
Correct |
3 ms |
7516 KB |
Output is correct |
13 |
Correct |
42 ms |
24840 KB |
Output is correct |
14 |
Correct |
58 ms |
28904 KB |
Output is correct |
15 |
Correct |
58 ms |
24908 KB |
Output is correct |
16 |
Correct |
52 ms |
10824 KB |
Output is correct |
17 |
Correct |
50 ms |
10916 KB |
Output is correct |
18 |
Correct |
40 ms |
16768 KB |
Output is correct |
19 |
Correct |
32 ms |
15292 KB |
Output is correct |
20 |
Correct |
38 ms |
24860 KB |
Output is correct |
21 |
Correct |
15 ms |
8504 KB |
Output is correct |
22 |
Correct |
54 ms |
11484 KB |
Output is correct |
23 |
Correct |
61 ms |
13384 KB |
Output is correct |
24 |
Correct |
93 ms |
15280 KB |
Output is correct |
25 |
Correct |
53 ms |
18996 KB |
Output is correct |
26 |
Correct |
78 ms |
17108 KB |
Output is correct |
27 |
Correct |
69 ms |
18272 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
7380 KB |
Output is correct |
2 |
Correct |
4 ms |
7380 KB |
Output is correct |
3 |
Correct |
3 ms |
7252 KB |
Output is correct |
4 |
Correct |
3 ms |
7368 KB |
Output is correct |
5 |
Correct |
3 ms |
7372 KB |
Output is correct |
6 |
Correct |
3 ms |
7252 KB |
Output is correct |
7 |
Correct |
3 ms |
7380 KB |
Output is correct |
8 |
Correct |
4 ms |
7380 KB |
Output is correct |
9 |
Correct |
3 ms |
7380 KB |
Output is correct |
10 |
Correct |
41 ms |
12840 KB |
Output is correct |
11 |
Correct |
30 ms |
18040 KB |
Output is correct |
12 |
Correct |
89 ms |
16848 KB |
Output is correct |
13 |
Correct |
59 ms |
19532 KB |
Output is correct |
14 |
Correct |
65 ms |
15508 KB |
Output is correct |
15 |
Correct |
4 ms |
7508 KB |
Output is correct |
16 |
Correct |
4 ms |
7508 KB |
Output is correct |
17 |
Correct |
3 ms |
7516 KB |
Output is correct |
18 |
Correct |
42 ms |
24840 KB |
Output is correct |
19 |
Correct |
58 ms |
28904 KB |
Output is correct |
20 |
Correct |
58 ms |
24908 KB |
Output is correct |
21 |
Correct |
48 ms |
10916 KB |
Output is correct |
22 |
Correct |
46 ms |
10876 KB |
Output is correct |
23 |
Correct |
41 ms |
16732 KB |
Output is correct |
24 |
Correct |
32 ms |
15296 KB |
Output is correct |
25 |
Correct |
40 ms |
24920 KB |
Output is correct |
26 |
Correct |
52 ms |
10824 KB |
Output is correct |
27 |
Correct |
50 ms |
10916 KB |
Output is correct |
28 |
Correct |
40 ms |
16768 KB |
Output is correct |
29 |
Correct |
32 ms |
15292 KB |
Output is correct |
30 |
Correct |
38 ms |
24860 KB |
Output is correct |
31 |
Correct |
7 ms |
8020 KB |
Output is correct |
32 |
Correct |
26 ms |
10716 KB |
Output is correct |
33 |
Correct |
21 ms |
10708 KB |
Output is correct |
34 |
Correct |
22 ms |
10692 KB |
Output is correct |
35 |
Correct |
11 ms |
9184 KB |
Output is correct |
36 |
Correct |
20 ms |
13020 KB |
Output is correct |
37 |
Correct |
20 ms |
16652 KB |
Output is correct |
38 |
Correct |
15 ms |
8504 KB |
Output is correct |
39 |
Correct |
54 ms |
11484 KB |
Output is correct |
40 |
Correct |
61 ms |
13384 KB |
Output is correct |
41 |
Correct |
93 ms |
15280 KB |
Output is correct |
42 |
Correct |
53 ms |
18996 KB |
Output is correct |
43 |
Correct |
78 ms |
17108 KB |
Output is correct |
44 |
Correct |
69 ms |
18272 KB |
Output is correct |
45 |
Correct |
16 ms |
8780 KB |
Output is correct |
46 |
Correct |
62 ms |
11532 KB |
Output is correct |
47 |
Correct |
61 ms |
13564 KB |
Output is correct |
48 |
Correct |
110 ms |
15896 KB |
Output is correct |
49 |
Correct |
56 ms |
19532 KB |
Output is correct |
50 |
Correct |
83 ms |
17848 KB |
Output is correct |
51 |
Correct |
79 ms |
19016 KB |
Output is correct |