#include<bits/stdc++.h>
using namespace std;
#define ll long long
const int MAXN=2e5+6;
int n;
vector<int>g[MAXN];
vector<int>g1[MAXN];
vector<int>g3[MAXN];
bool avoid[MAXN];
ll c[MAXN];
int a[MAXN],h[MAXN],subtree[MAXN];
void dfs11(int u,int p)
{
subtree[u]=1;
for(auto xd:g[u])
{
if(xd==p)continue;
dfs11(xd,u);
subtree[u]+=subtree[xd];
}
}
void dfs(int u,int p,map<int,ll> &mp)
{
int bc=-1,maxst=-1;
for(auto xd:g[u])
{
if(xd==p)continue;
if(subtree[xd]>maxst)
{
maxst=subtree[xd];
bc=xd;
}
}
if(bc==-1)
{
mp[h[u]+1]=c[u];
return;
}
dfs(bc,u,mp);
for(auto xd:g[u])
{
if(xd==p||xd==bc)continue;
map<int,ll> tmp;
dfs(xd,u,tmp);
for(auto it:tmp)
{
if(it.second!=0)mp[it.first]+=it.second;
}
}
mp[h[u]+1]+=c[u];
mp[0]+=c[u];
mp[h[u]]-=c[u];
auto it=mp.lower_bound(h[u]);
int d=mp[h[u]];
auto it1=it;
it--;
while(d<0)
{
int t=(*it).first;
if((*it).second+d>=0)
{
mp[t]+=d;
d=0;
}
else
{
d+=(*it).second;
it1=it;
it--;mp.erase(it1);
}
}
mp[h[u]]=d;
}
bool used[MAXN];
vector<int>order;
int comp[MAXN];
void dfs1(int u)
{
used[u]=1;
for(auto xd:g[u])
{
if(used[xd]==0)dfs1(xd);
}
order.push_back(u);
}
void dfs2(int u,int c)
{
comp[u]=c;used[u]=1;
for(auto xd:g[u])
{
if(used[xd]==0)dfs2(xd,c);
}
}
vector<int>v;
void dfs3(int u)
{
used[u]=1;
v.push_back(u);
for(auto xd:g[u])if(!used[xd])dfs3(xd);
for(auto xd:g1[u])if(!used[xd])dfs3(xd);
}
int cnt[MAXN];
int main()
{
//ios_base::sync_with_stdio(false);
//cin.tie(NULL);
//cout.tie(NULL);
cin>>n;
for(int i=1;i<=n;++i)
{
cin>>a[i]>>h[i]>>c[i];
g[a[i]].push_back(i);
g1[i].push_back(a[i]);
}
for(int i=1;i<=n;i++)
{
dfs1(i);
}
reverse(order.begin(),order.end());
memset(used,0,sizeof(used));
for(int i=0;i<order.size();i++)
{
if(used[order[i]]==0)dfs2(order[i],i);
}
memset(used,0,sizeof(used));
ll ans=0;
for(int i=1;i<=n;i++)
{
if(used[i]==0)
{
v.clear();
dfs3(i);
int y=-1;
for(int j=0;j<v.size();j++)
{
cnt[comp[v[j]]]++;
if(cnt[comp[v[j]]]>1)y=comp[v[j]];
if(a[v[j]]==v[j])y=comp[v[j]];
}
for(int j=0;j<v.size();j++)
{
--cnt[comp[v[j]]];
}
vector<pair<int,int> >roots;
for(int j=0;j<v.size();++j)
{
if(comp[v[j]]==y)
{
roots.push_back({h[v[j]],v[j]});
}
}
sort(roots.rbegin(),roots.rend());
vector<int>g1;
for(auto xd:g[roots[roots.size()-1].second])
{
if(xd==roots[0].second)continue;
else g1.push_back(xd);
}
g[roots.back().second]=g1;
for(int j=0;j<roots.size()-1;++j)
{
for(auto xd:g[roots[j].second])g[roots[roots.size()-1].second].push_back(xd);
g[roots[j].second]={roots[j+1].second};
}
//cout<<roots.size()<<" "<<roots[0].second<<endl;
dfs11(roots[0].second,0);
map<int,ll>mp;
dfs(roots[0].second,0,mp);
ans+=mp[0];
}
}
cout<<ans<<endl;
return 0;
}
Compilation message
worst_reporter2.cpp: In function 'int main()':
worst_reporter2.cpp:121:15: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
121 | for(int i=0;i<order.size();i++)
| ~^~~~~~~~~~~~~
worst_reporter2.cpp:134:17: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
134 | for(int j=0;j<v.size();j++)
| ~^~~~~~~~~
worst_reporter2.cpp:140:17: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
140 | for(int j=0;j<v.size();j++)
| ~^~~~~~~~~
worst_reporter2.cpp:145:17: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
145 | for(int j=0;j<v.size();++j)
| ~^~~~~~~~~
worst_reporter2.cpp:160:17: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
160 | for(int j=0;j<roots.size()-1;++j)
| ~^~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
11 ms |
14572 KB |
Output is correct |
2 |
Correct |
12 ms |
14572 KB |
Output is correct |
3 |
Correct |
11 ms |
14572 KB |
Output is correct |
4 |
Correct |
12 ms |
14572 KB |
Output is correct |
5 |
Correct |
22 ms |
15468 KB |
Output is correct |
6 |
Correct |
20 ms |
15212 KB |
Output is correct |
7 |
Correct |
21 ms |
15212 KB |
Output is correct |
8 |
Correct |
26 ms |
15468 KB |
Output is correct |
9 |
Correct |
24 ms |
15340 KB |
Output is correct |
10 |
Correct |
19 ms |
15212 KB |
Output is correct |
11 |
Correct |
21 ms |
15212 KB |
Output is correct |
12 |
Correct |
21 ms |
16236 KB |
Output is correct |
13 |
Correct |
19 ms |
16236 KB |
Output is correct |
14 |
Correct |
20 ms |
15724 KB |
Output is correct |
15 |
Correct |
20 ms |
15724 KB |
Output is correct |
16 |
Correct |
21 ms |
15468 KB |
Output is correct |
17 |
Correct |
20 ms |
15212 KB |
Output is correct |
18 |
Correct |
18 ms |
15212 KB |
Output is correct |
19 |
Correct |
22 ms |
16096 KB |
Output is correct |
20 |
Correct |
19 ms |
15724 KB |
Output is correct |
21 |
Correct |
18 ms |
15872 KB |
Output is correct |
22 |
Correct |
18 ms |
15468 KB |
Output is correct |
23 |
Correct |
18 ms |
15212 KB |
Output is correct |
24 |
Correct |
22 ms |
15980 KB |
Output is correct |
25 |
Correct |
19 ms |
15724 KB |
Output is correct |
26 |
Correct |
22 ms |
17024 KB |
Output is correct |
27 |
Correct |
20 ms |
15852 KB |
Output is correct |
28 |
Correct |
21 ms |
16236 KB |
Output is correct |
29 |
Correct |
20 ms |
16492 KB |
Output is correct |
30 |
Correct |
21 ms |
16492 KB |
Output is correct |
31 |
Correct |
21 ms |
16492 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
21 ms |
15468 KB |
Output is correct |
2 |
Correct |
667 ms |
45504 KB |
Output is correct |
3 |
Correct |
567 ms |
34316 KB |
Output is correct |
4 |
Correct |
678 ms |
43492 KB |
Output is correct |
5 |
Correct |
554 ms |
34308 KB |
Output is correct |
6 |
Correct |
502 ms |
34404 KB |
Output is correct |
7 |
Correct |
454 ms |
34268 KB |
Output is correct |
8 |
Correct |
457 ms |
74908 KB |
Output is correct |
9 |
Correct |
425 ms |
74848 KB |
Output is correct |
10 |
Correct |
361 ms |
74976 KB |
Output is correct |
11 |
Correct |
436 ms |
56032 KB |
Output is correct |
12 |
Correct |
419 ms |
56156 KB |
Output is correct |
13 |
Correct |
546 ms |
45532 KB |
Output is correct |
14 |
Correct |
416 ms |
34140 KB |
Output is correct |
15 |
Correct |
314 ms |
34268 KB |
Output is correct |
16 |
Correct |
544 ms |
62428 KB |
Output is correct |
17 |
Correct |
360 ms |
52952 KB |
Output is correct |
18 |
Correct |
312 ms |
53084 KB |
Output is correct |
19 |
Correct |
404 ms |
44120 KB |
Output is correct |
20 |
Correct |
288 ms |
33116 KB |
Output is correct |
21 |
Correct |
563 ms |
63836 KB |
Output is correct |
22 |
Correct |
353 ms |
53472 KB |
Output is correct |
23 |
Correct |
539 ms |
98912 KB |
Output is correct |
24 |
Correct |
482 ms |
67420 KB |
Output is correct |
25 |
Correct |
479 ms |
82272 KB |
Output is correct |
26 |
Correct |
447 ms |
88820 KB |
Output is correct |
27 |
Correct |
495 ms |
86496 KB |
Output is correct |
28 |
Correct |
496 ms |
86240 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
11 ms |
14572 KB |
Output is correct |
2 |
Correct |
12 ms |
14572 KB |
Output is correct |
3 |
Correct |
11 ms |
14572 KB |
Output is correct |
4 |
Correct |
12 ms |
14572 KB |
Output is correct |
5 |
Correct |
22 ms |
15468 KB |
Output is correct |
6 |
Correct |
20 ms |
15212 KB |
Output is correct |
7 |
Correct |
21 ms |
15212 KB |
Output is correct |
8 |
Correct |
26 ms |
15468 KB |
Output is correct |
9 |
Correct |
24 ms |
15340 KB |
Output is correct |
10 |
Correct |
19 ms |
15212 KB |
Output is correct |
11 |
Correct |
21 ms |
15212 KB |
Output is correct |
12 |
Correct |
21 ms |
16236 KB |
Output is correct |
13 |
Correct |
19 ms |
16236 KB |
Output is correct |
14 |
Correct |
20 ms |
15724 KB |
Output is correct |
15 |
Correct |
20 ms |
15724 KB |
Output is correct |
16 |
Correct |
21 ms |
15468 KB |
Output is correct |
17 |
Correct |
20 ms |
15212 KB |
Output is correct |
18 |
Correct |
18 ms |
15212 KB |
Output is correct |
19 |
Correct |
22 ms |
16096 KB |
Output is correct |
20 |
Correct |
19 ms |
15724 KB |
Output is correct |
21 |
Correct |
18 ms |
15872 KB |
Output is correct |
22 |
Correct |
18 ms |
15468 KB |
Output is correct |
23 |
Correct |
18 ms |
15212 KB |
Output is correct |
24 |
Correct |
22 ms |
15980 KB |
Output is correct |
25 |
Correct |
19 ms |
15724 KB |
Output is correct |
26 |
Correct |
22 ms |
17024 KB |
Output is correct |
27 |
Correct |
20 ms |
15852 KB |
Output is correct |
28 |
Correct |
21 ms |
16236 KB |
Output is correct |
29 |
Correct |
20 ms |
16492 KB |
Output is correct |
30 |
Correct |
21 ms |
16492 KB |
Output is correct |
31 |
Correct |
21 ms |
16492 KB |
Output is correct |
32 |
Correct |
21 ms |
15468 KB |
Output is correct |
33 |
Correct |
667 ms |
45504 KB |
Output is correct |
34 |
Correct |
567 ms |
34316 KB |
Output is correct |
35 |
Correct |
678 ms |
43492 KB |
Output is correct |
36 |
Correct |
554 ms |
34308 KB |
Output is correct |
37 |
Correct |
502 ms |
34404 KB |
Output is correct |
38 |
Correct |
454 ms |
34268 KB |
Output is correct |
39 |
Correct |
457 ms |
74908 KB |
Output is correct |
40 |
Correct |
425 ms |
74848 KB |
Output is correct |
41 |
Correct |
361 ms |
74976 KB |
Output is correct |
42 |
Correct |
436 ms |
56032 KB |
Output is correct |
43 |
Correct |
419 ms |
56156 KB |
Output is correct |
44 |
Correct |
546 ms |
45532 KB |
Output is correct |
45 |
Correct |
416 ms |
34140 KB |
Output is correct |
46 |
Correct |
314 ms |
34268 KB |
Output is correct |
47 |
Correct |
544 ms |
62428 KB |
Output is correct |
48 |
Correct |
360 ms |
52952 KB |
Output is correct |
49 |
Correct |
312 ms |
53084 KB |
Output is correct |
50 |
Correct |
404 ms |
44120 KB |
Output is correct |
51 |
Correct |
288 ms |
33116 KB |
Output is correct |
52 |
Correct |
563 ms |
63836 KB |
Output is correct |
53 |
Correct |
353 ms |
53472 KB |
Output is correct |
54 |
Correct |
539 ms |
98912 KB |
Output is correct |
55 |
Correct |
482 ms |
67420 KB |
Output is correct |
56 |
Correct |
479 ms |
82272 KB |
Output is correct |
57 |
Correct |
447 ms |
88820 KB |
Output is correct |
58 |
Correct |
495 ms |
86496 KB |
Output is correct |
59 |
Correct |
496 ms |
86240 KB |
Output is correct |
60 |
Incorrect |
11 ms |
14572 KB |
Output isn't correct |
61 |
Halted |
0 ms |
0 KB |
- |