답안 #900374

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
900374 2024-01-08T08:03:51 Z abcvuitunggio Islands (IOI08_islands) C++17
0 / 100
2000 ms 131076 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){
    pair <long long, int> 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];
        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;
            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);
        }
    delete ke;
    delete vis;
    delete vis2;
    for (int i=1;i<=n;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++){
      |                                          ~^~~~~~~~~~~
islands.cpp:74:12: warning: deleting array 'ke'
   74 |     delete ke;
      |            ^~
islands.cpp:75:12: warning: deleting array 'vis'
   75 |     delete vis;
      |            ^~~
islands.cpp:76:12: warning: deleting array 'vis2'
   76 |     delete vis2;
      |            ^~~~
# 결과 실행 시간 메모리 Grader output
1 Execution timed out 2342 ms 131072 KB Time limit exceeded
2 Execution timed out 2200 ms 131072 KB Time limit exceeded
3 Execution timed out 2154 ms 131072 KB Time limit exceeded
4 Execution timed out 2172 ms 131072 KB Time limit exceeded
5 Execution timed out 2158 ms 131072 KB Time limit exceeded
6 Runtime error 232 ms 131072 KB Execution killed with signal 9
7 Execution timed out 2164 ms 131072 KB Time limit exceeded
8 Execution timed out 2195 ms 131076 KB Time limit exceeded
9 Execution timed out 2322 ms 131076 KB Time limit exceeded
10 Runtime error 146 ms 131072 KB Execution killed with signal 9
11 Execution timed out 2229 ms 131072 KB Time limit exceeded
# 결과 실행 시간 메모리 Grader output
1 Execution timed out 2151 ms 131072 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Execution timed out 2294 ms 131072 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Execution timed out 2149 ms 131072 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Execution timed out 2195 ms 131072 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Runtime error 345 ms 131072 KB Execution killed with signal 9
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Runtime error 215 ms 131072 KB Execution killed with signal 9
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Execution timed out 2450 ms 131072 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Runtime error 207 ms 131072 KB Execution killed with signal 9
2 Halted 0 ms 0 KB -