Submission #1184214

#TimeUsernameProblemLanguageResultExecution timeMemory
1184214avighnaDesignated Cities (JOI19_designated_cities)C++20
100 / 100
293 ms63028 KiB
#include <bits/stdc++.h>

int main() {
  std::ios_base::sync_with_stdio(false);
  std::cin.tie(nullptr);

  int n;
  std::cin >> n;
  std::vector<std::vector<std::pair<int, std::pair<long long, long long>>>> adj(
      n + 1);
  long long tot = 0;
  for (int i = 0, a, b, c, d; i < n - 1; ++i) {
    std::cin >> a >> b >> c >> d;
    adj[a].push_back({b, {c, d}});
    adj[b].push_back({a, {d, c}});
    tot += c + d;
  }

  std::vector<long long> path_up(n + 1), path_down(n + 1), ans(n + 1);
  std::vector<std::pair<long long, int>> max(n + 1);
  std::vector<int> par(n + 1);
  long long up = 0;
  auto dfs = [&](auto &&self, int u, int p) -> void {
    max[u] = {path_down[u], u};
    for (auto &[i, pr] : adj[u]) {
      if (i == p) {
        continue;
      }
      par[i] = u;
      auto [c, d] = pr;
      up += d;
      path_up[i] = path_up[u] + d;
      path_down[i] = path_down[u] + c;
      self(self, i, u);
      max[u] = std::max(max[u], max[i]);
    }
  };
  dfs(dfs, 1, 0);
  std::pair<long long, std::pair<int, int>> s;
  auto dfs1 = [&](auto &&self, int u, int p,
                  long long dep) -> std::pair<long long, int> {
    std::pair<long long, int> down = {path_down[u], u};
    std::vector<std::pair<long long, int>> v;
    for (auto &[i, pr] : adj[u]) {
      if (i == p) {
        continue;
      }
      auto [c, d] = pr;
      v.push_back(self(self, i, u, dep + c));
      down = std::max(down, v.back());
    }
    long long r = up + dep - path_up[u];
    s = std::max(s, {r + (down.first - path_down[u]), {u, down.second}});
    std::sort(v.rbegin(), v.rend());
    if (v.size() > 1) {
      s = std::max(s, {r + v[0].first + v[1].first - 2 * path_down[u],
                       {v[0].second, v[1].second}});
    }
    return down;
  };
  dfs1(dfs1, 1, 0, 0);

  // root at one of the start nodes and expand: anyways you will end up with the
  // first node being picked as the other start node
  for (int i = 1; i <= n; ++i) {
    ans[1] = std::max(ans[1], up - path_up[i] + path_down[i]);
  }
  ans[2] = s.first;
  int &node = s.second.first;
  up = 0;
  path_down.assign(n + 1, 0), path_up.assign(n + 1, 0);
  dfs(dfs, node, 0);
  int idx = 1;
  std::priority_queue<std::pair<long long, int>> pq;
  pq.push(max[node]);
  std::vector<bool> vis(n + 1);
  while (!pq.empty()) {
    auto [d, tar] = pq.top();
    pq.pop();
    if (++idx != 2) {
      ans[idx] = ans[idx - 1] + d;
    }
    while (!vis[tar]) {
      vis[tar] = true;
      for (auto &[i, pr] : adj[tar]) {
        if (!vis[i] and par[tar] != i) {
          pq.push({max[i].first - path_down[tar], max[i].second});
        }
      }
      tar = par[tar];
    }
  }
  for (int i = idx; i < ans.size(); ++i) {
    ans[i] = tot;
  }

  int q, x;
  std::cin >> q;
  while (q--) {
    std::cin >> x;
    std::cout << tot - ans[x] << '\n';
  }
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...