Submission #566789

#TimeUsernameProblemLanguageResultExecution timeMemory
566789Ooops_sorryRoad Closures (APIO21_roads)C++14
0 / 100
138 ms30152 KiB
#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]; vector<bool> used(N); vector<ll> arr[N]; int n, k; ll dp[N][2]; struct Fenwick { vector<ll> t; void build(int n) { t.resize(n); } void inc(int i, ll val) { t[i] += val; for (; i < t.size(); i = (i | (i + 1))) { t[i] += val; } } ll get(int r) { ll ans = 0; for (; r >= 0; r = (r & (r + 1)) - 1) { ans += t[r]; } return ans; } } sum[N], cnt[N]; void add(int v, ll a) { int j = lower_bound(arr[v].begin(), arr[v].end(), a) - arr[v].begin(); sum[v].inc(j, a); cnt[v].inc(j, 1); } ll get(int v, int need) { need = (int)(g[v].size()) - need; if (need <= 0) return 0; int l = -1, r = arr[v].size(); while (r - l > 1) { int mid = (r + l) / 2; if (cnt[v].get(mid) >= need) { r = mid; } else { l = mid; } } return sum[v].get(r - 1) + (arr[v][r] * (need - cnt[v].get(r - 1))); } void dfs(int v, int p) { used[v] = 1; ll sum = 0; vector<ll> arr; for (auto to : g[v]) { if (to.first != p && g[to.first].size() > k) { dfs(to.first, v); sum += dp[to.first][0] + to.second; arr.pb({dp[to.first][1] - (dp[to.first][0] + to.second)}); } } sort(arr.begin(), arr.end()); dp[v][0] = dp[v][1] = INF; for (int i = 0; i <= min((int)arr.size(), k); i++) { dp[v][0] = min(dp[v][0], sum + get(v, k - i)); if (i < k) { dp[v][1] = min(dp[v][1], sum + get(v, k - i - 1)); } if (i < arr.size()) sum += arr[i]; } } vector<long long> minimum_closure_costs(int N, vector<int> u, vector<int> v, vector<int> w) { n = N; ll res = 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]}); res += w[i]; } vector<vector<int>> vert(n); vector<int> ord(n); iota(ord.begin(), ord.end(), 0); for (int i = 0; i < n; i++) { sort(g[i].begin(), g[i].end(), [&](pair<int,int> a, pair<int,int> b){return g[a.first].size() > g[b.first].size();}); for (auto to : g[i]) { arr[i].pb(to.second); } sort(arr[i].begin(), arr[i].end()); arr[i].erase(unique(arr[i].begin(), arr[i].end()), arr[i].end()); sum[i].build((int)arr[i].size()); cnt[i].build((int)arr[i].size()); vert[g[i].size()].pb(i); } vector<ll> ans(n); ans[0] = res; for (k = 1; k < n; k++) { for (auto v : vert[k]) { for (auto to : g[v]) { add(to.first, to.second); } } for (auto to : ord) { if (g[to].size() <= k) break; used[to] = 0; } for (auto to : ord) { if (g[to].size() <= k) break; if (!used[to]) { dfs(to, -1); ans[k] += dp[to][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 (stderr)

roads.cpp: In member function 'void Fenwick::inc(int, long long int)':
roads.cpp:27:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   27 |         for (; i < t.size(); i = (i | (i + 1))) {
      |                ~~^~~~~~~~~~
roads.cpp: In function 'void dfs(int, int)':
roads.cpp:66:49: warning: comparison of integer expressions of different signedness: 'std::vector<std::pair<int, long long int> >::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
   66 |         if (to.first != p && g[to.first].size() > k) {
      |                              ~~~~~~~~~~~~~~~~~~~^~~
roads.cpp:79:15: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   79 |         if (i < arr.size()) sum += arr[i];
      |             ~~^~~~~~~~~~~~
roads.cpp: In function 'std::vector<long long int> minimum_closure_costs(int, std::vector<int>, std::vector<int>, std::vector<int>)':
roads.cpp:114:30: warning: comparison of integer expressions of different signedness: 'std::vector<std::pair<int, long long int> >::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
  114 |             if (g[to].size() <= k) break;
      |                 ~~~~~~~~~~~~~^~~~
roads.cpp:118:30: warning: comparison of integer expressions of different signedness: 'std::vector<std::pair<int, long long int> >::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
  118 |             if (g[to].size() <= k) break;
      |                 ~~~~~~~~~~~~~^~~~
#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...
#Verdict Execution timeMemoryGrader output
Fetching results...