Submission #1037880

# Submission time Handle Problem Language Result Execution time Memory
1037880 2024-07-29T09:38:20 Z juicy Designated Cities (JOI19_designated_cities) C++17
100 / 100
396 ms 39076 KB
// https://codeforces.com/blog/entry/66022?#comment-501121

#include <bits/stdc++.h>

using namespace std;

#ifdef LOCAL
#include "debug.h"
#else
#define debug(...) 42
#endif

const int N = 2e5 + 5;

int n, q;
int sz[N], c[N];
bool del[N];
long long dp[N], f[N], ans[N];
vector<array<int, 2>> g[N];

void pre_dfs(int u, int p) {
  for (auto [v, w] : g[u]) {
    if (v ^ p) {
      pre_dfs(v, u);
      dp[u] += dp[v] + w;  
    } else {
      c[u] = w;
    }
  }
}

void reroot(int u, int p) {
  for (auto [v, w] : g[u]) {
    if (v != p) {
      f[v] = f[u] + dp[u] - dp[v] - w + c[v];
      reroot(v, u);
    }
  }
}

long long get(int u, int p, vector<long long> &costs) {
  long long mx = 0;
  for (auto [v, w] : g[u]) {
    if (!del[v] && v != p) {
      auto nxt = get(v, u, costs) + w;
      if (nxt > mx) {
        swap(nxt, mx);
      }
      if (nxt) {
        costs.push_back(nxt);
      }
    }
  }
  return mx;
}

void dfs(int u, int p) {
  sz[u] = 1;
  for (auto [v, w] : g[u]) {
    if (v != p && !del[v]) {
      dfs(v, u);
      sz[u] += sz[v];
    }
  }
}

int findCen(int u, int p, int tot) {
  for (auto [v, w] : g[u]) {
    if (v != p && !del[v] && sz[v] * 2 > tot) {
      return findCen(v, u, tot);
    }
  }
  return u;
}

void cd(int u) {
  dfs(u, u);
  u = findCen(u, u, sz[u]);
  del[u] = 1;
  vector<pair<long long, int>> cands;
  for (auto [v, w] : g[u]) {
    if (!del[v]) {
      vector<long long> costs;
      long long mx = get(v, u, costs);
      costs.push_back(mx + w);
      for (auto x : costs) {
        cands.push_back({x, v});
      }
    }
  }
  sort(cands.rbegin(), cands.rend());
  long long tot = dp[u] + f[u], sum = 0;
  ans[1] = min(ans[1], tot);
  for (int i = 0; i < cands.size(); ++i) {
    sum += cands[i].first;
    ans[i + 2] = min(ans[i + 2], tot - sum);
  }
  int j = -1;
  for (int i = 1; i < cands.size(); ++i) {
    if (cands[i].second != cands[0].second) {
      j = i;
      break;
    }
  }
  if (~j) {
    long long sum = cands[j].first;
    for (int i = 0, sz = 1; i < cands.size(); ++i) {
      if (i != j) {
        sum += cands[i].first, ++sz;
        ans[sz] = min(ans[sz], tot - sum);
      }
    } 
  }
  for (auto [v, w] : g[u]) {
    if (!del[v]) {
      cd(v);
    }
  }
}

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

  cin >> n;
  for (int i = 1; i < n; ++i) {
    int u, v, c, d; cin >> u >> v >> c >> d;
    g[u].push_back({v, c});
    g[v].push_back({u, d});
  }
  pre_dfs(1, 1);
  reroot(1, 1);
  memset(ans, 0x3f, sizeof(ans));
  cd(1);
  for (int i = 2; i <= n; ++i) {
    ans[i] = min(ans[i], ans[i - 1]);
  }
  cin >> q;
  while (q--) {
    int e; cin >> e;
    cout << ans[e] << "\n";
  }
  return 0;
}

Compilation message

