This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#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);
}
bool seen[MAX_N];
lint dp[MAX_N][2];
void dfs2(int u, int k) {
seen[u] = true;
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;
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());
for (pii x : bigs) seen[x.second] = false;
for (pii x : bigs) {
int u = x.second;
if (seen[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:52:27: warning: comparison of integer expressions of different signedness: 'std::vector<int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
52 | if (adj[u].size() <= k) break;
| ~~~~~~~~~~~~~~^~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |