#include <bits/stdc++.h>
using namespace std;
#define fr first
#define sc second
#define ll long long
const int mxn=1e5+5;
int n,m,k,par[mxn],comp[mxn];
pair<int,int>f[mxn];
set<pair<int,ll>>s[mxn];
vector<int>v[mxn];
void up(int z,int x){
ll w=f[z].sc,d=f[z].fr,res=0;
auto y=s[x].lower_bound({d,(ll)-1e18});
if(y!=s[x].end() && y->fr==d){
s[x].erase(y);
res=y->sc;
}
s[x].insert({d,res+w});
while(w>0){
y=s[x].upper_bound({d+1,(ll)-1e18});
if(y==s[x].end())
break;
ll res=y->sc,dd=y->fr;
s[x].erase(y);
if(res>w)
s[x].insert({dd,res-w});
w-=res;
}
}
void dsu(int x,int y){
for(auto i:s[y]){
auto w=s[x].lower_bound({i.fr,(ll)-1e18});
ll res=0;
if(w !=s[x].end() && w->fr==i.fr){
s[x].erase(w);
res+=w->sc;
}
s[x].insert({i.fr,i.sc+res});
}
}
void dfs(int z){
comp[z]=z;
for(auto i:v[z]){
if(i!=par[z]){
dfs(i);
if(s[comp[i]].size()>s[comp[z]].size())
comp[z]=comp[i];
}
}
for(auto i:v[z]){
if(i!=par[z] && comp[z]!=comp[i]){
dsu(comp[z],comp[i]);
}
}
up(z,comp[z]);
}
int main(){
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin>>n>>m>>k;
for(int i=2;i<=n;i++){
cin>>par[i];
v[par[i]].push_back(i);
}
for(int i=1;i<=m;i++){
int x,y,w;
cin>>x>>y>>w;
f[x]={y,w};
}
dfs(1);
ll ans=0;
for(auto i:s[comp[1]])
ans+=i.sc;
cout<<ans<<'\n';
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
8792 KB |
Output is correct |
2 |
Correct |
2 ms |
8792 KB |
Output is correct |
3 |
Correct |
3 ms |
8792 KB |
Output is correct |
4 |
Correct |
2 ms |
8792 KB |
Output is correct |
5 |
Correct |
2 ms |
8796 KB |
Output is correct |
6 |
Correct |
2 ms |
8796 KB |
Output is correct |
7 |
Correct |
2 ms |
8796 KB |
Output is correct |
8 |
Correct |
2 ms |
8796 KB |
Output is correct |
9 |
Correct |
2 ms |
8796 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
70 ms |
21812 KB |
Output is correct |
2 |
Correct |
42 ms |
18512 KB |
Output is correct |
3 |
Correct |
141 ms |
36948 KB |
Output is correct |
4 |
Correct |
65 ms |
21456 KB |
Output is correct |
5 |
Correct |
89 ms |
22184 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
8844 KB |
Output is correct |
2 |
Correct |
2 ms |
8792 KB |
Output is correct |
3 |
Correct |
2 ms |
8796 KB |
Output is correct |
4 |
Correct |
42 ms |
21844 KB |
Output is correct |
5 |
Correct |
72 ms |
26036 KB |
Output is correct |
6 |
Correct |
56 ms |
21724 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
52 ms |
17152 KB |
Output is correct |
2 |
Correct |
53 ms |
16280 KB |
Output is correct |
3 |
Correct |
43 ms |
18004 KB |
Output is correct |
4 |
Correct |
37 ms |
17368 KB |
Output is correct |
5 |
Correct |
42 ms |
22148 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
8792 KB |
Output is correct |
2 |
Correct |
2 ms |
8792 KB |
Output is correct |
3 |
Correct |
3 ms |
8792 KB |
Output is correct |
4 |
Correct |
2 ms |
8792 KB |
Output is correct |
5 |
Correct |
2 ms |
8796 KB |
Output is correct |
6 |
Correct |
2 ms |
8796 KB |
Output is correct |
7 |
Correct |
2 ms |
8796 KB |
Output is correct |
8 |
Correct |
2 ms |
8796 KB |
Output is correct |
9 |
Correct |
2 ms |
8796 KB |
Output is correct |
10 |
Correct |
78 ms |
19288 KB |
Output is correct |
11 |
Correct |
53 ms |
18260 KB |
Output is correct |
12 |
Correct |
51 ms |
17232 KB |
Output is correct |
13 |
Correct |
35 ms |
16844 KB |
Output is correct |
14 |
Correct |
46 ms |
21252 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
10076 KB |
Output is correct |
2 |
Correct |
30 ms |
14388 KB |
Output is correct |
3 |
Correct |
43 ms |
14420 KB |
Output is correct |
4 |
Correct |
38 ms |
14532 KB |
Output is correct |
5 |
Correct |
20 ms |
15972 KB |
Output is correct |
6 |
Correct |
30 ms |
16212 KB |
Output is correct |
7 |
Correct |
24 ms |
16464 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
8792 KB |
Output is correct |
2 |
Correct |
2 ms |
8792 KB |
Output is correct |
3 |
Correct |
3 ms |
8792 KB |
Output is correct |
4 |
Correct |
2 ms |
8792 KB |
Output is correct |
5 |
Correct |
2 ms |
8796 KB |
Output is correct |
6 |
Correct |
2 ms |
8796 KB |
Output is correct |
7 |
Correct |
2 ms |
8796 KB |
Output is correct |
8 |
Correct |
2 ms |
8796 KB |
Output is correct |
9 |
Correct |
2 ms |
8796 KB |
Output is correct |
10 |
Correct |
3 ms |
8844 KB |
Output is correct |
11 |
Correct |
2 ms |
8792 KB |
Output is correct |
12 |
Correct |
2 ms |
8796 KB |
Output is correct |
13 |
Correct |
42 ms |
21844 KB |
Output is correct |
14 |
Correct |
72 ms |
26036 KB |
Output is correct |
15 |
Correct |
56 ms |
21724 KB |
Output is correct |
16 |
Correct |
78 ms |
19288 KB |
Output is correct |
17 |
Correct |
53 ms |
18260 KB |
Output is correct |
18 |
Correct |
51 ms |
17232 KB |
Output is correct |
19 |
Correct |
35 ms |
16844 KB |
Output is correct |
20 |
Correct |
46 ms |
21252 KB |
Output is correct |
21 |
Correct |
17 ms |
12124 KB |
Output is correct |
22 |
Correct |
84 ms |
21820 KB |
Output is correct |
23 |
Correct |
86 ms |
24940 KB |
Output is correct |
24 |
Correct |
112 ms |
31568 KB |
Output is correct |
25 |
Correct |
60 ms |
20692 KB |
Output is correct |
26 |
Correct |
84 ms |
21404 KB |
Output is correct |
27 |
Correct |
74 ms |
19268 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
8792 KB |
Output is correct |
2 |
Correct |
2 ms |
8792 KB |
Output is correct |
3 |
Correct |
3 ms |
8792 KB |
Output is correct |
4 |
Correct |
2 ms |
8792 KB |
Output is correct |
5 |
Correct |
2 ms |
8796 KB |
Output is correct |
6 |
Correct |
2 ms |
8796 KB |
Output is correct |
7 |
Correct |
2 ms |
8796 KB |
Output is correct |
8 |
Correct |
2 ms |
8796 KB |
Output is correct |
9 |
Correct |
2 ms |
8796 KB |
Output is correct |
10 |
Correct |
70 ms |
21812 KB |
Output is correct |
11 |
Correct |
42 ms |
18512 KB |
Output is correct |
12 |
Correct |
141 ms |
36948 KB |
Output is correct |
13 |
Correct |
65 ms |
21456 KB |
Output is correct |
14 |
Correct |
89 ms |
22184 KB |
Output is correct |
15 |
Correct |
3 ms |
8844 KB |
Output is correct |
16 |
Correct |
2 ms |
8792 KB |
Output is correct |
17 |
Correct |
2 ms |
8796 KB |
Output is correct |
18 |
Correct |
42 ms |
21844 KB |
Output is correct |
19 |
Correct |
72 ms |
26036 KB |
Output is correct |
20 |
Correct |
56 ms |
21724 KB |
Output is correct |
21 |
Correct |
52 ms |
17152 KB |
Output is correct |
22 |
Correct |
53 ms |
16280 KB |
Output is correct |
23 |
Correct |
43 ms |
18004 KB |
Output is correct |
24 |
Correct |
37 ms |
17368 KB |
Output is correct |
25 |
Correct |
42 ms |
22148 KB |
Output is correct |
26 |
Correct |
78 ms |
19288 KB |
Output is correct |
27 |
Correct |
53 ms |
18260 KB |
Output is correct |
28 |
Correct |
51 ms |
17232 KB |
Output is correct |
29 |
Correct |
35 ms |
16844 KB |
Output is correct |
30 |
Correct |
46 ms |
21252 KB |
Output is correct |
31 |
Correct |
7 ms |
10076 KB |
Output is correct |
32 |
Correct |
30 ms |
14388 KB |
Output is correct |
33 |
Correct |
43 ms |
14420 KB |
Output is correct |
34 |
Correct |
38 ms |
14532 KB |
Output is correct |
35 |
Correct |
20 ms |
15972 KB |
Output is correct |
36 |
Correct |
30 ms |
16212 KB |
Output is correct |
37 |
Correct |
24 ms |
16464 KB |
Output is correct |
38 |
Correct |
17 ms |
12124 KB |
Output is correct |
39 |
Correct |
84 ms |
21820 KB |
Output is correct |
40 |
Correct |
86 ms |
24940 KB |
Output is correct |
41 |
Correct |
112 ms |
31568 KB |
Output is correct |
42 |
Correct |
60 ms |
20692 KB |
Output is correct |
43 |
Correct |
84 ms |
21404 KB |
Output is correct |
44 |
Correct |
74 ms |
19268 KB |
Output is correct |
45 |
Correct |
21 ms |
12440 KB |
Output is correct |
46 |
Correct |
79 ms |
22836 KB |
Output is correct |
47 |
Correct |
93 ms |
26132 KB |
Output is correct |
48 |
Correct |
133 ms |
34640 KB |
Output is correct |
49 |
Correct |
65 ms |
21456 KB |
Output is correct |
50 |
Correct |
99 ms |
22356 KB |
Output is correct |
51 |
Correct |
81 ms |
20048 KB |
Output is correct |