답안 #900434

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
900434 2024-01-08T09:55:44 Z abcvuitunggio Islands (IOI08_islands) C++17
90 / 100
647 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];
queue <int> q;
int n,p[mxn],l[mxn],last[mxn],r[mxn],x,u;
bitset <mxn> ch;
long long dp[mxn],d[mxn],val,res,a,b,sum,s;
int dfs(int u){
    int mx=u;
    for (int v:ke[u]){
        int w=dfs(v);
        if (dp[v]+l[v]>dp[u])
            mx=w;
        dp[u]=max(dp[u],dp[v]+l[v]);
    }
    return mx;
}
long long dfs2(int u, int par){
    long long mx=0;
    for (int v:ke[u])
        if (v!=par)
            mx=max(mx,dfs2(v,u)+l[v]);
    if (ch[u]&&p[u]!=par)
        mx=max(mx,dfs2(p[u],u)+l[u]);
    return mx;
}
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])
            d[i]=dfs2(dfs(i),0);
    for (int i=1;i<=n;i++){
        last[i]=0;
        vector <int>().swap(ke[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]){
            val=max(max(val,d[x]),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 6 ms 32600 KB Output is correct
2 Correct 6 ms 32604 KB Output is correct
3 Correct 6 ms 32464 KB Output is correct
4 Correct 6 ms 32468 KB Output is correct
5 Correct 6 ms 32604 KB Output is correct
6 Correct 7 ms 32604 KB Output is correct
7 Correct 7 ms 32604 KB Output is correct
8 Correct 7 ms 32600 KB Output is correct
9 Correct 7 ms 32604 KB Output is correct
10 Correct 6 ms 32604 KB Output is correct
11 Correct 6 ms 32632 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 8 ms 32604 KB Output is correct
2 Correct 8 ms 32444 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 7 ms 32604 KB Output is correct
2 Correct 8 ms 32680 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 10 ms 33112 KB Output is correct
2 Correct 14 ms 35676 KB Output is correct
3 Correct 12 ms 33372 KB Output is correct
4 Correct 9 ms 32860 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 17 ms 35928 KB Output is correct
2 Correct 22 ms 36444 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 48 ms 39788 KB Output is correct
2 Correct 39 ms 39764 KB Output is correct
3 Correct 50 ms 41812 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 75 ms 52820 KB Output is correct
2 Correct 91 ms 56688 KB Output is correct
3 Correct 81 ms 50032 KB Output is correct
4 Correct 115 ms 59884 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 154 ms 51048 KB Output is correct
2 Correct 283 ms 58964 KB Output is correct
3 Correct 141 ms 64584 KB Output is correct
4 Correct 178 ms 69924 KB Output is correct
5 Correct 176 ms 71040 KB Output is correct
6 Correct 647 ms 73452 KB Output is correct
7 Correct 181 ms 68180 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Runtime error 214 ms 131072 KB Execution killed with signal 9
2 Halted 0 ms 0 KB -