Submission #985169

#TimeUsernameProblemLanguageResultExecution timeMemory
985169GhettoRoad Closures (APIO21_roads)C++17
0 / 100
2057 ms20280 KiB
#include "roads.h" #include <bits/stdc++.h> using namespace std; using lint = long long; using pii = pair<int, int>; using pll = pair<lint, lint>; const int MAX_N = 1e5 + 5; int n; vector<int> adj[MAX_N]; int n_inds, ind[MAX_N]; vector<int> children[MAX_N]; bool size_cmp(int u, int v) { return adj[u].size() > adj[v].size(); } void dfs1(int u, int par = -1) { n_inds++, ind[u] = n_inds; for (int v : adj[u]) if (v != par) dfs1(v, u), children[u].push_back(v); sort(children[u].begin(), children[u].end(), size_cmp); } vector<int> size_ord; void precomp() { dfs1(0); for (int u = 0; u < n; u++) size_ord.push_back(u); sort(size_ord.begin(), size_ord.end(), size_cmp); } unordered_set<int> seen; lint dp[MAX_N][2]; void dfs2(int u, int k) { seen.insert(u); int c0 = adj[u].size() - k, c1 = adj[u].size() - k - 1; vector<pll> vals; for (int v : children[u]) { if (adj[v].size() <= k) break; assert(!seen.count(v)); dfs2(v, k); vals.push_back({1 + dp[v][true] - dp[v][false], 1 + dp[v][true]}); } sort(vals.begin(), vals.end()); dp[u][false] = dp[u][true] = 0; while (vals.size() && (c0 || c1)) { if (c0) c0--, dp[u][false] += vals.back().second; if (c1) c1--, dp[u][true] += vals.back().second; vals.pop_back(); } } lint solve_k(int k) { vector<pii> bigs; lint ans = 0; for (int u : size_ord) { if (adj[u].size() <= k) break; bigs.push_back({ind[u], u}); ans += adj[u].size() - k; } sort(bigs.begin(), bigs.end()); seen.clear(); for (pii x : bigs) { int u = x.second; if (seen.count(u)) continue; dfs2(u, k); ans -= dp[u][false]; } return ans; } vector<lint> minimum_closure_costs(int tmp_n, vector<int> tmp_e1, vector<int> tmp_e2, vector<int> tmp_cost) { n = tmp_n; for (int i = 0; i < n - 1; i++) adj[tmp_e1[i]].push_back(tmp_e2[i]), adj[tmp_e2[i]].push_back(tmp_e1[i]); precomp(); vector<lint> ans; for (int k = 0; k < n; k++) ans.push_back(solve_k(k)); return ans; }

Compilation message (stderr)

roads.cpp: In function 'void dfs2(int, int)':
roads.cpp:35:27: warning: comparison of integer expressions of different signedness: 'std::vector<int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
   35 |         if (adj[v].size() <= k) break;
      |             ~~~~~~~~~~~~~~^~~~
roads.cpp: In function 'lint solve_k(int)':
roads.cpp:53:27: warning: comparison of integer expressions of different signedness: 'std::vector<int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
   53 |         if (adj[u].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...