Submission #566743

# Submission time Handle Problem Language Result Execution time Memory
566743 2022-05-22T19:26:19 Z Ooops_sorry Road Closures (APIO21_roads) C++14
31 / 100
2000 ms 59388 KB
#include<bits/stdc++.h>
#ifndef LOCAL
    #include "roads.h"
#endif

#define ld long double
#define ll long long
#define pb push_back

using namespace std;

const int N = 1e5 + 10;
const ll INF = 1e18;
vector<pair<int,ll>> g[N];
ll e[N], val[N];
map<int, pair<ll, ll>> dp[N];
int k, n;

void dfs(int v, int p) {
    vector<int> arr;
    ll sum = 0;
    int r = -1;
    for (auto to : g[v]) {
        if (to.first != p) {
            e[to.first] = to.second;
            dfs(to.first, v);
            arr.pb(to.first);
        }
    }
    sort(arr.begin(), arr.end(), [&](int a, int b){return dp[a].size() > dp[b].size();});
    int sz = arr.size();
    if (arr.size() > 0 && dp[arr[0]].size() > arr.size() + 1) {
        swap(dp[v], dp[arr[0]]);
        r = arr[0];
        sz = (int)(dp[v].size()) - 1;
    }
    for (int i = 0; i <= sz; i++) {
        ll sum = 0;
        vector<int> mas;
        for (auto to : arr) {
            int need = to;
            if (to == r) {
                need = v;
            }
            if (dp[need].find(i) == dp[need].end()) {
                assert(need != v);
                sum += e[need];
                mas.pb(-e[need]);
                continue;
            }
            sum += dp[need][i].first + e[to];
            mas.pb({dp[need][i].second - (dp[need][i].first + e[to])});
        }
        sort(mas.begin(), mas.end());
        if (arr.size() < i) {
            for (auto to : mas) {
                if (to > 0) break;
                sum += to;
            }
            dp[v][i].first = dp[v][i].second = sum;
        } else if (i == 0) {
            dp[v][i].first = sum;
        } else {
            dp[v][i].second = sum;
            for (int j = 1; j <= i; j++) {
                sum += mas[j - 1];
                if (j < i) {
                    dp[v][i].second = min(dp[v][i].second, sum);
                } else {
                    dp[v][i].first = min(dp[v][i].second, sum);
                }
            }
        }
    }
}

void zhfs(int v, int p) {
    for (auto to : g[v]) {
        if (to.first != p) {
            zhfs(to.first, v);
            val[v] += to.second + val[to.first];
        }
    }
}

vector<long long> minimum_closure_costs(int N, vector<int> u, vector<int> v, vector<int> w) {
    n = N;
    ll sum = 0;
    for (int i = 0; i < n - 1; i++) {
        g[u[i]].pb({v[i], w[i]});
        g[v[i]].pb({u[i], w[i]});
        sum += w[i];
    }
    vector<ll> ans;
    zhfs(0, -1);
    dfs(0, -1);
    for (int i = 0; i < n; i++) {
        if (i < dp[0].size()) {
            ans.pb(dp[0][i].first);
        } else {
            ans.pb(0);
        }
    }
    return ans;
}

#ifdef LOCAL

int main() {
  int N;
  assert(1 == scanf("%d", &N));

  std::vector<int> U(N - 1), V(N - 1), W(N - 1);
  for (int i = 0; i < N - 1; ++i) {
    assert(3 == scanf("%d %d %d", &U[i], &V[i], &W[i]));
  }

  std::vector<long long> closure_costs = minimum_closure_costs(N, U, V, W);
  for (int i = 0; i < static_cast<int>(closure_costs.size()); ++i) {
    if (i > 0) {
      printf(" ");
    }
    printf("%lld",closure_costs[i]);
  }
  printf("\n");
  return 0;
}

#endif

Compilation message

roads.cpp: In function 'void dfs(int, int)':
roads.cpp:52:40: warning: narrowing conversion of '(dp[need].std::map<int, std::pair<long long int, long long int> >::operator[](i).std::pair<long long int, long long int>::second - (dp[need].std::map<int, std::pair<long long int, long long int> >::operator[](i).std::pair<long long int, long long int>::first + e[to]))' from 'long long int' to 'std::vector<int>::value_type' {aka 'int'} [-Wnarrowing]
   52 |             mas.pb({dp[need][i].second - (dp[need][i].first + e[to])});
