#include<bits/stdc++.h>
using namespace std;
typedef long long int ll;
ll n,i,j,k,l,r,x,y,z,w,s,t,b[1100000],h[1100000],m;
vector<ll> v[1100000],u;
pair<ll,ll> a[1100000];
multiset<pair<ll,ll>> dp[110000];
void f(ll x,ll y)
{
ll i,j;
for(i=0;i<h[x];i++)
{
if(v[x][i]==y)
continue;
f(v[x][i],x);
}
for(i=0;i<h[x];i++)
{
if(v[x][i]==y)
continue;
if(dp[x].empty())
{
swap(dp[x],dp[v[x][i]]);
continue;
}
if(dp[x].size()<dp[v[x][i]].size())
swap(dp[x],dp[v[x][i]]);
for(pair<ll,ll> p:dp[v[x][i]])
{
dp[x].insert(p);
}
dp[v[x][i]].clear();
}
if(a[x].first==0)
return;
dp[x].insert({a[x].first,a[x].second});
pair<ll,ll> p;
p={a[x].first+1,0};
s=0;
pair<ll,ll> id;
while(1)
{
if(dp[x].lower_bound(p)==dp[x].end())
break;
id=(*dp[x].lower_bound(p));
if(id.second+s>=a[x].second)
{
dp[x].erase(dp[x].find(id));
dp[x].insert({id.first,id.second+s-a[x].second});
break;
}
dp[x].erase(dp[x].find(id));
s+=id.second;
}
}
int main()
{
scanf("%lld %lld %lld",&n,&m,&k);
for(i=2;i<=n;i++)
{
scanf("%lld",&x);
y=i;
v[x].push_back(y);
v[y].push_back(x);
h[x]++;
h[y]++;
}
for(i=1;i<=m;i++)
{
scanf("%lld %lld %lld",&x,&y,&z);
a[x]={y,z};
}
f(1,0);
s=0;
for(pair<ll,ll> p:dp[1])
{
s+=p.second;
}
printf("%lld",s);
}
Compilation message
magictree.cpp: In function 'void f(ll, ll)':
magictree.cpp:10:10: warning: unused variable 'j' [-Wunused-variable]
10 | ll i,j;
| ^
magictree.cpp: In function 'int main()':
magictree.cpp:58:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
58 | scanf("%lld %lld %lld",&n,&m,&k);
| ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~
magictree.cpp:61:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
61 | scanf("%lld",&x);
| ~~~~~^~~~~~~~~~~
magictree.cpp:70:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
70 | scanf("%lld %lld %lld",&x,&y,&z);
| ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
11 ms |
35420 KB |
Output is correct |
2 |
Correct |
10 ms |
35416 KB |
Output is correct |
3 |
Correct |
10 ms |
35252 KB |
Output is correct |
4 |
Correct |
10 ms |
35416 KB |
Output is correct |
5 |
Correct |
9 ms |
35420 KB |
Output is correct |
6 |
Correct |
12 ms |
35672 KB |
Output is correct |
7 |
Correct |
11 ms |
35416 KB |
Output is correct |
8 |
Correct |
11 ms |
35264 KB |
Output is correct |
9 |
Correct |
10 ms |
35420 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
60 ms |
43348 KB |
Output is correct |
2 |
Correct |
54 ms |
50168 KB |
Output is correct |
3 |
Correct |
130 ms |
48900 KB |
Output is correct |
4 |
Correct |
85 ms |
47300 KB |
Output is correct |
5 |
Correct |
107 ms |
49320 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
11 ms |
35676 KB |
Output is correct |
2 |
Correct |
12 ms |
35672 KB |
Output is correct |
3 |
Correct |
10 ms |
35676 KB |
Output is correct |
4 |
Correct |
76 ms |
59244 KB |
Output is correct |
5 |
Correct |
105 ms |
65936 KB |
Output is correct |
6 |
Correct |
113 ms |
59488 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
124 ms |
46880 KB |
Output is correct |
2 |
Correct |
149 ms |
48580 KB |
Output is correct |
3 |
Correct |
102 ms |
53304 KB |
Output is correct |
4 |
Correct |
71 ms |
47092 KB |
Output is correct |
5 |
Correct |
87 ms |
62160 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
11 ms |
35420 KB |
Output is correct |
2 |
Correct |
10 ms |
35416 KB |
Output is correct |
3 |
Correct |
10 ms |
35252 KB |
Output is correct |
4 |
Correct |
10 ms |
35416 KB |
Output is correct |
5 |
Correct |
9 ms |
35420 KB |
Output is correct |
6 |
Correct |
12 ms |
35672 KB |
Output is correct |
7 |
Correct |
11 ms |
35416 KB |
Output is correct |
8 |
Correct |
11 ms |
35264 KB |
Output is correct |
9 |
Correct |
10 ms |
35420 KB |
Output is correct |
10 |
Correct |
156 ms |
46620 KB |
Output is correct |
11 |
Correct |
137 ms |
47344 KB |
Output is correct |
12 |
Correct |
96 ms |
50408 KB |
Output is correct |
13 |
Correct |
63 ms |
47044 KB |
Output is correct |
14 |
Correct |
93 ms |
59696 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
19 ms |
36440 KB |
Output is correct |
2 |
Correct |
47 ms |
41476 KB |
Output is correct |
3 |
Correct |
39 ms |
41308 KB |
Output is correct |
4 |
Correct |
40 ms |
41292 KB |
Output is correct |
5 |
Correct |
28 ms |
41424 KB |
Output is correct |
6 |
Correct |
45 ms |
45148 KB |
Output is correct |
7 |
Correct |
42 ms |
48364 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
11 ms |
35420 KB |
Output is correct |
2 |
Correct |
10 ms |
35416 KB |
Output is correct |
3 |
Correct |
10 ms |
35252 KB |
Output is correct |
4 |
Correct |
10 ms |
35416 KB |
Output is correct |
5 |
Correct |
9 ms |
35420 KB |
Output is correct |
6 |
Correct |
12 ms |
35672 KB |
Output is correct |
7 |
Correct |
11 ms |
35416 KB |
Output is correct |
8 |
Correct |
11 ms |
35264 KB |
Output is correct |
9 |
Correct |
10 ms |
35420 KB |
Output is correct |
10 |
Correct |
11 ms |
35676 KB |
Output is correct |
11 |
Correct |
12 ms |
35672 KB |
Output is correct |
12 |
Correct |
10 ms |
35676 KB |
Output is correct |
13 |
Correct |
76 ms |
59244 KB |
Output is correct |
14 |
Correct |
105 ms |
65936 KB |
Output is correct |
15 |
Correct |
113 ms |
59488 KB |
Output is correct |
16 |
Correct |
156 ms |
46620 KB |
Output is correct |
17 |
Correct |
137 ms |
47344 KB |
Output is correct |
18 |
Correct |
96 ms |
50408 KB |
Output is correct |
19 |
Correct |
63 ms |
47044 KB |
Output is correct |
20 |
Correct |
93 ms |
59696 KB |
Output is correct |
21 |
Correct |
28 ms |
37724 KB |
Output is correct |
22 |
Correct |
100 ms |
44832 KB |
Output is correct |
23 |
Correct |
88 ms |
44372 KB |
Output is correct |
24 |
Correct |
121 ms |
47700 KB |
Output is correct |
25 |
Correct |
73 ms |
47096 KB |
Output is correct |
26 |
Correct |
133 ms |
51544 KB |
Output is correct |
27 |
Correct |
120 ms |
51168 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
11 ms |
35420 KB |
Output is correct |
2 |
Correct |
10 ms |
35416 KB |
Output is correct |
3 |
Correct |
10 ms |
35252 KB |
Output is correct |
4 |
Correct |
10 ms |
35416 KB |
Output is correct |
5 |
Correct |
9 ms |
35420 KB |
Output is correct |
6 |
Correct |
12 ms |
35672 KB |
Output is correct |
7 |
Correct |
11 ms |
35416 KB |
Output is correct |
8 |
Correct |
11 ms |
35264 KB |
Output is correct |
9 |
Correct |
10 ms |
35420 KB |
Output is correct |
10 |
Correct |
60 ms |
43348 KB |
Output is correct |
11 |
Correct |
54 ms |
50168 KB |
Output is correct |
12 |
Correct |
130 ms |
48900 KB |
Output is correct |
13 |
Correct |
85 ms |
47300 KB |
Output is correct |
14 |
Correct |
107 ms |
49320 KB |
Output is correct |
15 |
Correct |
11 ms |
35676 KB |
Output is correct |
16 |
Correct |
12 ms |
35672 KB |
Output is correct |
17 |
Correct |
10 ms |
35676 KB |
Output is correct |
18 |
Correct |
76 ms |
59244 KB |
Output is correct |
19 |
Correct |
105 ms |
65936 KB |
Output is correct |
20 |
Correct |
113 ms |
59488 KB |
Output is correct |
21 |
Correct |
124 ms |
46880 KB |
Output is correct |
22 |
Correct |
149 ms |
48580 KB |
Output is correct |
23 |
Correct |
102 ms |
53304 KB |
Output is correct |
24 |
Correct |
71 ms |
47092 KB |
Output is correct |
25 |
Correct |
87 ms |
62160 KB |
Output is correct |
26 |
Correct |
156 ms |
46620 KB |
Output is correct |
27 |
Correct |
137 ms |
47344 KB |
Output is correct |
28 |
Correct |
96 ms |
50408 KB |
Output is correct |
29 |
Correct |
63 ms |
47044 KB |
Output is correct |
30 |
Correct |
93 ms |
59696 KB |
Output is correct |
31 |
Correct |
19 ms |
36440 KB |
Output is correct |
32 |
Correct |
47 ms |
41476 KB |
Output is correct |
33 |
Correct |
39 ms |
41308 KB |
Output is correct |
34 |
Correct |
40 ms |
41292 KB |
Output is correct |
35 |
Correct |
28 ms |
41424 KB |
Output is correct |
36 |
Correct |
45 ms |
45148 KB |
Output is correct |
37 |
Correct |
42 ms |
48364 KB |
Output is correct |
38 |
Correct |
28 ms |
37724 KB |
Output is correct |
39 |
Correct |
100 ms |
44832 KB |
Output is correct |
40 |
Correct |
88 ms |
44372 KB |
Output is correct |
41 |
Correct |
121 ms |
47700 KB |
Output is correct |
42 |
Correct |
73 ms |
47096 KB |
Output is correct |
43 |
Correct |
133 ms |
51544 KB |
Output is correct |
44 |
Correct |
120 ms |
51168 KB |
Output is correct |
45 |
Correct |
29 ms |
37200 KB |
Output is correct |
46 |
Correct |
104 ms |
44588 KB |
Output is correct |
47 |
Correct |
80 ms |
43832 KB |
Output is correct |
48 |
Correct |
156 ms |
46540 KB |
Output is correct |
49 |
Correct |
86 ms |
47092 KB |
Output is correct |
50 |
Correct |
135 ms |
50260 KB |
Output is correct |
51 |
Correct |
115 ms |
50192 KB |
Output is correct |