Submission #1003662

# Submission time Handle Problem Language Result Execution time Memory
1003662 2024-06-20T14:50:58 Z bachhoangxuan Islands (IOI08_islands) C++17
100 / 100
206 ms 67916 KB
#include<bits/stdc++.h>
using namespace std;
#define int long long
const int maxn = 1e6+5;
const int inf = 1e18;
int N,P[maxn],L[maxn],dp[maxn],mx[maxn],res;
int deg[maxn];

signed main(){
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);cout.tie(NULL);
    cin >> N;
    for(int i=1;i<=N;i++){
        cin >> P[i] >> L[i];
        deg[P[i]]++;
    }
    queue<int> q;
    for(int i=1;i<=N;i++) if(!deg[i]) q.push(i);
    while(!q.empty()){
        int u=q.front(),p=P[u];q.pop();
        mx[p]=max({mx[p],mx[u],dp[p]+dp[u]+L[u]});
        dp[p]=max(dp[p],dp[u]+L[u]);
        if(!(--deg[p])) q.push(p);
    }
    for(int i=1;i<=N;i++){
        if(!deg[i]) continue;
        vector<int> cycle;
        int u=i,ans=0,total=0;
        while(deg[u]){
            ans=max(ans,mx[u]);
            total+=L[u];
            cycle.push_back(u);
            deg[u]=0;u=P[u];
        }
        int m1=-inf,m2=-inf;
        for(int v:cycle){
            ans=max({ans,m1+dp[v],m2+dp[v]+total});
            m1=max(m1,dp[v])+L[v];
            m2=max(m2,dp[v])-L[v];
        }
        res+=ans;
    }
    cout << res << '\n';
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 4440 KB Output is correct
2 Correct 1 ms 4444 KB Output is correct
3 Correct 1 ms 4444 KB Output is correct
4 Correct 1 ms 4444 KB Output is correct
5 Correct 1 ms 4444 KB Output is correct
6 Correct 1 ms 4444 KB Output is correct
7 Correct 1 ms 4444 KB Output is correct
8 Correct 0 ms 4444 KB Output is correct
9 Correct 1 ms 4444 KB Output is correct
10 Correct 1 ms 4444 KB Output is correct
11 Correct 1 ms 4444 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 4444 KB Output is correct
2 Correct 1 ms 4444 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 4440 KB Output is correct
2 Correct 1 ms 4624 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 4956 KB Output is correct
2 Correct 5 ms 8028 KB Output is correct
3 Correct 3 ms 5284 KB Output is correct
4 Correct 2 ms 4956 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 6 ms 8024 KB Output is correct
2 Correct 11 ms 9504 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 25 ms 12636 KB Output is correct
2 Correct 23 ms 15948 KB Output is correct
3 Correct 39 ms 21360 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 43 ms 19032 KB Output is correct
2 Correct 56 ms 34552 KB Output is correct
3 Correct 80 ms 29984 KB Output is correct
4 Correct 76 ms 46196 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 119 ms 29024 KB Output is correct
2 Correct 139 ms 38020 KB Output is correct
3 Correct 93 ms 46420 KB Output is correct
4 Correct 118 ms 61268 KB Output is correct
5 Correct 117 ms 60644 KB Output is correct
6 Correct 206 ms 56656 KB Output is correct
7 Correct 123 ms 67916 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 113 ms 39508 KB Output is correct
2 Correct 112 ms 54776 KB Output is correct
3 Correct 112 ms 49352 KB Output is correct
4 Correct 123 ms 40020 KB Output is correct
5 Correct 107 ms 61608 KB Output is correct
6 Correct 108 ms 54608 KB Output is correct
7 Correct 141 ms 57168 KB Output is correct