roads.cpp:55:24: warning: comparison of integer expressions of different signedness: 'std::vector<int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
   55 |         if (arr.size() < i) {
      |             ~~~~~~~~~~~^~~
roads.cpp:21:8: warning: unused variable 'sum' [-Wunused-variable]
   21 |     ll sum = 0;
      |        ^~~
roads.cpp: In function 'std::vector<long long int> minimum_closure_costs(int, std::vector<int>, std::vector<int>, std::vector<int>)':
roads.cpp:98:15: warning: comparison of integer expressions of different signedness: 'int' and 'std::map<int, std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   98 |         if (i < dp[0].size()) {
      |             ~~^~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 4 ms 7252 KB Output is correct
2 Correct 236 ms 7756 KB Output is correct
3 Correct 266 ms 7764 KB Output is correct
4 Correct 175 ms 7884 KB Output is correct
5 Correct 5 ms 7380 KB Output is correct
6 Correct 6 ms 7300 KB Output is correct
7 Correct 5 ms 7380 KB Output is correct
8 Correct 129 ms 7692 KB Output is correct
9 Correct 209 ms 7756 KB Output is correct
10 Correct 5 ms 7380 KB Output is correct
11 Correct 5 ms 7380 KB Output is correct
12 Execution timed out 2097 ms 16712 KB Time limit exceeded
13 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 4 ms 7252 KB Output is correct
2 Correct 84 ms 50684 KB Output is correct
3 Correct 95 ms 56068 KB Output is correct
4 Correct 101 ms 59328 KB Output is correct
5 Correct 101 ms 59328 KB Output is correct
6 Correct 5 ms 8148 KB Output is correct
7 Correct 6 ms 8276 KB Output is correct
8 Correct 6 ms 8260 KB Output is correct
9 Correct 5 ms 7380 KB Output is correct
10 Correct 5 ms 7380 KB Output is correct
11 Correct 4 ms 7420 KB Output is correct
12 Correct 61 ms 38472 KB Output is correct
13 Correct 95 ms 59388 KB Output is correct
14 Correct 4 ms 7252 KB Output is correct
15 Correct 87 ms 54224 KB Output is correct
16 Correct 95 ms 59332 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 7252 KB Output is correct
2 Correct 3 ms 7252 KB Output is correct
3 Correct 3 ms 7252 KB Output is correct
4 Correct 4 ms 7380 KB Output is correct
5 Correct 5 ms 7380 KB Output is correct
6 Correct 4 ms 7380 KB Output is correct
7 Correct 4 ms 7380 KB Output is correct
8 Correct 4 ms 7380 KB Output is correct
9 Correct 4 ms 7380 KB Output is correct
10 Correct 4 ms 7380 KB Output is correct
11 Correct 5 ms 7380 KB Output is correct
12 Correct 4 ms 7380 KB Output is correct
13 Correct 4 ms 7380 KB Output is correct
14 Correct 6 ms 7380 KB Output is correct
15 Correct 5 ms 7380 KB Output is correct
16 Correct 4 ms 7380 KB Output is correct
17 Correct 5 ms 7380 KB Output is correct
18 Correct 4 ms 7380 KB Output is correct
19 Correct 5 ms 7380 KB Output is correct
20 Correct 5 ms 7380 KB Output is correct
21 Correct 4 ms 7252 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 7252 KB Output is correct
2 Correct 3 ms 7252 KB Output is correct
3 Correct 3 ms 7252 KB Output is correct
4 Correct 4 ms 7380 KB Output is correct
5 Correct 5 ms 7380 KB Output is correct
6 Correct 4 ms 7380 KB Output is correct
7 Correct 4 ms 7380 KB Output is correct
8 Correct 4 ms 7380 KB Output is correct
9 Correct 4 ms 7380 KB Output is correct
10 Correct 4 ms 7380 KB Output is correct
11 Correct 5 ms 7380 KB Output is correct
12 Correct 4 ms 7380 KB Output is correct
13 Correct 4 ms 7380 KB Output is correct
14 Correct 6 ms 7380 KB Output is correct
15 Correct 5 ms 7380 KB Output is correct
16 Correct 4 ms 7380 KB Output is correct
17 Correct 5 ms 7380 KB Output is correct
18 Correct 4 ms 7380 KB Output is correct
19 Correct 5 ms 7380 KB Output is correct
20 Correct 5 ms 7380 KB Output is correct
21 Correct 4 ms 7252 KB Output is correct
22 Correct 4 ms 7252 KB Output is correct
23 Correct 5 ms 7508 KB Output is correct
24 Correct 8 ms 7780 KB Output is correct
25 Correct 5 ms 7636 KB Output is correct
26 Correct 6 ms 7764 KB Output is correct
27 Correct 26 ms 7760 KB Output is correct
28 Correct 24 ms 7780 KB Output is correct
29 Correct 6 ms 7636 KB Output is correct
30 Correct 9 ms 7764 KB Output is correct
31 Correct 8 ms 7740 KB Output is correct
32 Correct 60 ms 7732 KB Output is correct
33 Correct 5 ms 8148 KB Output is correct
34 Correct 6 ms 8372 KB Output is correct
35 Correct 6 ms 8276 KB Output is correct
36 Correct 231 ms 7788 KB Output is correct
37 Correct 272 ms 7760 KB Output is correct
38 Correct 188 ms 7756 KB Output is correct
39 Correct 3 ms 7252 KB Output is correct
40 Correct 4 ms 7252 KB Output is correct
41 Correct 4 ms 7392 KB Output is correct
42 Correct 4 ms 7380 KB Output is correct
43 Correct 4 ms 7380 KB Output is correct
44 Correct 5 ms 7380 KB Output is correct
45 Correct 4 ms 7396 KB Output is correct
46 Correct 4 ms 7380 KB Output is correct
47 Correct 5 ms 7380 KB Output is correct
48 Correct 4 ms 7380 KB Output is correct
49 Correct 4 ms 7376 KB Output is correct
50 Correct 5 ms 7380 KB Output is correct
51 Correct 5 ms 7380 KB Output is correct
52 Correct 5 ms 7380 KB Output is correct
53 Correct 6 ms 7588 KB Output is correct
54 Correct 6 ms 7728 KB Output is correct
55 Correct 24 ms 7764 KB Output is correct
56 Correct 125 ms 7764 KB Output is correct
57 Correct 214 ms 7832 KB Output is correct
58 Correct 4 ms 7252 KB Output is correct
59 Correct 4 ms 7380 KB Output is correct
60 Correct 5 ms 7380 KB Output is correct
61 Correct 5 ms 7364 KB Output is correct
62 Correct 5 ms 7380 KB Output is correct
63 Correct 4 ms 7252 KB Output is correct
64 Correct 6 ms 7636 KB Output is correct
65 Correct 6 ms 7744 KB Output is correct
66 Correct 9 ms 7764 KB Output is correct
67 Correct 47 ms 7752 KB Output is correct
68 Correct 8 ms 7764 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 141 ms 25668 KB Output is correct
2 Correct 149 ms 25336 KB Output is correct
3 Correct 987 ms 28468 KB Output is correct
4 Correct 135 ms 26188 KB Output is correct
5 Correct 1032 ms 30520 KB Output is correct
6 Execution timed out 2087 ms 18348 KB Time limit exceeded
7 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 141 ms 25668 KB Output is correct
2 Correct 149 ms 25336 KB Output is correct
3 Correct 987 ms 28468 KB Output is correct
4 Correct 135 ms 26188 KB Output is correct
5 Correct 1032 ms 30520 KB Output is correct
6 Execution timed out 2087 ms 18348 KB Time limit exceeded
7 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 4 ms 7252 KB Output is correct
2 Correct 236 ms 7756 KB Output is correct
3 Correct 266 ms 7764 KB Output is correct
4 Correct 175 ms 7884 KB Output is correct
5 Correct 5 ms 7380 KB Output is correct
6 Correct 6 ms 7300 KB Output is correct
7 Correct 5 ms 7380 KB Output is correct
8 Correct 129 ms 7692 KB Output is correct
9 Correct 209 ms 7756 KB Output is correct
10 Correct 5 ms 7380 KB Output is correct
11 Correct 5 ms 7380 KB Output is correct
12 Execution timed out 2097 ms 16712 KB Time limit exceeded
13 Halted 0 ms 0 KB -