답안 #990410

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
990410 2024-05-30T11:17:32 Z huutuan Islands (IOI08_islands) C++14
90 / 100
432 ms 131072 KB
#include<bits/stdc++.h>

using namespace std;

// #pragma GCC optimize("trapv")

// #define int long long

const int N=1e6+10;
int n, vis[N], par[N], wpar[N], on_cycle[N];
vector<pair<int, int>> g[N];
long long pf[N], f[N][2];

void dfs(int u){
   vis[u]=1;
   long long mx1=-1e18, mx2=-1e18;
   for (auto &e:g[u]){
      int v=e.first, w=e.second;
      if (on_cycle[v]) continue;
      dfs(v);
      long long val=f[v][0]+w;
      f[u][0]=max(f[u][0], val);
      f[u][1]=max(f[u][1], f[v][1]);
      if (mx1<val) mx2=mx1, mx1=val;
      else mx2=max(mx2, val);
   }
   f[u][1]=max({f[u][1], f[u][0], mx1+mx2});
}

int32_t main(){
   ios_base::sync_with_stdio(false);
   cin.tie(nullptr);
   cin >> n;
   for (int i=1; i<=n; ++i){
      int j, k; cin >> j >> k;
      g[j].emplace_back(i, k);
      par[i]=j;
      wpar[i]=k;
   }
   vector<long long> ans;
   for (int i=1; i<=n; ++i) if (!vis[i]){
      long long cur=0;
      vector<int> cycle;
      int u=i;
      while (1){
         if (vis[u]) break;
         vis[u]=1;
         cycle.push_back(u);
         u=par[u];
      }
      cycle.erase(cycle.begin(), find(cycle.begin(), cycle.end(), u));
      for (int v:cycle) on_cycle[v]=1;
      for (int v:cycle){
         dfs(v);
         cur=max(cur, f[v][1]);
      }
      for (int j=1; j<=(int)cycle.size(); ++j){
         pf[j]=pf[j-1]+wpar[cycle[j-1]];
      }
      long long sum=pf[(int)cycle.size()];
      long long mx1=-1e18, mx2=-1e18;
      for (int j=0; j<(int)cycle.size(); ++j){
         cur=max(cur, f[cycle[j]][0]+pf[j]+mx1);
         cur=max(cur, f[cycle[j]][0]-pf[j]+mx2);
         mx1=max(mx1, f[cycle[j]][0]-pf[j]);
         mx2=max(mx2, f[cycle[j]][0]+sum+pf[j]);
      }
      ans.push_back(cur);
   }
   cout << accumulate(ans.begin(), ans.end(), 0ll) << '\n';
   return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 29016 KB Output is correct
2 Correct 5 ms 29020 KB Output is correct
3 Correct 6 ms 29020 KB Output is correct
4 Correct 4 ms 29020 KB Output is correct
5 Correct 4 ms 29148 KB Output is correct
6 Correct 4 ms 29020 KB Output is correct
7 Correct 4 ms 29132 KB Output is correct
8 Correct 4 ms 29020 KB Output is correct
9 Correct 4 ms 29020 KB Output is correct
10 Correct 4 ms 29020 KB Output is correct
11 Correct 4 ms 29020 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 29020 KB Output is correct
2 Correct 5 ms 29276 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 29276 KB Output is correct
2 Correct 5 ms 29276 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 7 ms 30044 KB Output is correct
2 Correct 12 ms 31064 KB Output is correct
3 Correct 10 ms 30300 KB Output is correct
4 Correct 6 ms 29532 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 15 ms 31836 KB Output is correct
2 Correct 21 ms 34680 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 40 ms 43868 KB Output is correct
2 Correct 41 ms 50108 KB Output is correct
3 Correct 49 ms 52168 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 73 ms 57428 KB Output is correct
2 Correct 88 ms 63128 KB Output is correct
3 Correct 93 ms 76004 KB Output is correct
4 Correct 112 ms 75976 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 138 ms 77240 KB Output is correct
2 Correct 308 ms 99460 KB Output is correct
3 Correct 134 ms 78672 KB Output is correct
4 Correct 168 ms 91436 KB Output is correct
5 Correct 170 ms 89080 KB Output is correct
6 Correct 432 ms 90500 KB Output is correct
7 Correct 176 ms 97472 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Runtime error 190 ms 131072 KB Execution killed with signal 9
2 Halted 0 ms 0 KB -