designated_cities.cpp: In function 'void cd(int)':
designated_cities.cpp:94:21: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<long long int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   94 |   for (int i = 0; i < cands.size(); ++i) {
      |                   ~~^~~~~~~~~~~~~~
designated_cities.cpp:99:21: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<long long int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   99 |   for (int i = 1; i < cands.size(); ++i) {
      |                   ~~^~~~~~~~~~~~~~
designated_cities.cpp:107:31: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<long long int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  107 |     for (int i = 0, sz = 1; i < cands.size(); ++i) {
      |                             ~~^~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 1 ms 8796 KB Output is correct
2 Correct 1 ms 8792 KB Output is correct
3 Correct 2 ms 8796 KB Output is correct
4 Correct 1 ms 8796 KB Output is correct
5 Correct 1 ms 8796 KB Output is correct
6 Correct 1 ms 8796 KB Output is correct
7 Correct 1 ms 8796 KB Output is correct
8 Correct 1 ms 8796 KB Output is correct
9 Correct 1 ms 8844 KB Output is correct
10 Correct 2 ms 8796 KB Output is correct
11 Correct 1 ms 8796 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 8796 KB Output is correct
2 Correct 275 ms 28412 KB Output is correct
3 Correct 354 ms 36948 KB Output is correct
4 Correct 245 ms 27128 KB Output is correct
5 Correct 141 ms 27464 KB Output is correct
6 Correct 340 ms 30680 KB Output is correct
7 Correct 121 ms 27464 KB Output is correct
8 Correct 376 ms 37972 KB Output is correct
9 Correct 97 ms 30696 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 8796 KB Output is correct
2 Correct 277 ms 28356 KB Output is correct
3 Correct 347 ms 38904 KB Output is correct
4 Correct 267 ms 27036 KB Output is correct
5 Correct 137 ms 27468 KB Output is correct
6 Correct 328 ms 30988 KB Output is correct
7 Correct 96 ms 30780 KB Output is correct
8 Correct 371 ms 35444 KB Output is correct
9 Correct 90 ms 30276 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 8796 KB Output is correct
2 Correct 1 ms 8792 KB Output is correct
3 Correct 2 ms 8796 KB Output is correct
4 Correct 1 ms 8796 KB Output is correct
5 Correct 1 ms 8796 KB Output is correct
6 Correct 1 ms 8796 KB Output is correct
7 Correct 1 ms 8796 KB Output is correct
8 Correct 1 ms 8796 KB Output is correct
9 Correct 1 ms 8844 KB Output is correct
10 Correct 2 ms 8796 KB Output is correct
11 Correct 1 ms 8796 KB Output is correct
12 Correct 1 ms 8796 KB Output is correct
13 Correct 3 ms 9052 KB Output is correct
14 Correct 3 ms 9052 KB Output is correct
15 Correct 3 ms 8852 KB Output is correct
16 Correct 3 ms 9052 KB Output is correct
17 Correct 3 ms 8796 KB Output is correct
18 Correct 3 ms 9052 KB Output is correct
19 Correct 3 ms 8796 KB Output is correct
20 Correct 3 ms 9052 KB Output is correct
21 Correct 3 ms 8936 KB Output is correct
22 Correct 3 ms 9052 KB Output is correct
23 Correct 3 ms 9052 KB Output is correct
24 Correct 2 ms 9016 KB Output is correct
25 Correct 3 ms 9140 KB Output is correct
26 Correct 2 ms 8852 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 8796 KB Output is correct
2 Correct 275 ms 28412 KB Output is correct
3 Correct 354 ms 36948 KB Output is correct
4 Correct 245 ms 27128 KB Output is correct
5 Correct 141 ms 27464 KB Output is correct
6 Correct 340 ms 30680 KB Output is correct
7 Correct 121 ms 27464 KB Output is correct
8 Correct 376 ms 37972 KB Output is correct
9 Correct 97 ms 30696 KB Output is correct
10 Correct 1 ms 8796 KB Output is correct
11 Correct 277 ms 28356 KB Output is correct
12 Correct 347 ms 38904 KB Output is correct
13 Correct 267 ms 27036 KB Output is correct
14 Correct 137 ms 27468 KB Output is correct
15 Correct 328 ms 30988 KB Output is correct
16 Correct 96 ms 30780 KB Output is correct
17 Correct 371 ms 35444 KB Output is correct
18 Correct 90 ms 30276 KB Output is correct
19 Correct 1 ms 8792 KB Output is correct
20 Correct 279 ms 28304 KB Output is correct
21 Correct 353 ms 39076 KB Output is correct
22 Correct 250 ms 27416 KB Output is correct
23 Correct 268 ms 28516 KB Output is correct
24 Correct 254 ms 27520 KB Output is correct
25 Correct 278 ms 28420 KB Output is correct
26 Correct 250 ms 27152 KB Output is correct
27 Correct 130 ms 27492 KB Output is correct
28 Correct 335 ms 30456 KB Output is correct
29 Correct 247 ms 27748 KB Output is correct
30 Correct 230 ms 29200 KB Output is correct
31 Correct 108 ms 30536 KB Output is correct
32 Correct 393 ms 35700 KB Output is correct
33 Correct 89 ms 30272 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 8796 KB Output is correct
2 Correct 1 ms 8792 KB Output is correct
3 Correct 2 ms 8796 KB Output is correct
4 Correct 1 ms 8796 KB Output is correct
5 Correct 1 ms 8796 KB Output is correct
6 Correct 1 ms 8796 KB Output is correct
7 Correct 1 ms 8796 KB Output is correct
8 Correct 1 ms 8796 KB Output is correct
9 Correct 1 ms 8844 KB Output is correct
10 Correct 2 ms 8796 KB Output is correct
11 Correct 1 ms 8796 KB Output is correct
12 Correct 1 ms 8796 KB Output is correct
13 Correct 275 ms 28412 KB Output is correct
14 Correct 354 ms 36948 KB Output is correct
15 Correct 245 ms 27128 KB Output is correct
16 Correct 141 ms 27464 KB Output is correct
17 Correct 340 ms 30680 KB Output is correct
18 Correct 121 ms 27464 KB Output is correct
19 Correct 376 ms 37972 KB Output is correct
20 Correct 97 ms 30696 KB Output is correct
21 Correct 1 ms 8796 KB Output is correct
22 Correct 277 ms 28356 KB Output is correct
23 Correct 347 ms 38904 KB Output is correct
24 Correct 267 ms 27036 KB Output is correct
25 Correct 137 ms 27468 KB Output is correct
26 Correct 328 ms 30988 KB Output is correct
27 Correct 96 ms 30780 KB Output is correct
28 Correct 371 ms 35444 KB Output is correct
29 Correct 90 ms 30276 KB Output is correct
30 Correct 1 ms 8796 KB Output is correct
31 Correct 3 ms 9052 KB Output is correct
32 Correct 3 ms 9052 KB Output is correct
33 Correct 3 ms 8852 KB Output is correct
34 Correct 3 ms 9052 KB Output is correct
35 Correct 3 ms 8796 KB Output is correct
36 Correct 3 ms 9052 KB Output is correct
37 Correct 3 ms 8796 KB Output is correct
38 Correct 3 ms 9052 KB Output is correct
39 Correct 3 ms 8936 KB Output is correct
40 Correct 3 ms 9052 KB Output is correct
41 Correct 3 ms 9052 KB Output is correct
42 Correct 2 ms 9016 KB Output is correct
43 Correct 3 ms 9140 KB Output is correct
44 Correct 2 ms 8852 KB Output is correct
45 Correct 1 ms 8792 KB Output is correct
46 Correct 279 ms 28304 KB Output is correct
47 Correct 353 ms 39076 KB Output is correct
48 Correct 250 ms 27416 KB Output is correct
49 Correct 268 ms 28516 KB Output is correct
50 Correct 254 ms 27520 KB Output is correct
51 Correct 278 ms 28420 KB Output is correct
52 Correct 250 ms 27152 KB Output is correct
53 Correct 130 ms 27492 KB Output is correct
54 Correct 335 ms 30456 KB Output is correct
55 Correct 247 ms 27748 KB Output is correct
56 Correct 230 ms 29200 KB Output is correct
57 Correct 108 ms 30536 KB Output is correct
58 Correct 393 ms 35700 KB Output is correct
59 Correct 89 ms 30272 KB Output is correct
60 Correct 1 ms 8796 KB Output is correct
61 Correct 298 ms 28988 KB Output is correct
62 Correct 348 ms 38740 KB Output is correct
63 Correct 282 ms 27488 KB Output is correct
64 Correct 252 ms 29592 KB Output is correct
65 Correct 256 ms 28552 KB Output is correct
66 Correct 281 ms 29028 KB Output is correct
67 Correct 265 ms 27444 KB Output is correct
68 Correct 158 ms 28104 KB Output is correct
69 Correct 328 ms 31676 KB Output is correct
70 Correct 272 ms 28384 KB Output is correct
71 Correct 249 ms 28876 KB Output is correct
72 Correct 131 ms 29696 KB Output is correct
73 Correct 396 ms 35876 KB Output is correct
74 Correct 117 ms 30644 KB Output is correct