Submission #900381

# Submission time Handle Problem Language Result Execution time Memory
900381 2024-01-08T08:14:38 Z abcvuitunggio Islands (IOI08_islands) C++17
80 / 100
501 ms 131072 KB
#include <bits/stdc++.h>
using namespace std;
const int mxn=1000001;
const long long INF=1e18;
vector <int> ke[mxn],ve[mxn],V[mxn],tmp;
int n,p[mxn],l[mxn],vis[mxn],vis2[mxn],root[mxn],idx,sz,x;
long long dp[mxn],dp2[mxn],d[mxn],mx,res,a,b,sum,s;
void dfs(int u){
    for (int v:ke[u]){
        d[v]=d[u]+l[v];
        dfs(v);
        dp[u]=max(dp[u],dp[v]+l[v]);
    }
}
void dfs2(int u){
    int mx=0,mx2=0;
    for (int v:ke[u]){
        if (dp[v]+l[v]>dp[mx]+l[mx]){
            mx2=mx;
            mx=v;
        }
        else if (dp[v]+l[v]>dp[mx2]+l[mx2])
            mx2=v;
    }
    for (int v:ke[u]){
        dp2[v]=max(dp2[u],(mx==v?dp[mx2]+l[mx2]:dp[mx]+l[mx]))+l[v];
        dfs2(v);
    }
}
int main(){
    ios_base::sync_with_stdio(NULL);cin.tie(nullptr);
    cin >> n;
    for (int i=1;i<=n;i++)
        cin >> p[i] >> l[i];
    for (int i=1;i<=n;i++)
        if (!vis[i]){
            x=i;
            tmp.clear();
            while (true){
                tmp.push_back(i);
                vis2[i]=tmp.size();
                i=p[i];
                if (vis[i]){
                    for (int j:tmp){
                        root[j]=root[i];
                        ke[p[j]].push_back(j);
                        vis[j]=1;
                    }
                    break;
                }
                if (vis2[i]){
                    for (int j=vis2[i]-1;j<tmp.size();j++){
                        root[tmp[j]]=tmp[j];
                        ve[idx].push_back(tmp[j]);
                    }
                    for (int j=0;j<vis2[i]-1;j++){
                        root[tmp[j]]=i;
                        ke[tmp[j+1]].push_back(tmp[j]);
                    }
                    for (int j:tmp)
                        vis[j]=1;
                    idx++;
                    break;
                }
            }
            vector <int>().swap(tmp);
            i=x;
        }
    for (int i=1;i<=n;i++)
        if (root[i]==i){
            dfs(i);
            dfs2(i);
        }
    for (int i=1;i<=n;i++){
        vector <int>().swap(ke[i]);
        V[root[i]].push_back(i);
    }
    for (int i=0;i<idx;i++){
        sz=ve[i].size(),sum=0,a=-INF,b=-INF;
        for (int j:ve[i])
            sum+=l[j];
        mx=0,s=sum;
        for (int j=sz-1;j>=0;j--){
            for (int u:V[ve[i][j]])
                mx=max(mx,max(max(dp[u],dp2[u]),d[u]+max(a-s,b+sum+s)));
            a=max(a,dp[ve[i][j]]+s);
            b=max(b,dp[ve[i][j]]-s);
            s-=l[ve[i][(j?j:sz)-1]];
        }
        res+=mx;
    }
    cout << res;
}

Compilation message

islands.cpp: In function 'int main()':
islands.cpp:52:43: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   52 |                     for (int j=vis2[i]-1;j<tmp.size();j++){
      |                                          ~^~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 23 ms 80728 KB Output is correct
2 Correct 19 ms 80988 KB Output is correct
3 Correct 17 ms 80992 KB Output is correct
4 Correct 17 ms 80732 KB Output is correct
5 Correct 19 ms 80732 KB Output is correct
6 Correct 16 ms 80732 KB Output is correct
7 Correct 16 ms 80728 KB Output is correct
8 Correct 17 ms 80972 KB Output is correct
9 Correct 17 ms 80924 KB Output is correct
10 Correct 17 ms 80960 KB Output is correct
11 Correct 17 ms 80732 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 18 ms 80988 KB Output is correct
2 Correct 17 ms 80988 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 18 ms 80896 KB Output is correct
2 Correct 18 ms 80984 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 21 ms 83548 KB Output is correct
2 Correct 29 ms 86616 KB Output is correct
3 Correct 23 ms 84056 KB Output is correct
4 Correct 20 ms 83548 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 30 ms 87048 KB Output is correct
2 Correct 37 ms 87128 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 66 ms 93696 KB Output is correct
2 Correct 62 ms 93264 KB Output is correct
3 Correct 72 ms 96064 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 101 ms 103960 KB Output is correct
2 Correct 128 ms 108952 KB Output is correct
3 Correct 119 ms 108904 KB Output is correct
4 Correct 160 ms 120240 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 206 ms 118420 KB Output is correct
2 Correct 501 ms 126620 KB Output is correct
3 Correct 189 ms 121632 KB Output is correct
4 Runtime error 210 ms 131072 KB Execution killed with signal 9
5 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 188 ms 131072 KB Execution killed with signal 9
2 Halted 0 ms 0 KB -