#include<bits/stdc++.h>
#pragma GCC optimzie("unroll-loops,no-stack-ptorector,Ofast")
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<ll,ll> pll;
const ll N=1e5+5;
const ll MOD=1e9+7;
const ld pi=acos(-1);
const ll INF=(1LL<<60);
#define REP(i,n) for(int i=0;i<n;i++)
#define REP1(i,n) for(int i=1;i<=n;i++)
#define pb push_back
#define mp make_pair
#define X first
#define Y second
#define lwb lower_bound
#define ALL(_a) _a.begin(),_a.end()
#define SZ(_a) (ll)_a.size()
ll n,m,k,x,y,day[N],weight[N];
vector<ll> v[N];
ll dp[N];
map<ll,ll> mm[N];
ll mg(ll a,ll b){
if(SZ(mm[a])>SZ(mm[b]))swap(a,b);
for(auto i:mm[a])mm[b][i.X]+=i.Y;
return b;
}
void DFS(ll nd){
dp[nd]=nd;
for(ll i:v[nd]){
DFS(i);
//dp[nd]=mg(dp[nd],dp[i]);
dp[nd]=mg(dp[nd],dp[i]);
//REP(j,k+1)dp[nd][j]+=dp[i][j];
}
ll last=weight[nd];
vector<ll> era;
map<ll,ll> &DP=mm[dp[nd]];
if(DP.lwb(day[nd]+1)!=DP.end())for(int i=DP.lwb(day[nd]+1)->X;i<=k;i=DP.lwb(i+1)->X){
if(last==0)break;
if(last>DP[i]){
last-=DP[i];
era.pb(i);
}else{
DP[i]-=last;
last=0;
}
if(DP.lwb(i+1)==DP.end())break;
}
for(ll i:era)DP.erase(i);
DP[day[nd]]+=weight[nd];
//dp[nd][day[nd]]+=weight[nd];
//REP1(j,k)dp[nd][j]=max(dp[nd][j],dp[nd][j-1]);
}
int main(){
ios_base::sync_with_stdio(0);cin.tie(0);
cin>>n>>m>>k;
for(int i=2;i<=n;i++){
cin>>x;v[x].pb(i);
}
REP(i,m){
cin>>x>>y;
day[x]=y;
cin>>weight[x];
//for(int j=y+1;j<=k;j++)weight[x][j]=weight[x][y];
}
DFS(1);
/*REP1(i,n){
cout<<"Node::"<<i<<"\n";
REP(j,k+1){
cout<<setw(2)<<dp[i][j]<<" \n"[j==k];
}
}
*/
ll ans=0;
for(auto i:mm[dp[1]])ans+=i.Y;
cout<<ans<<"\n";
return 0;
}
Compilation message
magictree.cpp:2: warning: ignoring #pragma GCC optimzie [-Wunknown-pragmas]
2 | #pragma GCC optimzie("unroll-loops,no-stack-ptorector,Ofast")
|
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
7404 KB |
Output is correct |
2 |
Correct |
6 ms |
7404 KB |
Output is correct |
3 |
Correct |
6 ms |
7404 KB |
Output is correct |
4 |
Correct |
6 ms |
7404 KB |
Output is correct |
5 |
Correct |
6 ms |
7404 KB |
Output is correct |
6 |
Correct |
5 ms |
7404 KB |
Output is correct |
7 |
Correct |
5 ms |
7404 KB |
Output is correct |
8 |
Correct |
5 ms |
7404 KB |
Output is correct |
9 |
Correct |
5 ms |
7404 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
101 ms |
23132 KB |
Output is correct |
2 |
Correct |
61 ms |
21996 KB |
Output is correct |
3 |
Correct |
167 ms |
39552 KB |
Output is correct |
4 |
Correct |
123 ms |
22632 KB |
Output is correct |
5 |
Correct |
157 ms |
24300 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
7660 KB |
Output is correct |
2 |
Correct |
6 ms |
7660 KB |
Output is correct |
3 |
Correct |
6 ms |
7660 KB |
Output is correct |
4 |
Correct |
85 ms |
32108 KB |
Output is correct |
5 |
Correct |
86 ms |
35948 KB |
Output is correct |
6 |
Correct |
153 ms |
32108 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
98 ms |
18188 KB |
Output is correct |
2 |
Correct |
91 ms |
17260 KB |
Output is correct |
3 |
Correct |
73 ms |
22636 KB |
Output is correct |
4 |
Correct |
72 ms |
18536 KB |
Output is correct |
5 |
Correct |
62 ms |
32236 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
7404 KB |
Output is correct |
2 |
Correct |
6 ms |
7404 KB |
Output is correct |
3 |
Correct |
6 ms |
7404 KB |
Output is correct |
4 |
Correct |
6 ms |
7404 KB |
Output is correct |
5 |
Correct |
6 ms |
7404 KB |
Output is correct |
6 |
Correct |
5 ms |
7404 KB |
Output is correct |
7 |
Correct |
5 ms |
7404 KB |
Output is correct |
8 |
Correct |
5 ms |
7404 KB |
Output is correct |
9 |
Correct |
5 ms |
7404 KB |
Output is correct |
10 |
Correct |
119 ms |
21740 KB |
Output is correct |
11 |
Correct |
97 ms |
20204 KB |
Output is correct |
12 |
Correct |
80 ms |
21868 KB |
Output is correct |
13 |
Correct |
61 ms |
17896 KB |
Output is correct |
14 |
Correct |
65 ms |
31596 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
18 ms |
9196 KB |
Output is correct |
2 |
Correct |
63 ms |
15468 KB |
Output is correct |
3 |
Correct |
61 ms |
15488 KB |
Output is correct |
4 |
Correct |
59 ms |
15596 KB |
Output is correct |
5 |
Correct |
27 ms |
17000 KB |
Output is correct |
6 |
Correct |
54 ms |
19308 KB |
Output is correct |
7 |
Correct |
45 ms |
21100 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
7404 KB |
Output is correct |
2 |
Correct |
6 ms |
7404 KB |
Output is correct |
3 |
Correct |
6 ms |
7404 KB |
Output is correct |
4 |
Correct |
6 ms |
7404 KB |
Output is correct |
5 |
Correct |
6 ms |
7404 KB |
Output is correct |
6 |
Correct |
5 ms |
7404 KB |
Output is correct |
7 |
Correct |
5 ms |
7404 KB |
Output is correct |
8 |
Correct |
5 ms |
7404 KB |
Output is correct |
9 |
Correct |
5 ms |
7404 KB |
Output is correct |
10 |
Correct |
6 ms |
7660 KB |
Output is correct |
11 |
Correct |
6 ms |
7660 KB |
Output is correct |
12 |
Correct |
6 ms |
7660 KB |
Output is correct |
13 |
Correct |
85 ms |
32108 KB |
Output is correct |
14 |
Correct |
86 ms |
35948 KB |
Output is correct |
15 |
Correct |
153 ms |
32108 KB |
Output is correct |
16 |
Correct |
119 ms |
21740 KB |
Output is correct |
17 |
Correct |
97 ms |
20204 KB |
Output is correct |
18 |
Correct |
80 ms |
21868 KB |
Output is correct |
19 |
Correct |
61 ms |
17896 KB |
Output is correct |
20 |
Correct |
65 ms |
31596 KB |
Output is correct |
21 |
Correct |
29 ms |
12012 KB |
Output is correct |
22 |
Correct |
111 ms |
24556 KB |
Output is correct |
23 |
Correct |
138 ms |
28780 KB |
Output is correct |
24 |
Correct |
225 ms |
39020 KB |
Output is correct |
25 |
Correct |
113 ms |
21992 KB |
Output is correct |
26 |
Correct |
158 ms |
25324 KB |
Output is correct |
27 |
Correct |
159 ms |
24636 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
7404 KB |
Output is correct |
2 |
Correct |
6 ms |
7404 KB |
Output is correct |
3 |
Correct |
6 ms |
7404 KB |
Output is correct |
4 |
Correct |
6 ms |
7404 KB |
Output is correct |
5 |
Correct |
6 ms |
7404 KB |
Output is correct |
6 |
Correct |
5 ms |
7404 KB |
Output is correct |
7 |
Correct |
5 ms |
7404 KB |
Output is correct |
8 |
Correct |
5 ms |
7404 KB |
Output is correct |
9 |
Correct |
5 ms |
7404 KB |
Output is correct |
10 |
Correct |
101 ms |
23132 KB |
Output is correct |
11 |
Correct |
61 ms |
21996 KB |
Output is correct |
12 |
Correct |
167 ms |
39552 KB |
Output is correct |
13 |
Correct |
123 ms |
22632 KB |
Output is correct |
14 |
Correct |
157 ms |
24300 KB |
Output is correct |
15 |
Correct |
6 ms |
7660 KB |
Output is correct |
16 |
Correct |
6 ms |
7660 KB |
Output is correct |
17 |
Correct |
6 ms |
7660 KB |
Output is correct |
18 |
Correct |
85 ms |
32108 KB |
Output is correct |
19 |
Correct |
86 ms |
35948 KB |
Output is correct |
20 |
Correct |
153 ms |
32108 KB |
Output is correct |
21 |
Correct |
98 ms |
18188 KB |
Output is correct |
22 |
Correct |
91 ms |
17260 KB |
Output is correct |
23 |
Correct |
73 ms |
22636 KB |
Output is correct |
24 |
Correct |
72 ms |
18536 KB |
Output is correct |
25 |
Correct |
62 ms |
32236 KB |
Output is correct |
26 |
Correct |
119 ms |
21740 KB |
Output is correct |
27 |
Correct |
97 ms |
20204 KB |
Output is correct |
28 |
Correct |
80 ms |
21868 KB |
Output is correct |
29 |
Correct |
61 ms |
17896 KB |
Output is correct |
30 |
Correct |
65 ms |
31596 KB |
Output is correct |
31 |
Correct |
18 ms |
9196 KB |
Output is correct |
32 |
Correct |
63 ms |
15468 KB |
Output is correct |
33 |
Correct |
61 ms |
15488 KB |
Output is correct |
34 |
Correct |
59 ms |
15596 KB |
Output is correct |
35 |
Correct |
27 ms |
17000 KB |
Output is correct |
36 |
Correct |
54 ms |
19308 KB |
Output is correct |
37 |
Correct |
45 ms |
21100 KB |
Output is correct |
38 |
Correct |
29 ms |
12012 KB |
Output is correct |
39 |
Correct |
111 ms |
24556 KB |
Output is correct |
40 |
Correct |
138 ms |
28780 KB |
Output is correct |
41 |
Correct |
225 ms |
39020 KB |
Output is correct |
42 |
Correct |
113 ms |
21992 KB |
Output is correct |
43 |
Correct |
158 ms |
25324 KB |
Output is correct |
44 |
Correct |
159 ms |
24636 KB |
Output is correct |
45 |
Correct |
38 ms |
11756 KB |
Output is correct |
46 |
Correct |
136 ms |
24044 KB |
Output is correct |
47 |
Correct |
123 ms |
27500 KB |
Output is correct |
48 |
Correct |
182 ms |
36076 KB |
Output is correct |
49 |
Correct |
107 ms |
22604 KB |
Output is correct |
50 |
Correct |
170 ms |
25324 KB |
Output is correct |
51 |
Correct |
172 ms |
24812 KB |
Output is correct |