Submission #559163

# Submission time Handle Problem Language Result Execution time Memory
559163 2022-05-09T12:54:49 Z acm Road Closures (APIO21_roads) C++14
31 / 100
2000 ms 25748 KB
#include <bits/stdc++.h>
#define FOR(i, l, r) for (int i = (l); i <= (r); ++i)
#define REP(i, l, r) for (int i = (l); i < (r); ++i)
#define FORD(i, r, l) for (int i = (r); i >= (l); --i)
#define REPD(i, r, l) for (int i = (r)-1; i >= (l); --i)
#define pb(x) push_back(x)
#define ll long long
#define pii pair<int, int>
using namespace std;
const int N = 1e5 + 5, Ns4 = 200005;
int n;
long long ans[N];
long long dpx[N], val[N];
int sz[Ns4];
long long dd[Ns4][2];
vector<pii> S[N];

void DFS(int u, int p, int max_sz) {
  int now_sz = min(sz[u], max_sz), w = 0;
  vector<int> Q;
  long long sum = 0, pre_sum = 0;
  for (auto [c, v] : S[u])
    if (v != p) {
      DFS(v, u, max_sz);
      long long xx = dd[v][1] + c;
      long long yy = dd[v][0];
      sum += xx;
      if (yy - xx < 0)
        Q.push_back(yy - xx);
      else
        w++;
    }
  for (int i = 0; i <= min(w, 1); i++) Q.pb(0);
  now_sz = min(now_sz, (int)Q.size());
  pre_sum = 1e15;
  random_shuffle(Q.begin(), Q.end());
  if (now_sz) {
    nth_element(Q.begin(), Q.begin() + now_sz - 1, Q.end());
    int alta = Q[now_sz - 1];
    int x = now_sz;
    for (auto X : Q) {
      if (X < alta) {
        x--;
        pre_sum = sum;
        sum += min(0, X);
      }
    }
    for (auto X : Q) {
      if (X == alta && x) {
        x--;
        pre_sum = sum;
        sum += min(0, X);
      }
    }
    Q.clear();
  }
  if (now_sz < max_sz) pre_sum = sum;
  dd[u][0] = pre_sum;
  dd[u][1] = sum;
}
void calc(int i) {
  DFS(0, 0, i);
  ans[i] = dd[0][1];
}
void dnc(int l = 1, int r = n - 2, ll L = ans[n - 1], ll R = ans[0]) {
  if (l > r) return;
  if (L == R) {
    for (int i = l; i <= r; i++) ans[i] = L;
    return;
  }
  int tm = (l + r) / 2;
  calc(tm);
  dnc(l, tm - 1, ans[tm], R);
  dnc(tm + 1, r, L, ans[tm]);
}
void sub34() {
  REP(i, 0, n) {
    sz[i] = S[i].size() - 1;
    if (i == 0) ++sz[i];
  }
  calc(0);
  calc(n - 1);
  if (1 <= n - 2) dnc();
}

vector<long long> minimum_closure_costs(int N, vector<int> U, vector<int> V,
                                        vector<int> W) {
  n = N;
  REP(i, 0, n - 1) {
    S[U[i]].emplace_back(W[i], V[i]);
    S[V[i]].emplace_back(W[i], U[i]);
  }
  sub34();
  vector<long long> ret(n);
  REP(i, 0, n) ret[i] = ans[i];
  return ret;
}

Compilation message

roads.cpp: In function 'void DFS(int, int, int)':
roads.cpp:22:13: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
   22 |   for (auto [c, v] : S[u])
      |             ^
