이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
// #pragma GCC target ("avx,avx2,fma")
// #pragma GCC optimize ("Ofast,inline") // O1 - O2 - O3 - Os - Ofast
// #pragma GCC optimize ("unroll-loops")
#include <bits/stdc++.h>
using namespace std;
#define rep(i, a, b) for (int i = (a); i < (b); ++i)
#define per(i, a, b) for (int i = (b - 1); i >= (a); --i)
#define trav(a, x) for (auto &a : x)
#define all(x) x.begin(), x.end()
#define sz(x) x.size()
#define pb push_back
#define debug(x) cout << #x << " = " << x << endl
#define umap unordered_map
#define uset unordered_set
typedef pair<int, int> ii;
typedef pair<int, ii> iii;
typedef vector<int> vi;
typedef vector<ii> vii;
typedef vector<vi> vvi;
typedef long long ll;
typedef pair<ll, ll> pll;
typedef vector<ll> vll;
typedef vector<pll> vpll;
const int INF = 1'000'000'007;
ll n, k, vis[250001], deg[250001];
set<pll> graph[250001];
ll memo[250001][2], sums[250001];
vpll degree;
multiset<ll> pqs[250001];
void dp(ll u, ll p) {
vis[u] = k;
ll ans = sums[u];
vll diff;
trav(edge, graph[u]) if (edge.first != p) {
ll v, w;
tie(v, w) = edge;
dp(v, u);
ans += memo[v][0];
diff.emplace_back((memo[v][1] + w) - memo[v][0]);
}
sort(all(diff));
diff.emplace_back(1e17);
memo[u][0] = memo[u][1] = 1e14;
ll need_rem = max(0LL, deg[u] - k - (ll)sz(pqs[u]));
vll removed, added;
rep(i, 0, min(ll(sz(diff) - 1), need_rem)) {
ll val = diff[i];
added.pb(val);
pqs[u].insert(val);
ans += val;
}
if (sz(added) < need_rem)
memo[u][1] = ans;
else {
// debug(u);
rep(i, need_rem, sz(diff)) {
memo[u][0] = min(memo[u][0], ans);
memo[u][1] = min(memo[u][1], ans - max(0LL, *(--pqs[u].end())));
ll val = diff[i];
if (val < *(--pqs[u].end())) {
added.pb(val);
ans += val;
auto it = (--pqs[u].end());
removed.pb(*it);
ans -= *it;
pqs[u].erase(it);
pqs[u].insert(val);
} else if (val < 0)
ans += val;
else
break;
}
}
trav(val, added) pqs[u].erase(val);
trav(val, removed) pqs[u].insert(val);
// cout<<"u = "<<u+1<<" need_rem = "<<need_rem<<' '<<memo[u][0]<<' '<<memo[u][1]<<endl;
}
vector<long long> minimum_closure_costs(int N, vector<int> U, vector<int> V, vector<int> W) {
n = N;
memset(vis, -1, sizeof(vis));
memset(deg, 0, sizeof(deg));
memset(sums, 0, sizeof(sums));
degree.assign(n, {0, 0});
vll ans(n, 0);
rep(i, 0, n - 1) {
graph[U[i]].emplace(V[i], W[i]);
graph[V[i]].emplace(U[i], W[i]);
++degree[U[i]].first;
degree[U[i]].second = U[i];
++degree[V[i]].first;
degree[V[i]].second = V[i];
++deg[U[i]];
++deg[V[i]];
}
sort(all(degree));
ll removed = 0;
rep(i, 0, n) {
k = i;
while (removed < n && degree[removed].first <= k) {
int u = degree[removed].second;
trav(edge, graph[u]) {
ll v, w;
tie(v, w) = edge;
graph[v].erase({u, w});
sums[v] += w;
pqs[v].insert(w);
}
// debug(u+1);
++removed;
}
rep(j, removed, n) {
ll u = degree[j].second;
while (sz(pqs[u]) > deg[u] - k) {
auto it = (--pqs[u].end());
sums[u] -= *it;
pqs[u].erase(it);
}
}
rep(j, removed, n) if (vis[degree[j].second] != k) {
ll u = degree[j].second;
dp(u, u);
ans[k] += memo[u][0];
}
}
return ans;
}
/*
int main() {
int N;
assert(1 == scanf("%d", &N));
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]));
--U[i];
--V[i];
}
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;
}*/
컴파일 시 표준 에러 (stderr) 메시지
roads.cpp: In function 'void dp(ll, ll)':
roads.cpp:66:19: warning: comparison of integer expressions of different signedness: 'std::vector<long long int>::size_type' {aka 'long unsigned int'} and 'll' {aka 'long long int'} [-Wsign-compare]
66 | if (sz(added) < need_rem)
| ^
roads.cpp:9:42: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
9 | #define rep(i, a, b) for (int i = (a); i < (b); ++i)
| ^
roads.cpp:70:9: note: in expansion of macro 'rep'
70 | rep(i, need_rem, sz(diff)) {
| ^~~
roads.cpp: In function 'std::vector<long long int> minimum_closure_costs(int, std::vector<int>, std::vector<int>, std::vector<int>)':
roads.cpp:137:31: warning: comparison of integer expressions of different signedness: 'std::multiset<long long int>::size_type' {aka 'long unsigned int'} and 'll' {aka 'long long int'} [-Wsign-compare]
137 | while (sz(pqs[u]) > deg[u] - 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... |