이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#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,int>> g[N];
ll e[N], dp[N][2];
int k, n;
void dfs(int v, int p) {
vector<int> arr;
ll sum = 0;
for (auto to : g[v]) {
if (to.first != p) {
e[to.first] = to.second;
dfs(to.first, v);
arr.pb(to.first);
sum += dp[to.first][0] + to.second;
}
}
sort(arr.begin(), arr.end(), [&](int u, int v){return dp[u][1] - (dp[u][0] + e[u]) < dp[v][1] - (dp[v][0] + e[v]);});
if (k != 0) {
dp[v][1] = min(dp[v][1], sum);
}
if (arr.size() < k) {
for (auto to : arr) {
sum += dp[to][1] - (dp[to][0] + e[to]);
}
dp[v][0] = dp[v][1] = sum;
} else {
if (k == 0) {
dp[v][0] = sum;
} else {
for (int i = 1; i <= k; i++) {
int u = arr[i - 1];
sum += dp[u][1] - (dp[u][0] + e[u]);
if (i < k) {
dp[v][1] = min(dp[v][1], sum);
} else {
dp[v][0] = sum;
}
}
}
}
}
vector<long long> minimum_closure_costs(int N, vector<int> u, vector<int> v, vector<int> w) {
n = N;
for (int i = 0; i < n - 1; i++) {
g[u[i]].pb({v[i], w[i]});
g[v[i]].pb({u[i], w[i]});
}
vector<ll> ans;
for (k = 0; k < n; k++) {
for (int i = 0; i < n; i++) {
dp[i][0] = dp[i][1] = INF;
}
dfs(0, -1);
ans.pb(dp[0][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
컴파일 시 표준 에러 (stderr) 메시지
roads.cpp: In function 'void dfs(int, int)':
roads.cpp:33:20: warning: comparison of integer expressions of different signedness: 'std::vector<int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
33 | if (arr.size() < k) {
| ~~~~~~~~~~~^~~
# | 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... |