답안 #900397

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
900397 2024-01-08T08:33:39 Z abcvuitunggio Islands (IOI08_islands) C++17
90 / 100
816 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],tmp;
int n,p[mxn],l[mxn],vis[mxn],vis2[mxn],root[mxn],idx,sz,x;
long long dp[mxn],dp2[mxn],d[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]){
        d[v]=d[u]+l[v];
        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 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];
        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;
}

Compilation message

islands.cpp: In function 'int main()':
islands.cpp:54:43: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   54 |                     for (int j=vis2[i]-1;j<tmp.size();j++){
      |                                          ~^~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 14 ms 69212 KB Output is correct
2 Correct 14 ms 69112 KB Output is correct
3 Correct 14 ms 69212 KB Output is correct
4 Correct 14 ms 69208 KB Output is correct
5 Correct 16 ms 69464 KB Output is correct
6 Correct 14 ms 69208 KB Output is correct
7 Correct 14 ms 69212 KB Output is correct
8 Correct 14 ms 69200 KB Output is correct
9 Correct 14 ms 69212 KB Output is correct
10 Correct 14 ms 69212 KB Output is correct
11 Correct 14 ms 69240 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 14 ms 69212 KB Output is correct
2 Correct 15 ms 69212 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 15 ms 69208 KB Output is correct
2 Correct 15 ms 69212 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 20 ms 72028 KB Output is correct
2 Correct 25 ms 74700 KB Output is correct
3 Correct 22 ms 72284 KB Output is correct
4 Correct 17 ms 71772 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 29 ms 75264 KB Output is correct
2 Correct 35 ms 75520 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 61 ms 81920 KB Output is correct
2 Correct 58 ms 81732 KB Output is correct
3 Correct 69 ms 84224 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 95 ms 93244 KB Output is correct
2 Correct 117 ms 96828 KB Output is correct
3 Correct 123 ms 97124 KB Output is correct
4 Correct 147 ms 105292 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 190 ms 105916 KB Output is correct
2 Correct 445 ms 113040 KB Output is correct
3 Correct 182 ms 110412 KB Output is correct
4 Correct 233 ms 125612 KB Output is correct
5 Correct 229 ms 121780 KB Output is correct
6 Correct 816 ms 131072 KB Output is correct
7 Correct 225 ms 131072 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Runtime error 193 ms 131072 KB Execution killed with signal 9
2 Halted 0 ms 0 KB -