Submission #1019796

# Submission time Handle Problem Language Result Execution time Memory
1019796 2024-07-11T08:54:19 Z ttamx Islands (IOI08_islands) C++17
90 / 100
490 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.empty()&&dq.front().second==u)dq.pop_front();
            if(!dq.empty())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 13 ms 23896 KB Output is correct
2 Correct 11 ms 23900 KB Output is correct
3 Correct 13 ms 23900 KB Output is correct
4 Correct 11 ms 23900 KB Output is correct
5 Correct 11 ms 23900 KB Output is correct
6 Correct 11 ms 23956 KB Output is correct
7 Correct 11 ms 23948 KB Output is correct
8 Correct 15 ms 24152 KB Output is correct
9 Correct 12 ms 23912 KB Output is correct
10 Correct 11 ms 23900 KB Output is correct
11 Correct 11 ms 23836 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 12 ms 23896 KB Output is correct
2 Correct 11 ms 23900 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 15 ms 24048 KB Output is correct
2 Correct 12 ms 24152 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 20 ms 24664 KB Output is correct
2 Correct 19 ms 25816 KB Output is correct
3 Correct 17 ms 24900 KB Output is correct
4 Correct 14 ms 24412 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 22 ms 26856 KB Output is correct
2 Correct 30 ms 28636 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 76 ms 34348 KB Output is correct
2 Correct 50 ms 36200 KB Output is correct
3 Correct 57 ms 39872 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 75 ms 41880 KB Output is correct
2 Correct 100 ms 49636 KB Output is correct
3 Correct 100 ms 50928 KB Output is correct
4 Correct 155 ms 64240 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 155 ms 63668 KB Output is correct
2 Correct 292 ms 74156 KB Output is correct
3 Correct 152 ms 57852 KB Output is correct
4 Correct 184 ms 69224 KB Output is correct
5 Correct 195 ms 69480 KB Output is correct
6 Correct 490 ms 71216 KB Output is correct
7 Correct 187 ms 82864 KB Output is correct
# Verdict Execution time Memory Grader output
1 Runtime error 221 ms 131072 KB Execution killed with signal 9
2 Halted 0 ms 0 KB -