제출 #1345772

#제출 시각아이디문제언어결과실행 시간메모리
1345772avighnaWorst Reporter 4 (JOI21_worst_reporter4)C++20
14 / 100
2096 ms33020 KiB
#include <bits/stdc++.h>

using namespace std;

const int64_t inf = 1e15;

int main() {
  cin.tie(nullptr)->sync_with_stdio(false);

  int n;
  cin >> n;
  vector<int> h(n + 1), c(n + 1), next(n + 1), buff;
  for (int i = 1, a; i <= n; ++i) {
    cin >> a >> h[i] >> c[i];
    next[i] = a;
    buff.push_back(h[i]);
  }

  vector<int> vis(n + 1), vis_rn(n + 1), in_cycle(n + 1), cycle_of(n + 1);
  vector<vector<int>> cycle_nodes(n + 1);
  int cycle_num = 0;
  for (int u = 1; u <= n; ++u) {
    if (vis[u]) {
      continue;
    }
    int i = u;
    vector<int> stk;
    while (!vis[i] && !vis_rn[i]) {
      vis_rn[i] = 1;
      stk.push_back(i);
      i = next[i];
    }
    if (vis[i]) { // lead to a cycle
      int node = i;
      for (int i = u; !vis[i]; i = next[i]) {
        vis_rn[i] = false;
        cycle_of[i] = cycle_of[node];
      }
    } else { // we formed a cycle
      cycle_of[i] = cycle_num++, in_cycle[i] = 1;
      cycle_nodes[cycle_of[i]].push_back(i);
      for (int j = next[i]; j != i; j = next[j]) {
        cycle_of[j] = cycle_of[i], in_cycle[j] = 1;
        cycle_nodes[cycle_of[j]].push_back(j);
      }
    }
    for (int &i : stk) {
      vis[i] = vis[i] | exchange(vis_rn[i], 0);
    }
  }
  vector<vector<int>> adj(n + 1);
  vector<int> cycle_head(n + 1);
  for (auto &v : cycle_nodes) {
    if (v.empty()) {
      continue;
    }
    sort(v.begin(), v.end(), [&](int i, int j) { return h[i] < h[j]; });
    cycle_head[cycle_of[v[0]]] = v[0];
    for (int i = 0; i < v.size() - 1; ++i) {
      adj[v[i + 1]].push_back(v[i]);
    }
  }
  for (int u = 1; u <= n; ++u) {
    if (in_cycle[u]) {
      continue;
    }
    if (in_cycle[next[u]]) {
      adj[cycle_head[cycle_of[next[u]]]].push_back(u);
    } else {
      adj[next[u]].push_back(u);
    }
  }
  vector<int> indeg(n + 1);
  for (int u = 1; u <= n; ++u) {
    for (int &i : adj[u]) {
      indeg[i]++;
    }
  }

  sort(buff.begin(), buff.end());
  buff.erase(unique(buff.begin(), buff.end()), buff.end());
  auto compr = [&](int i) { return lower_bound(buff.begin(), buff.end(), i) - buff.begin(); };
  for (int i = 1; i <= n; ++i) {
    h[i] = compr(h[i]);
  }
  auto dfs = [&](auto &&self, int u) -> map<int, int64_t> {
    map<int, int64_t> dp;
    dp[h[u]] = -c[u], dp[0] = c[u] * (h[u] != 0), dp[h[u] + 1] = c[u];
    for (int &i : adj[u]) {
      auto ch = self(self, i);
      if (dp.size() < ch.size()) {
        swap(dp, ch);
      }
      for (auto &[i, val] : ch) {
        dp[i] += val;
      }
    }
    for (auto it = dp.find(h[u]); it != dp.begin() && it->second < 0; it = --dp.erase(it)) {
      prev(it)->second += exchange(it->second, 0);
    }
    return dp;
  };
  int64_t ans = 0;
  for (int u = 1; u <= n; ++u) {
    if (indeg[u] == 0) {
      ans += dfs(dfs, u).begin()->second;
    }
  }
  cout << ans << '\n';
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...