이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#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;
vector<lint> vals;
dp[u][false] = dp[u][true] = 0;
for (int v : children[u]) {
if (adj[v].size() <= k) break;
dfs2(v, k);
dp[u][false] += dp[v][false], dp[u][true] += dp[v][false];
if (1 + dp[v][true] - dp[v][false] < 0) continue;
vals.push_back(1 + dp[v][true] - dp[v][false]);
}
sort(vals.begin(), vals.end());
int c0 = adj[u].size() - k, c1 = adj[u].size() - k - 1;
while (vals.size() && (c0 || c1)) {
if (c0) c0--, dp[u][false] += vals.back();
if (c1) c1--, dp[u][true] += vals.back();
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;
}
컴파일 시 표준 에러 (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:54:27: warning: comparison of integer expressions of different signedness: 'std::vector<int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
54 | 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... |