# Verdict Execution time Memory Grader output
1 Correct 1 ms 2644 KB Output is correct
2 Correct 223 ms 2840 KB Output is correct
3 Correct 244 ms 2860 KB Output is correct
4 Correct 199 ms 2860 KB Output is correct
5 Correct 2 ms 2644 KB Output is correct
6 Correct 4 ms 2644 KB Output is correct
7 Correct 4 ms 2644 KB Output is correct
8 Correct 161 ms 2836 KB Output is correct
9 Correct 235 ms 2848 KB Output is correct
10 Correct 3 ms 2644 KB Output is correct
11 Correct 4 ms 2644 KB Output is correct
12 Execution timed out 2091 ms 8292 KB Time limit exceeded
13 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 2644 KB Output is correct
2 Correct 230 ms 21784 KB Output is correct
3 Correct 262 ms 24252 KB Output is correct
4 Correct 327 ms 25676 KB Output is correct
5 Correct 281 ms 25748 KB Output is correct
6 Correct 5 ms 3028 KB Output is correct
7 Correct 6 ms 3220 KB Output is correct
8 Correct 4 ms 3028 KB Output is correct
9 Correct 2 ms 2644 KB Output is correct
10 Correct 2 ms 2644 KB Output is correct
11 Correct 2 ms 2644 KB Output is correct
12 Correct 210 ms 16428 KB Output is correct
13 Correct 279 ms 25732 KB Output is correct
14 Correct 1 ms 2644 KB Output is correct
15 Correct 247 ms 23432 KB Output is correct
16 Correct 286 ms 25664 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 2644 KB Output is correct
2 Correct 2 ms 2644 KB Output is correct
3 Correct 2 ms 2664 KB Output is correct
4 Correct 2 ms 2644 KB Output is correct
5 Correct 2 ms 2644 KB Output is correct
6 Correct 2 ms 2644 KB Output is correct
7 Correct 2 ms 2644 KB Output is correct
8 Correct 2 ms 2644 KB Output is correct
9 Correct 2 ms 2644 KB Output is correct
10 Correct 2 ms 2644 KB Output is correct
11 Correct 2 ms 2644 KB Output is correct
12 Correct 2 ms 2644 KB Output is correct
13 Correct 3 ms 2644 KB Output is correct
14 Correct 4 ms 2644 KB Output is correct
15 Correct 4 ms 2632 KB Output is correct
16 Correct 2 ms 2644 KB Output is correct
17 Correct 2 ms 2644 KB Output is correct
18 Correct 2 ms 2644 KB Output is correct
19 Correct 4 ms 2644 KB Output is correct
20 Correct 4 ms 2644 KB Output is correct
21 Correct 2 ms 2644 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 2644 KB Output is correct
2 Correct 2 ms 2644 KB Output is correct
3 Correct 2 ms 2664 KB Output is correct
4 Correct 2 ms 2644 KB Output is correct
5 Correct 2 ms 2644 KB Output is correct
6 Correct 2 ms 2644 KB Output is correct
7 Correct 2 ms 2644 KB Output is correct
8 Correct 2 ms 2644 KB Output is correct
9 Correct 2 ms 2644 KB Output is correct
10 Correct 2 ms 2644 KB Output is correct
11 Correct 2 ms 2644 KB Output is correct
12 Correct 2 ms 2644 KB Output is correct
13 Correct 3 ms 2644 KB Output is correct
14 Correct 4 ms 2644 KB Output is correct
15 Correct 4 ms 2632 KB Output is correct
16 Correct 2 ms 2644 KB Output is correct
17 Correct 2 ms 2644 KB Output is correct
18 Correct 2 ms 2644 KB Output is correct
19 Correct 4 ms 2644 KB Output is correct
20 Correct 4 ms 2644 KB Output is correct
21 Correct 2 ms 2644 KB Output is correct
22 Correct 1 ms 2644 KB Output is correct
23 Correct 4 ms 2772 KB Output is correct
24 Correct 6 ms 2772 KB Output is correct
25 Correct 5 ms 2772 KB Output is correct
26 Correct 13 ms 2884 KB Output is correct
27 Correct 46 ms 2872 KB Output is correct
28 Correct 52 ms 2864 KB Output is correct
29 Correct 7 ms 2772 KB Output is correct
30 Correct 12 ms 2860 KB Output is correct
31 Correct 13 ms 2772 KB Output is correct
32 Correct 110 ms 2856 KB Output is correct
33 Correct 4 ms 3028 KB Output is correct
34 Correct 5 ms 3028 KB Output is correct
35 Correct 5 ms 3028 KB Output is correct
36 Correct 207 ms 2840 KB Output is correct
37 Correct 239 ms 2976 KB Output is correct
38 Correct 231 ms 2772 KB Output is correct
39 Correct 2 ms 2644 KB Output is correct
40 Correct 2 ms 2644 KB Output is correct
41 Correct 2 ms 2644 KB Output is correct
42 Correct 2 ms 2644 KB Output is correct
43 Correct 2 ms 2692 KB Output is correct
44 Correct 2 ms 2644 KB Output is correct
45 Correct 2 ms 2644 KB Output is correct
46 Correct 2 ms 2644 KB Output is correct
47 Correct 2 ms 2644 KB Output is correct
48 Correct 2 ms 2644 KB Output is correct
49 Correct 2 ms 2644 KB Output is correct
50 Correct 3 ms 2644 KB Output is correct
51 Correct 5 ms 2644 KB Output is correct
52 Correct 4 ms 2696 KB Output is correct
53 Correct 8 ms 2772 KB Output is correct
54 Correct 7 ms 2772 KB Output is correct
55 Correct 42 ms 2876 KB Output is correct
56 Correct 170 ms 2828 KB Output is correct
57 Correct 242 ms 2976 KB Output is correct
58 Correct 2 ms 2644 KB Output is correct
59 Correct 2 ms 2644 KB Output is correct
60 Correct 2 ms 2644 KB Output is correct
61 Correct 4 ms 2644 KB Output is correct
62 Correct 4 ms 2644 KB Output is correct
63 Correct 3 ms 2644 KB Output is correct
64 Correct 9 ms 2880 KB Output is correct
65 Correct 6 ms 2772 KB Output is correct
66 Correct 12 ms 2868 KB Output is correct
67 Correct 112 ms 2840 KB Output is correct
68 Correct 11 ms 2772 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 541 ms 12056 KB Output is correct
2 Correct 604 ms 11784 KB Output is correct
3 Execution timed out 2072 ms 11340 KB Time limit exceeded
4 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 541 ms 12056 KB Output is correct
2 Correct 604 ms 11784 KB Output is correct
3 Execution timed out 2072 ms 11340 KB Time limit exceeded
4 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 2644 KB Output is correct
2 Correct 223 ms 2840 KB Output is correct
3 Correct 244 ms 2860 KB Output is correct
4 Correct 199 ms 2860 KB Output is correct
5 Correct 2 ms 2644 KB Output is correct
6 Correct 4 ms 2644 KB Output is correct
7 Correct 4 ms 2644 KB Output is correct
8 Correct 161 ms 2836 KB Output is correct
9 Correct 235 ms 2848 KB Output is correct
10 Correct 3 ms 2644 KB Output is correct
11 Correct 4 ms 2644 KB Output is correct
12 Execution timed out 2091 ms 8292 KB Time limit exceeded
13 Halted 0 ms 0 KB -