이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pii = pair<int, int>;
using pil = pair<int, ll>;
using pll = pair<ll, ll>;
#define pb push_back
#define ff first
#define ss second
#define arr3 array<ll, 3>
struct DS{
multiset<ll> st;
void add(ll x){
st.insert(x);
}
void rem(ll x){
st.erase(st.find(x));
}
int sz(){
return (int) st.size();
}
ll get(int k){
if (st.empty()) return 0;
k = min(k, sz());
ll out = 0;
auto it = prev(st.end());
for (int i = 0; i < k; i++){
out += (*it);
if (i == (k - 1)) break;
it--;
}
return out;
}
};
vector<ll> minimum_closure_costs(int n, vector<int> U, vector<int> V, vector<int> W){
vector<pii> g[n + 1];
ll tot = 0;
for (int i = 0; i < n - 1; i++){
int u = U[i] + 1, v = V[i] + 1, w = W[i];
g[u].pb({v, w});
g[v].pb({u, w});
tot += w;
}
vector<pil> ch1[n + 1], ch2[n + 1];
vector<pll> p(n + 1);
vector<ll> W1(n + 1);
function<void(int, int)> dfs = [&](int v, int pr){
if ((g[v].size() + !pr) == 1){
ch1[v] = {{0, 0}};
ch2[v] = {{0, 0}};
return;
}
for (auto [i, w]: g[v]){
if (i == pr) continue;
dfs(i, v);
}
vector<arr3> t;
DS T;
for (auto [i, w]: g[v]){
if (i == pr) continue;
p[i].ff = p[i].ss = 0;
T.add(w);
W1[i] = w;
for (auto [j, x]: ch1[i]){
t.pb({j, i, x});
}
for (auto [j, x]: ch2[i]){
t.pb({j, -i, x});
}
}
sort(t.begin(), t.end());
int p1 = 0; ll sum = 0;
while (p1 < t.size()){
int p2 = p1;
while (p2 < t.size() && t[p1][0] == t[p2][0]){
auto [j, i, x] = t[p2];
if (i > 0){
ll f = p[i].ff + W1[i] - p[i].ss;
if (f > 0) T.rem(f);
p[i].ff = x;
f = p[i].ff + W1[i] - p[i].ss;
if (f > 0) T.add(f);
}
else {
i = -i;
sum -= p[i].ss;
ll f = p[i].ff + W1[i] - p[i].ss;
if (f > 0) T.rem(f);
p[i].ss = x;
f = p[i].ff + W1[i] - p[i].ss;
if (f > 0) T.add(f);
sum += p[i].ss;
}
p2++;
}
ch2[v].pb({t[p1][0], sum + T.get((int) t[p1][0])});
if (!t[p1][0]) ch1[v].pb({0, sum});
else ch1[v].pb({t[p1][0], sum + T.get((int) t[p1][0] - 1)});
int R = (int)((p2 == (int) t.size()) ? n : t[p2][0]);
for (int j = (int) t[p1][0] + 1; j <= min(R - 1, T.sz()); j++){
ch2[v].pb({j, sum + T.get(j)});
}
for (int j = (int) t[p1][0] + 1; j <= min(R - 1, T.sz() + 1); j++){
if (!j) ch1[v].pb({0, sum});
else ch1[v].pb({j, sum + T.get(j - 1)});
}
p1 = p2;
}
};
dfs(1, 0);
vector<ll> dp(n);
for (int i = 0; i < ch2[1].size(); i++){
int R = (i == (ch2[1].size() - 1)) ? n : ch2[1][i + 1].ff;
for (int j = ch2[1][i].ff; j < R; j++){
dp[j] = ch2[1][i].ss;
}
}
vector<ll> out;
for (int i = 0; i < n; i++){
out.pb(tot - dp[i]);
}
return out;
}
컴파일 시 표준 에러 (stderr) 메시지
roads.cpp: In lambda function:
roads.cpp:78:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::array<long long int, 3> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
78 | while (p1 < t.size()){
| ~~~^~~~~~~~~~
roads.cpp:80:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::array<long long int, 3> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
80 | while (p2 < t.size() && t[p1][0] == t[p2][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:120:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
120 | for (int i = 0; i < ch2[1].size(); i++){
| ~~^~~~~~~~~~~~~~~
roads.cpp:121:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
121 | int R = (i == (ch2[1].size() - 1)) ? n : ch2[1][i + 1].ff;
| ~~^~~~~~~~~~~~~~~~~~~~~~
# | 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... |