Submission #566747

# Submission time Handle Problem Language Result Execution time Memory
566747 2022-05-22T19:30:40 Z Ooops_sorry Road Closures (APIO21_roads) C++14
7 / 100
2000 ms 60928 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 = 1; i < arr.size(); i++) {
        int u = arr[i];
        for (int j = sz + 1; j < dp[arr[i]].size(); j++) {
            dp[v][i].first += min(dp[u][i].first + e[u], dp[u][i].second);
            dp[v][i].second += min(dp[u][i].first + e[u], dp[u][i].second);
        }
    }
    for (int i = 0; i <= arr.size(); 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:37:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   37 |     for (int i = 1; i < arr.size(); i++) {
      |                     ~~^~~~~~~~~~~~
roads.cpp:39:32: 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]
   39 |         for (int j = sz + 1; j < dp[arr[i]].size(); j++) {
      |                              ~~^~~~~~~~~~~~~~~~~~~
roads.cpp:44:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   44 |     for (int i = 0; i <= arr.size(); i++) {
      |                     ~~^~~~~~~~~~~~~
roads.cpp:59: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]
   59 |             mas.pb({dp[need][i].second - (dp[need][i].first + e[to])});
roads.cpp:62:24: warning: comparison of integer expressions of different signedness: 'std::vector<int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
   62 |         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:105: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]
  105 |         if (i < dp[0].size()) {
      |             ~~^~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 4 ms 7252 KB Output is correct
2 Correct 265 ms 7764 KB Output is correct
3 Correct 279 ms 7744 KB Output is correct
4 Correct 202 ms 7704 KB Output is correct
5 Correct 4 ms 7380 KB Output is correct
6 Correct 5 ms 7380 KB Output is correct
7 Correct 5 ms 7380 KB Output is correct
8 Correct 130 ms 7792 KB Output is correct
9 Correct 249 ms 7860 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 2081 ms 16716 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 86 ms 51976 KB Output is correct
3 Correct 104 ms 57536 KB Output is correct
4 Correct 103 ms 60924 KB Output is correct
5 Correct 109 ms 60928 KB Output is correct
6 Correct 6 ms 8276 KB Output is correct
7 Correct 6 ms 8404 KB Output is correct
8 Correct 5 ms 8276 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 7368 KB Output is correct
12 Correct 61 ms 39368 KB Output is correct
13 Correct 100 ms 60872 KB Output is correct
14 Correct 4 ms 7252 KB Output is correct
15 Correct 84 ms 55668 KB Output is correct
16 Correct 94 ms 60904 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 7252 KB Output is correct
2 Correct 4 ms 7252 KB Output is correct
3 Correct 4 ms 7252 KB Output is correct
4 Incorrect 5 ms 7380 KB Output isn't correct
5 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 4 ms 7252 KB Output is correct
2 Correct 4 ms 7252 KB Output is correct
3 Correct 4 ms 7252 KB Output is correct
4 Incorrect 5 ms 7380 KB Output isn't correct
5 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 141 ms 25660 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 141 ms 25660 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 4 ms 7252 KB Output is correct
2 Correct 265 ms 7764 KB Output is correct
3 Correct 279 ms 7744 KB Output is correct
4 Correct 202 ms 7704 KB Output is correct
5 Correct 4 ms 7380 KB Output is correct
6 Correct 5 ms 7380 KB Output is correct
7 Correct 5 ms 7380 KB Output is correct
8 Correct 130 ms 7792 KB Output is correct
9 Correct 249 ms 7860 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 2081 ms 16716 KB Time limit exceeded
13 Halted 0 ms 0 KB -