This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include<bits/stdc++.h>
using namespace std;
#define N 200100
#define int long long
int gonext[N],h[N],c[N],vis[N],ans;
vector<int>adj[N];
map<int,int>mp[N];
void merge(int a,int b){
if(mp[a].size()<mp[b].size())
swap(mp[a],mp[b]);
for(auto[i,j]:mp[b])
mp[a][i]+=j;
mp[b].clear();
}
void dfs(int n,int p,int q=1){
vis[n]=1;
for(auto i:adj[n])if(i-p)
dfs(i,n),merge(n,i);
mp[n][h[n]+1]+=c[n];
mp[n][h[n]];
mp[n][0]+=c[n];
if(q){
auto it=mp[n].lower_bound(h[n]);
while(it->second<c[n]){
prev(it)->second+=it->second;
it=--mp[n].erase(it);
}
it->second-=c[n];
} else mp[n][h[n]]-=c[n];
}
void docomp(int n){
while(!vis[n])
vis[n]=1,n=gonext[n];
vector<int>cyc;
int y=n;
do {
cyc.push_back(y);
y=gonext[y];
} while(y-n);
cyc.push_back(y);
for(int i=1;i<cyc.size();i++)
dfs(cyc[i],cyc[i-1],0);
for(int i=1;i+1<cyc.size();i++)
merge(n,cyc[i]);
int pref=0,cur=1e18;
for(auto[i,j]:mp[n])
cur=min(cur,pref+=j);
ans+=cur;
}
signed main(){
cin.tie(0)->sync_with_stdio(0);
int n;
cin>>n;
for(int i=1;i<=n;i++){
cin>>gonext[i]>>h[i]>>c[i];
adj[gonext[i]].push_back(i);
}
for(int i=1;i<=n;i++)
if(!vis[i])
docomp(i);
cout<<ans<<'\n';
}
Compilation message (stderr)
worst_reporter2.cpp: In function 'void docomp(long long int)':
worst_reporter2.cpp:41:18: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
41 | for(int i=1;i<cyc.size();i++)
| ~^~~~~~~~~~~
worst_reporter2.cpp:43:20: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
43 | for(int i=1;i+1<cyc.size();i++)
| ~~~^~~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |