Submission #1019818

# Submission time Handle Problem Language Result Execution time Memory
1019818 2024-07-11T09:15:11 Z ttamx Islands (IOI08_islands) C++17
100 / 100
212 ms 79440 KB
#include<bits/stdc++.h>

using namespace std;

using ll = long long;

const int N=1e6+5;

int n;
pair<int,int> nxt[N];
int deg[N];
queue<int> q;
ll dp[N],dp2[N],dia[N];
ll ans;

template<class T>
struct Vector{
    T dat[N];
    int sz=0;
    void clear(){
        sz=0;
    }
    void push_back(T val){
        dat[sz]=val;
        sz++;
    }
    T* begin(){
        return dat;
    }
    T* end(){
        return dat+sz;
    }
};
Vector<pair<int,ll>> comp;

template<class T>
struct Deque{
    T dat[N*2];
    int l=0,r=0;
    bool empty(){
        return l>r;
    }
    void clear(){
        l=0,r=-1;
    }
    void push_back(T val){
        dat[++r]=val;
    }
    void pop_front(){
        l++;
    }
    void pop_back(){
        r--;
    }
    T front(){
        return dat[l];
    }
    T back(){
        return dat[r];
    }
};
Deque<pair<ll,int>> dq;

int main(){
    cin.tie(nullptr)->sync_with_stdio(false);
    cin >> n;
    for(int i=1;i<=n;i++){
        auto &[v,w]=nxt[i];
        cin >> v >> w;
        deg[v]++;
    }
    for(int i=1;i<=n;i++)if(!deg[i])q.emplace(i);
    while(!q.empty()){
        int u=q.front();
        q.pop();
        auto [v,w]=nxt[u];
        dp2[v]=max(dp2[v],dp[u]+w);
        if(dp[v]<dp2[v])swap(dp[v],dp2[v]);
        dia[v]=max({dia[v],dia[u],dp[v]+dp2[v]});
        if(--deg[v]==0)q.emplace(v);
    }
    for(int i=1;i<=n;i++)if(deg[i]){
        comp.clear();
        ll len=0;
        for(int u=i;deg[u];){
            comp.push_back({u,len});
            deg[u]=0;
            auto [v,w]=nxt[u];
            len+=w;
            u=v;
        }
        ll res=0;
        dq.clear();
        for(auto [u,w]:comp){
            res=max(res,dia[u]);
            ll val=dp[u]-w;
            while(!dq.empty()&&dq.back().first<val)dq.pop_back();
            dq.push_back({val,u});
        }
        for(auto [u,w]:comp){
            if(dq.front().second==u)dq.pop_front();
            res=max(res,w+len+dp[u]+dq.front().first);
            ll val=dp[u]-w-len;
            while(!dq.empty()&&dq.back().first<val)dq.pop_back();
            dq.push_back({val,u});
        }
        ans+=res;
    }
    cout << ans;
}
# Verdict Execution time Memory Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 1 ms 348 KB Output is correct
3 Correct 1 ms 348 KB Output is correct
4 Correct 1 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 368 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 1 ms 476 KB Output is correct
10 Correct 1 ms 348 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 360 KB Output is correct
2 Correct 1 ms 520 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 600 KB Output is correct
2 Correct 1 ms 604 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 1292 KB Output is correct
2 Correct 6 ms 2396 KB Output is correct
3 Correct 4 ms 1372 KB Output is correct
4 Correct 3 ms 860 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 8 ms 3552 KB Output is correct
2 Correct 15 ms 5456 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 31 ms 10856 KB Output is correct
2 Correct 29 ms 9688 KB Output is correct
3 Correct 41 ms 20300 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 74 ms 19036 KB Output is correct
2 Correct 78 ms 28268 KB Output is correct
3 Correct 68 ms 23128 KB Output is correct
4 Correct 107 ms 51540 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 137 ms 27884 KB Output is correct
2 Correct 168 ms 58928 KB Output is correct
3 Correct 118 ms 39364 KB Output is correct
4 Correct 152 ms 57128 KB Output is correct
5 Correct 156 ms 56388 KB Output is correct
6 Correct 192 ms 50004 KB Output is correct
7 Correct 181 ms 79440 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 185 ms 51000 KB Output is correct
2 Correct 154 ms 50892 KB Output is correct
3 Correct 157 ms 74576 KB Output is correct
4 Correct 140 ms 27204 KB Output is correct
5 Correct 151 ms 60908 KB Output is correct
6 Correct 155 ms 51028 KB Output is correct
7 Correct 212 ms 51280 KB Output is correct