이 제출은 이전 버전의 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,ll>> g[N];
ll e[N], val[N];
map<int, pair<ll, ll>> dp[N];
int k, n;
void dfs(int v, int p) {
vector<int> arr;
ll sum = 0;
int r = -1;
for (auto to : g[v]) {
if (to.first != p) {
e[to.first] = to.second;
dfs(to.first, v);
arr.pb(to.first);
}
}
sort(arr.begin(), arr.end(), [&](int a, int b){return dp[a].size() > dp[b].size();});
if (arr.size() > 0 && dp[arr[0]].size() > arr.size() + 1) {
r = arr[0];
swap(dp[v], dp[r]);
}
int sz = arr.size();
if (arr.size() > 0) {
sz = max(sz, (int)(dp[arr[0]].size()) - 1);
}
for (int i = 0; i <= sz; i++) {
ll sum = 0;
vector<int> mas;
for (auto to : arr) {
int need = to;
if (to == r) {
need = v;
}
if (dp[need].find(i) == dp[need].end()) {
sum += val[need];
mas.pb(e[need]);
continue;
}
sum += dp[need][i].first;
mas.pb({dp[need][i].second + e[need] - dp[need][i].first});
}
sort(mas.rbegin(), mas.rend());
if (arr.size() < i) {
for (auto to : mas) {
if (to < 0) {
break;
}
sum += to;
}
dp[v][i].first = dp[v][i].second = sum;
} else if (i == 0) {
dp[v][i].first = sum;
} else {
ll bst = sum;
dp[v][i].second = sum;
for (int j = 1; j <= i; j++) {
sum += mas[j - 1];
if (j < i) {
dp[v][i].second = max(dp[v][i].second, sum);
} else {
dp[v][i].first = sum;
}
}
dp[v][i].first = max(dp[v][i].first, dp[v][i].second);
}
}
}
void zhfs(int v, int p) {
for (auto to : g[v]) {
if (to.first != p) {
zhfs(to.first, v);
val[v] += to.second + val[to.first];
}
}
}
vector<long long> minimum_closure_costs(int N, vector<int> u, vector<int> v, vector<int> w) {
n = N;
ll sum = 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]});
sum += w[i];
}
vector<ll> ans;
zhfs(0, -1);
dfs(0, -1);
for (int i = 0; i < n; i++) {
if (i < dp[0].size()) {
ans.pb(sum - dp[0][i].first);
} else {
ans.pb(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:53:50: warning: narrowing conversion of '((dp[need].std::map<int, std::pair<long long int, long long int> >::operator[](i).std::pair<long long int, long long int>::second + e[need]) - dp[need].std::map<int, std::pair<long long int, long long int> >::operator[](i).std::pair<long long int, long long int>::first)' from 'long long int' to 'std::vector<int>::value_type' {aka 'int'} [-Wnarrowing]
53 | mas.pb({dp[need][i].second + e[need] - dp[need][i].first});
roads.cpp:56:24: warning: comparison of integer expressions of different signedness: 'std::vector<int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
56 | if (arr.size() < i) {
| ~~~~~~~~~~~^~~
roads.cpp:67:16: warning: unused variable 'bst' [-Wunused-variable]
67 | ll bst = sum;
| ^~~
roads.cpp:21:8: warning: unused variable 'sum' [-Wunused-variable]
21 | ll sum = 0;
| ^~~
roads.cpp: In function 'std::vector<long long int> minimum_closure_costs(int, std::vector<int>, std::vector<int>, std::vector<int>)':
roads.cpp:103:15: warning: comparison of integer expressions of different signedness: 'int' and 'std::map<int, std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
103 | if (i < dp[0].size()) {
| ~~^~~~~~~~~~~~~~
# | 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... |