이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
//#include "roads.h"
#include <bits/stdc++.h>
#define speed ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0)
#define ll long long
#define pii pair<ll, ll>
#define f first
#define s second
#define mp make_pair
#define pinode pair<node*, node*>
#define pb push_back
using namespace std;
const int N = 3e5 + 10;
mt19937 bruh(chrono::steady_clock::now().time_since_epoch().count());
struct node {
node *l = nullptr, *r = nullptr;
ll sz = 1, y = 0, sum = 0, x = 0;
node(ll nx) {
x = nx;
sz = 1;
l = r = nullptr;
y = bruh();
sum = nx;
}
} *rt[N];
void pull(node *a) {
if(!a)return;
a->sum = a->x;
a->sz = 1;
if(a->l)a->sum += a->l->sum, a->sz += a->l->sz;
if(a->r)a->sum += a->r->sum, a->sz += a->r->sz;
}
node *mrg(node *a, node *b) {
if(!a || !b)
return (a ? a : b);
if(a->y > b->y) {
a->r = mrg(a->r, b);
pull(a);
return a;
}
b->l = mrg(a, b->l);
pull(b);
return b;
}
pinode splitSz(node *a, ll F) {
if(!a)
return mp(nullptr, nullptr);
if((a->l ? a->l->sz : 0) >= F) {
pinode z = splitSz(a->l, F);
a->l = z.s;
pull(a);
return mp(z.f, a);
}
else {
ll dlt = F - (a->l ? a->l->sz + 1 : 1);
pinode z = splitSz(a->r, dlt);
a->r = z.f;
pull(a);
return mp(a, z.s);
}
}
pinode splitX(node *a, ll x) {
if(!a)
return mp(nullptr, nullptr);
if(a->x <= x) {
pinode z = splitX(a->r, x);
a->r = z.f;
pull(a);
return mp(a, z.s);
}
else {
pinode z = splitX(a->l, x);
a->l = z.s;
pull(a);
return mp(z.f, a);
}
}
ll get(ll v, ll del) {
if(del == -1)return 0;
pinode z = splitSz(rt[v], del);
ll sum = (z.f ? z.f->sum : 0);
rt[v] = mrg(z.f, z.s);
return sum;
}
void insert(ll v, ll zn) {
pinode z = splitX(rt[v], zn);
node *u = new node(zn);
rt[v] = mrg(mrg(z.f, u), z.s);
}
void dele(ll v, ll zn) {
pinode z = splitX(rt[v], zn - 1);
pinode z2 = splitSz(z.s, 1);
rt[v] = mrg(z.f, z2.s);
}
ll dp[N][2];
bool was[N];
vector<int> act;
vector<pii> g[N];
vector<pii> g2[N];
vector<int> st[N];
void solve(ll v, ll p, ll x) {
ll del = g2[v].size() - x;
was[v] = 1;
for(auto u : g[v]) {
if(u.f != p) {
solve(u.f, v, x);
}
}
ll sum = 0;
for(auto u : g[v]) {
if(u.f != p) {
sum += dp[u.f][1];
if(dp[u.f][0] + u.s - dp[u.f][1] < 0)
sum += dp[u.f][0] + u.s - dp[u.f][1], del--;
else
insert(v, dp[u.f][0] + u.s - dp[u.f][1]);
}
}
dp[v][0] = sum + get(v, del - 1);
dp[v][1] = sum + get(v, del);
for(auto u : g[v]) {
if(u.f != p) {
if(dp[u.f][0] + u.s - dp[u.f][1] >= 0)
dele(v, dp[u.f][0] + u.s - dp[u.f][1]);
}
}
}
vector<ll> minimum_closure_costs(int n, vector<int> U, vector<int> V, vector<int> W) {
for(int i = 0; i < N; i++)
rt[i] = nullptr;
vector<ll> answ;
for(auto &u : U)u++;
for(auto &u : V)u++;
for(int i = 1; i < n; i++) {
g2[U[i - 1]].pb(mp(V[i - 1], W[i - 1]));
g2[V[i - 1]].pb(mp(U[i - 1], W[i - 1]));
}
for(int i = 1; i <= n; i++) {
st[g2[i].size()].pb(i);
}
for(int j = n - 1; j >= 0; j--) {
for(auto v : st[j]) {
act.pb(v);
for(auto u : g2[v]) {
if(g2[u.f].size() > j) {
g[u.f].pb(mp(v, u.s));
g[v].pb(u);
dele(u.f, u.s);
}
else if(g2[u.f].size() == j) {
g[v].pb(u);
}
else {
insert(v, u.s);
}
}
}
ll ans = 0;
for(auto u : act) {
if(!was[u]) {
solve(u, 0, j);
ans += dp[u][1];
}
}
answ.pb(ans);
for(auto u : act)was[u] = 0, dp[u][0] = dp[u][1] = 0;
}
reverse(answ.begin(), answ.end());
return answ;
}
/*
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;
}
/*
7
1 0 2
2 1 3
3 2 7
4 3 7
5 4 7
6 5 7
*/
컴파일 시 표준 에러 (stderr) 메시지
roads.cpp:212:1: warning: "/*" within comment [-Wcomment]
212 | /*
|
roads.cpp: In function 'std::vector<long long int> minimum_closure_costs(int, std::vector<int>, std::vector<int>, std::vector<int>)':
roads.cpp:165:23: warning: comparison of integer expressions of different signedness: 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
165 | if(g2[u.f].size() > j) {
| ~~~~~~~~~~~~~~~^~~
roads.cpp:170:28: warning: comparison of integer expressions of different signedness: 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
170 | else if(g2[u.f].size() == j) {
| ~~~~~~~~~~~~~~~^~~~
# | 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... |