# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
1011518 | pcc | Telegraph (JOI16_telegraph) | C++17 | 0 ms | 344 KiB |
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 ll long long
#define pii pair<int,int>
#define fs first
#define sc second
#define pll pair<ll,ll>
const int mxn = 1e5+10;
pii arr[mxn];
int deg[mxn];
queue<int> q;
int N;
ll dp[mxn][2],dp2[mxn][4];
ll sum = 0;
int main(){
ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
cin>>N;
for(int i = 1;i<=N;i++){
cin>>arr[i].fs>>arr[i].sc;
sum += arr[i].sc;
deg[arr[i].fs]++;
}
for(int i = 1;i<=N;i++)if(!deg[i])q.push(i);
while(!q.empty()){
auto now = q.front();
q.pop();
auto [nxt,d] = arr[now];
deg[nxt]--;
dp[nxt][0] = max(dp[nxt][0],max(dp[now][0],dp[now][1]));
dp[nxt][1] = max(dp[nxt][1],max(dp[now][0],dp[now][1])+d);
if(!deg[nxt])q.push(nxt);
}
ll ans = 0;
for(int ii = 1;ii<=N;ii++){
if(!deg[ii])continue;
int now = ii;
vector<pii> v;
ll tans = 0;
priority_queue<ll,vector<ll>,less<ll>> pq;
do{
deg[now] = 0;
auto [nxt,d] = arr[now];
pq.push(dp[nxt][1]-dp[nxt][0]-d);
ans += dp[nxt][0]+d;
now = nxt;
}while(now != ii);
if(pq.size() == N)continue;
ans += pq.top();pq.pop();
while(pq.top()>0){
ans += pq.top();
pq.pop();
}
}
cout<<sum-ans<<'\n';
return 0;
}
Compilation message (stderr)
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |