Submission #1019791

# Submission time Handle Problem Language Result Execution time Memory
1019791 2024-07-11T08:53:09 Z ttamx Islands (IOI08_islands) C++17
90 / 100
473 ms 131072 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;
vector<pair<int,int>> adj[N];
ll dp[N];
ll dia,ans;

void dfs(int u){
    ll mx=0,mx2=0;
    for(auto [v,w]:adj[u]){
        dfs(v);
        mx2=max(mx2,dp[v]+w);
        if(mx<mx2)swap(mx,mx2);
    }
    dp[u]=mx;
    dia=max(dia,mx+mx2);
}

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];
        adj[v].emplace_back(u,w);
        if(--deg[v]==0)q.emplace(v);
    }
    for(int i=1;i<=n;i++)if(deg[i]){
        ll len=0;
        vector<pair<int,ll>> comp;
        for(int u=i;deg[u];){
            comp.emplace_back(u,len);
            deg[u]=0;
            auto [v,w]=nxt[u];
            len+=w;
            u=v;
        }
        int m=comp.size();
        dia=0;
        for(auto [u,w]:comp)dfs(u);
        deque<pair<ll,int>> dq;
        for(auto [u,w]:comp){
            ll val=dp[u]-w;
            while(!dq.empty()&&dq.back().first<val)dq.pop_back();
            dq.emplace_back(val,u);
        }
        for(auto [u,w]:comp){
            if(dq.front().second==u)dq.pop_front();
            dia=max(dia,w+len+dp[u]+dq.front().first);
            ll val=dp[u]-w-len;
            while(!dq.empty()&&dq.back().first<val)dq.pop_back();
            dq.emplace_back(val,u);
        }
        ans+=dia;
    }
    cout << ans;
}

Compilation message

islands.cpp: In function 'int main()':
islands.cpp:54:13: warning: unused variable 'm' [-Wunused-variable]
   54 |         int m=comp.size();
      |             ^
# Verdict Execution time Memory Grader output
1 Correct 12 ms 23896 KB Output is correct
2 Correct 11 ms 23900 KB Output is correct
3 Correct 12 ms 23900 KB Output is correct
4 Correct 11 ms 23900 KB Output is correct
5 Correct 10 ms 23900 KB Output is correct
6 Correct 11 ms 23960 KB Output is correct
7 Correct 13 ms 23900 KB Output is correct
8 Correct 9 ms 23900 KB Output is correct
9 Correct 13 ms 23896 KB Output is correct
10 Correct 11 ms 23896 KB Output is correct
11 Correct 11 ms 23896 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 12 ms 23964 KB Output is correct
2 Correct 12 ms 23896 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 10 ms 23900 KB Output is correct
2 Correct 12 ms 24316 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 15 ms 24668 KB Output is correct
2 Correct 19 ms 25844 KB Output is correct
3 Correct 18 ms 24924 KB Output is correct
4 Correct 14 ms 24444 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 24 ms 26832 KB Output is correct
2 Correct 28 ms 28648 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 50 ms 34508 KB Output is correct
2 Correct 53 ms 36164 KB Output is correct
3 Correct 59 ms 40340 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 77 ms 42708 KB Output is correct
2 Correct 101 ms 50628 KB Output is correct
3 Correct 99 ms 51756 KB Output is correct
4 Correct 127 ms 66248 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 161 ms 67020 KB Output is correct
2 Correct 279 ms 81576 KB Output is correct
3 Correct 145 ms 62904 KB Output is correct
4 Correct 191 ms 75228 KB Output is correct
5 Correct 189 ms 75380 KB Output is correct
6 Correct 473 ms 78052 KB Output is correct
7 Correct 206 ms 90264 KB Output is correct
# Verdict Execution time Memory Grader output
1 Runtime error 218 ms 131072 KB Execution killed with signal 9
2 Halted 0 ms 0 KB -