답안 #900396

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
900396 2024-01-08T08:32:26 Z abcvuitunggio Islands (IOI08_islands) C++17
90 / 100
759 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/2],V[mxn];
int n,p[mxn],l[mxn],vis[mxn],vis2[mxn],root[mxn],tmp[mxn],idx,sz,x,mx,mx2,id;
long long dp[mxn],dp2[mxn],d[mxn],val,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){
    mx=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];
    for (int v:ke[u])
        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;
            id=0;
            while (true){
                tmp[id]=i;
                vis2[i]=++id;
                i=p[i];
                if (vis[i]){
                    for (int j=0;j<id;j++){
                        root[tmp[j]]=root[i];
                        ke[p[tmp[j]]].push_back(tmp[j]);
                        vis[tmp[j]]=1;
                    }
                    break;
                }
                if (vis2[i]){
                    for (int j=vis2[i]-1;j<id;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=0;j<id;j++)
                        vis[tmp[j]]=1;
                    idx++;
                    break;
                }
            }
            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];
        val=0,s=sum;
        for (int j=sz-1;j>=0;j--){
            for (int u:V[ve[i][j]])
                val=max(val,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+=val;
    }
    cout << res;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 22 ms 71004 KB Output is correct
2 Correct 15 ms 71004 KB Output is correct
3 Correct 15 ms 71004 KB Output is correct
4 Correct 15 ms 71004 KB Output is correct
5 Correct 14 ms 71004 KB Output is correct
6 Correct 15 ms 71004 KB Output is correct
7 Correct 15 ms 71000 KB Output is correct
8 Correct 15 ms 71004 KB Output is correct
9 Correct 15 ms 71096 KB Output is correct
10 Correct 14 ms 71004 KB Output is correct
11 Correct 14 ms 70876 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 18 ms 71512 KB Output is correct
2 Correct 16 ms 71000 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 16 ms 71000 KB Output is correct
2 Correct 17 ms 71256 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 20 ms 73816 KB Output is correct
2 Correct 25 ms 76636 KB Output is correct
3 Correct 21 ms 74076 KB Output is correct
4 Correct 18 ms 73616 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 28 ms 77148 KB Output is correct
2 Correct 38 ms 79508 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 64 ms 85844 KB Output is correct
2 Correct 55 ms 85416 KB Output is correct
3 Correct 69 ms 88016 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 95 ms 95824 KB Output is correct
2 Correct 138 ms 100684 KB Output is correct
3 Correct 114 ms 100220 KB Output is correct
4 Correct 146 ms 113860 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 182 ms 108528 KB Output is correct
2 Correct 427 ms 116672 KB Output is correct
3 Correct 193 ms 112260 KB Output is correct
4 Correct 262 ms 131072 KB Output is correct
5 Correct 225 ms 131072 KB Output is correct
6 Correct 759 ms 131072 KB Output is correct
7 Correct 223 ms 131072 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Runtime error 188 ms 131072 KB Execution killed with signal 9
2 Halted 0 ms 0 KB -