#include <bits/stdc++.h>
//#pragma GCC optimize("O3")
using namespace std;
typedef long long ll;
ll n,m,k,par[100005],niv[100005],dp[100005][3],val[100005],day[100005],minim[100005];
vector<int> muchii[100005];
struct date
{
ll nod,day,val;
} v[100005];
void dfscontrol(int nod)
{
if(nod==1)
niv[nod]=1;
for(int i=0;i<muchii[nod].size();i++)
{
int fiu=muchii[nod][i];
par[fiu]=nod;
niv[fiu]=niv[nod]+1;
dfscontrol(fiu);
}
}
bool comp(date a, date b)
{
if(a.day!=b.day)
return a.day<b.day;
if(niv[a.nod]!=niv[b.nod])
return niv[a.nod]>niv[b.nod];
return 0;
}
bool comp1(date a, date b)
{
return a.nod>b.nod;
}
void solve3()
{
sort(v+1,v+m+1,comp1);
for(int i=1;i<=1e5;i++)
minim[i]=1e9;
int ans=0;
for(int i=1;i<=m;i++)
{
int val=v[i].day;
int st=0;
int dr=i-1;
int best=0;
while(st<=dr)
{
int mij=(st+dr)/2;
if(minim[mij]<=val)
{
best=mij;
st=mij+1;
}
else
dr=mij-1;
}
ans=max(ans,best+1);
minim[best+1]=min(minim[best+1],val*1LL);
}
cout<<ans<<'\n';
}
void dfs(int nod)
{
for(int i=0;i<muchii[nod].size();i++)
{
int fiu=muchii[nod][i];
dfs(fiu);
}
if(val[nod]!=0&&day[nod]!=0)
for(int i=day[nod];i<=day[nod];i++)
dp[nod][i]+=val[nod];
for(int i=0;i<muchii[nod].size();i++)
{
int fiu=muchii[nod][i];
for(int j=1;j<=k;j++)
dp[nod][j]+=dp[fiu][j];
}
dp[nod][2]=max(dp[nod][1],dp[nod][2]);
}
void solve4()
{
for(int i=1;i<=m;i++)
{
int nod=v[i].nod;
day[nod]=v[i].day;
val[nod]=v[i].val;
}
dfs(1);
cout<<max(dp[1][1],dp[1][k])<<'\n';
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(0);
cin>>n>>m>>k;
bool is3=1;
for(int i=2;i<=n;i++)
{
int p;
cin>>p;
par[i]=p;
if(par[i]!=i-1)
is3=0;
muchii[p].push_back(i);
}
for(int i=1;i<=m;i++)
cin>>v[i].nod>>v[i].day>>v[i].val;
dfscontrol(1);
sort(v+1,v+m+1,comp);
if(k<=2)
{
solve4();
return 0;
}
if(is3)
{
solve3();
return 0;
}
for(int i=1;i<=m;i++)
{
int nod=v[i].nod;
ll x=v[i].val;
dp[nod][1]=dp[nod][0]+x;
while(nod!=1)
{
nod=par[nod];
dp[nod][0]+=x;
if(dp[nod][0]-x<=dp[nod][1]&&dp[nod][0]>dp[nod][1])
x=dp[nod][0]-dp[nod][1];
else if(dp[nod][0]<=dp[nod][1])
{
x=0;
break;
}
}
}
cout<<dp[1][0];
return 0;
}
Compilation message
magictree.cpp: In function 'void dfscontrol(int)':
magictree.cpp:15:18: 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<muchii[nod].size();i++)
| ~^~~~~~~~~~~~~~~~~~~
magictree.cpp: In function 'void dfs(int)':
magictree.cpp:65:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
65 | for(int i=0;i<muchii[nod].size();i++)
| ~^~~~~~~~~~~~~~~~~~~
magictree.cpp:73:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
73 | for(int i=0;i<muchii[nod].size();i++)
| ~^~~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
2636 KB |
Output is correct |
2 |
Correct |
2 ms |
2676 KB |
Output is correct |
3 |
Correct |
2 ms |
2680 KB |
Output is correct |
4 |
Correct |
2 ms |
2676 KB |
Output is correct |
5 |
Correct |
3 ms |
3448 KB |
Output is correct |
6 |
Correct |
2 ms |
3404 KB |
Output is correct |
7 |
Correct |
2 ms |
3464 KB |
Output is correct |
8 |
Correct |
1 ms |
2636 KB |
Output is correct |
9 |
Correct |
2 ms |
2636 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
42 ms |
9716 KB |
Output is correct |
2 |
Correct |
1899 ms |
13052 KB |
Output is correct |
3 |
Correct |
64 ms |
11772 KB |
Output is correct |
4 |
Correct |
47 ms |
11308 KB |
Output is correct |
5 |
Execution timed out |
2041 ms |
12628 KB |
Time limit exceeded |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
3532 KB |
Output is correct |
2 |
Correct |
2 ms |
3492 KB |
Output is correct |
3 |
Correct |
2 ms |
3532 KB |
Output is correct |
4 |
Correct |
52 ms |
18540 KB |
Output is correct |
5 |
Correct |
58 ms |
18628 KB |
Output is correct |
6 |
Correct |
70 ms |
18656 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
82 ms |
13944 KB |
Output is correct |
2 |
Correct |
93 ms |
13992 KB |
Output is correct |
3 |
Correct |
66 ms |
17348 KB |
Output is correct |
4 |
Correct |
39 ms |
12560 KB |
Output is correct |
5 |
Correct |
55 ms |
21744 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
2636 KB |
Output is correct |
2 |
Correct |
2 ms |
2676 KB |
Output is correct |
3 |
Correct |
2 ms |
2680 KB |
Output is correct |
4 |
Correct |
2 ms |
2676 KB |
Output is correct |
5 |
Correct |
3 ms |
3448 KB |
Output is correct |
6 |
Correct |
2 ms |
3404 KB |
Output is correct |
7 |
Correct |
2 ms |
3464 KB |
Output is correct |
8 |
Correct |
1 ms |
2636 KB |
Output is correct |
9 |
Correct |
2 ms |
2636 KB |
Output is correct |
10 |
Correct |
75 ms |
11744 KB |
Output is correct |
11 |
Correct |
62 ms |
11692 KB |
Output is correct |
12 |
Correct |
1248 ms |
15180 KB |
Output is correct |
13 |
Correct |
35 ms |
10292 KB |
Output is correct |
14 |
Correct |
68 ms |
17984 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
3916 KB |
Output is correct |
2 |
Correct |
24 ms |
8436 KB |
Output is correct |
3 |
Correct |
24 ms |
8440 KB |
Output is correct |
4 |
Correct |
25 ms |
8568 KB |
Output is correct |
5 |
Correct |
11 ms |
6900 KB |
Output is correct |
6 |
Correct |
44 ms |
9668 KB |
Output is correct |
7 |
Correct |
46 ms |
12096 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
2636 KB |
Output is correct |
2 |
Correct |
2 ms |
2676 KB |
Output is correct |
3 |
Correct |
2 ms |
2680 KB |
Output is correct |
4 |
Correct |
2 ms |
2676 KB |
Output is correct |
5 |
Correct |
3 ms |
3448 KB |
Output is correct |
6 |
Correct |
2 ms |
3404 KB |
Output is correct |
7 |
Correct |
2 ms |
3464 KB |
Output is correct |
8 |
Correct |
1 ms |
2636 KB |
Output is correct |
9 |
Correct |
2 ms |
2636 KB |
Output is correct |
10 |
Correct |
2 ms |
3532 KB |
Output is correct |
11 |
Correct |
2 ms |
3492 KB |
Output is correct |
12 |
Correct |
2 ms |
3532 KB |
Output is correct |
13 |
Correct |
52 ms |
18540 KB |
Output is correct |
14 |
Correct |
58 ms |
18628 KB |
Output is correct |
15 |
Correct |
70 ms |
18656 KB |
Output is correct |
16 |
Correct |
75 ms |
11744 KB |
Output is correct |
17 |
Correct |
62 ms |
11692 KB |
Output is correct |
18 |
Correct |
1248 ms |
15180 KB |
Output is correct |
19 |
Correct |
35 ms |
10292 KB |
Output is correct |
20 |
Correct |
68 ms |
17984 KB |
Output is correct |
21 |
Correct |
12 ms |
4444 KB |
Output is correct |
22 |
Correct |
45 ms |
10416 KB |
Output is correct |
23 |
Correct |
44 ms |
10392 KB |
Output is correct |
24 |
Correct |
65 ms |
11972 KB |
Output is correct |
25 |
Correct |
41 ms |
10616 KB |
Output is correct |
26 |
Correct |
1364 ms |
13072 KB |
Output is correct |
27 |
Correct |
918 ms |
15484 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
2636 KB |
Output is correct |
2 |
Correct |
2 ms |
2676 KB |
Output is correct |
3 |
Correct |
2 ms |
2680 KB |
Output is correct |
4 |
Correct |
2 ms |
2676 KB |
Output is correct |
5 |
Correct |
3 ms |
3448 KB |
Output is correct |
6 |
Correct |
2 ms |
3404 KB |
Output is correct |
7 |
Correct |
2 ms |
3464 KB |
Output is correct |
8 |
Correct |
1 ms |
2636 KB |
Output is correct |
9 |
Correct |
2 ms |
2636 KB |
Output is correct |
10 |
Correct |
42 ms |
9716 KB |
Output is correct |
11 |
Correct |
1899 ms |
13052 KB |
Output is correct |
12 |
Correct |
64 ms |
11772 KB |
Output is correct |
13 |
Correct |
47 ms |
11308 KB |
Output is correct |
14 |
Execution timed out |
2041 ms |
12628 KB |
Time limit exceeded |