답안 #548085

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
548085 2022-04-12T11:24:44 Z lorenzoferrari Islands (IOI08_islands) C++17
15 / 100
805 ms 131072 KB
/*

for each connected component, find the simple path with maximum sum

consider the main cycle
each node of the main cycle has a tree hanging from it
-> ez trovare il path massimo nel tree
-> let f(i) be the maximum path hanging from the tree in i
choose i,j to maximize maxDistCycle(i,j) + f(i) + f(j)

start from a generic node v

keep two sets:
  - L: {f(j) + dist_l(i, j)}
  - R: {f(j) + dist_r(i, j)}
bias_l = bias_r = 0

ans = max(L.max() + bias_l, R.max + bias_r)

transition, from v to u (dx di v):
  L,R.erase(u)
  L,R.insert(v)
  bias_l += L_{v,u}
  bias_r -= L_{v,u}

transition #cycle times

*/

#include <bits/stdc++.h>
using namespace std;
using LL = long long;

#define watch(x) cerr << (#x) << " is " << x << endl

vector<bool> vis, cycle;
vector<array<LL, 2>> prv, nxt;
vector<LL> dl, dr;
vector<LL> f;

LL max_path(const vector<int>& c, const vector<vector<array<LL, 3>>>& adj) {
  // identifica il ciclo
  auto find_cycle = [&](auto&& self, int v, int pid = -1) -> bool {
    if (vis[v]) return cycle[v] = true;
    vis[v] = true;
    for (auto [u, w, eid] : adj[v]) {
      if (eid == pid) continue;
      bool works = self(self, u, v);
      if (works) {
        nxt[v] = {u, w};
        prv[u] = {v, w};
        return cycle[v] ? false : cycle[v] = true;
      }
    }
    return false;
  };
  find_cycle(find_cycle, c[0]);
  auto path_in_tree = [&](auto&& self, int v, int pid = -1) -> LL {
    LL ans = 0;
    for (auto [u, w, eid] : adj[v]) {
      if (cycle[u] || eid == pid) continue;
      ans = max(ans, w + self(self, u, v));
    }
    return ans;
  };
  int start = -1;
  for (int i : c) {
    if (cycle[i]) {
      start = i;
      f[i] = path_in_tree(path_in_tree, i);
    }
  }
  LL len = 0;
  multiset<LL> L, R;
  {
    int v = start;
    int u = start;
    dl[v] = dr[u] = 0;
    do {
      auto [nv, lw] = nxt[v];
      auto [nu, rw] = prv[u];
      len += lw;
      if (v != start) L.insert(f[v] + dl[v]);
      if (u != start) R.insert(f[u] + dr[u]);
      if (nv != start) dl[nv] = dl[v] + lw;
      /*if (nu != start)*/ dr[nu] = dr[u] + rw;
      v = nv;
      u = nu;
    } while (v != start && u != start); // should happen in the same moment
  }
  LL bias = 0;
  LL ans = 0;
  {
    int v = start;
    do {
      // watch(v);
      // watch(bias);
      // cerr << "L: "; for (LL i : L) cerr << i << " "; cerr << endl;
      // cerr << "R: "; for (LL i : R) cerr << i << " "; cerr << endl;
      // cerr << endl;
      ans = max(ans, *prev(L.end()) - bias + f[v]);
      ans = max(ans, *prev(R.end()) + bias + f[v]);
      int nv = nxt[v][0];
      if (nv == start) break;
      bias += nxt[v][1];
      L.insert(f[v] + dl[v] + len);
      R.insert(f[v] + dr[v] - len);
      L.erase(L.find(f[nv] + dl[nv]));
      R.erase(R.find(f[nv] + dr[nv]));
      v = nv;
    } while (v != start);
  }
  // cerr << ans << endl;
  return ans;
}

int main() {
  int n; cin >> n;
  vector<vector<array<LL, 3>>> adj(n);
  for (int i = 0; i < n; ++i) {
    int a; cin >> a; --a;
    int w; cin >> w;
    adj[i].push_back({a, w, i});
    adj[a].push_back({i, w, i});
  }
  vis.assign(n, false);
  vector<vector<int>> cc;
  auto dfs = [&](auto&& self, int v) -> void {
    vis[v] = true;
    cc.back().push_back(v);
    for (auto [u, _, __] : adj[v]) {
      if (!vis[u]) {
        self(self, u);
      }
    }
  };
  for (int i = 0; i < n; ++i) {
    if (!vis[i]) {
      cc.push_back(vector<int>(0));
      dfs(dfs, i);
    }
  }
  f.assign(n, 0);
  dl.assign(n, 1e18);
  dr.assign(n, 1e18);
  prv.assign(n, {-1,-1});
  nxt.assign(n, {-1,-1});
  vis.assign(n, false);
  cycle.assign(n, false);
  LL ans = 0;
  for (const auto& c : cc) {
    ans += max_path(c, adj);
  }
  cout << ans << "\n";
}
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 212 KB Output is correct
2 Runtime error 78 ms 131072 KB Execution killed with signal 9
3 Correct 1 ms 340 KB Output is correct
4 Incorrect 0 ms 212 KB Output isn't correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 1 ms 212 KB Output is correct
7 Incorrect 1 ms 212 KB Output isn't correct
8 Runtime error 122 ms 131072 KB Execution killed with signal 9
9 Incorrect 0 ms 212 KB Output isn't correct
10 Incorrect 1 ms 212 KB Output isn't correct
11 Correct 1 ms 340 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Runtime error 82 ms 131072 KB Execution killed with signal 9
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Runtime error 78 ms 131072 KB Execution killed with signal 9
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Runtime error 93 ms 131072 KB Execution killed with signal 9
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Runtime error 125 ms 131072 KB Execution killed with signal 9
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Runtime error 209 ms 131072 KB Execution killed with signal 9
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Runtime error 356 ms 131072 KB Execution killed with signal 9
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Runtime error 757 ms 131072 KB Execution killed with signal 9
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Runtime error 805 ms 131072 KB Execution killed with signal 9
2 Halted 0 ms 0 KB -