답안 #900418

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
900418 2024-01-08T09:24:56 Z abcvuitunggio Islands (IOI08_islands) C++17
90 / 100
767 ms 131072 KB
#include <iostream>
#include <bitset>
#include <vector>
#include <queue>
using namespace std;
const int mxn=1000001;
const long long INF=1e18;
vector <int> ke[mxn],V[mxn];
queue <int> q;
int n,p[mxn],l[mxn],last[mxn],r[mxn],x,u;
bitset <mxn> ch;
long long dp[mxn],dp2[mxn],val,res,a,b,sum,s;
pair <long long, int> mx={-1,0},mx2={-1,0};
void dfs(int u){
    for (int v:ke[u]){
        dfs(v);
        dp[u]=max(dp[u],dp[v]+l[v]);
    }
}
void dfs2(int u){
    mx={-1,0},mx2={-1,0};
    for (int v:ke[u]){
        auto tmp=make_pair(dp[v]+l[v],v);
        if (tmp>mx){
            mx2=mx;
            mx=tmp;
        }
        else
            mx2=max(mx2,tmp);
    }
    for (int v:ke[u])
        dp2[v]=max(dp2[u],(mx.second==v?mx2.first:mx.first))+l[v];
    for (int v:ke[u])
        dfs2(v);
}
int f(int i){
    return (r[i]==i?i:r[i]=f(r[i]));
}
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];
        last[p[i]]++;
        r[i]=i;
    }
    for (int i=1;i<=n;i++)
        if (!last[i])
            q.push(i);
    while (!q.empty()){
        u=q.front();
        q.pop();
        ch[u]=1;
        r[f(u)]=f(p[u]);
        if (!--last[p[u]])
            q.push(p[u]);
    }
    for (int i=1;i<=n;i++)
        if (ch[i])
            ke[p[i]].push_back(i);
    for (int i=1;i<=n;i++)
        if (!ch[i]){
            dfs(i);
            dfs2(i);
        }
    for (int i=1;i<=n;i++){
        last[i]=0;
        vector <int>().swap(ke[i]);
        if (ch[i])
            V[f(i)].push_back(i);
    }
    for (int i=1;i<=n;i++){
        if (ch[i])
            continue;
        sum=0,a=-INF,b=-INF;
        x=i;
        while (true){
            sum+=l[x];
            ch[x]=1;
            last[p[x]]=x;
            x=p[x];
            if (x==i)
                break;
        }
        val=0,s=sum;
        for (x=last[i];;x=last[x]){
            for (int u:V[x])
                val=max(val,dp2[u]);
            val=max(val,dp[x]+max(a-s,b+sum+s));
            a=max(a,dp[x]+s);
            b=max(b,dp[x]-s);
            s-=l[last[x]];
            if (x==i)
                break;
        }
        res+=val;
    }
    cout << res;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 12 ms 55896 KB Output is correct
2 Correct 12 ms 55900 KB Output is correct
3 Correct 11 ms 55900 KB Output is correct
4 Correct 11 ms 55900 KB Output is correct
5 Correct 13 ms 55900 KB Output is correct
6 Correct 12 ms 55900 KB Output is correct
7 Correct 12 ms 55900 KB Output is correct
8 Correct 12 ms 55896 KB Output is correct
9 Correct 11 ms 56064 KB Output is correct
10 Correct 11 ms 56048 KB Output is correct
11 Correct 13 ms 56100 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 12 ms 56152 KB Output is correct
2 Correct 12 ms 55900 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 12 ms 56156 KB Output is correct
2 Correct 13 ms 56156 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 16 ms 56624 KB Output is correct
2 Correct 21 ms 59320 KB Output is correct
3 Correct 18 ms 57296 KB Output is correct
4 Correct 15 ms 56668 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 27 ms 60072 KB Output is correct
2 Correct 30 ms 59188 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 62 ms 64904 KB Output is correct
2 Correct 48 ms 63528 KB Output is correct
3 Correct 76 ms 67664 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 93 ms 79748 KB Output is correct
2 Correct 111 ms 83280 KB Output is correct
3 Correct 93 ms 71228 KB Output is correct
4 Correct 133 ms 87308 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 180 ms 94712 KB Output is correct
2 Correct 400 ms 92568 KB Output is correct
3 Correct 175 ms 93516 KB Output is correct
4 Correct 211 ms 100600 KB Output is correct
5 Correct 203 ms 101048 KB Output is correct
6 Correct 767 ms 112700 KB Output is correct
7 Correct 202 ms 99920 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Runtime error 225 ms 131072 KB Execution killed with signal 9
2 Halted 0 ms 0 KB -