이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include "roads.h"
#include <vector>
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = (int)1e5 +12;
vector<pair<int,int>> g[maxn];
ll dp[maxn][2];
int k;
int pos[maxn],id[maxn];
int dep[maxn];
vector<ll> pref[maxn],pref1[maxn];
int n;
int t[maxn * 4];
vector<int> P;
void build(int v = 1,int tl = 0,int tr = n - 1){
if(tl == tr) {
t[v] = (int)g[P[tl]].size();
}else {
int tm = (tl + tr) >> 1;
build(v + v,tl,tm);
build(v + v + 1,tm + 1,tr);
t[v] = max(t[v + v],t[v + v + 1]);
}
}
void upd(int pos,int val,int v = 1,int tl = 0,int tr = n - 1){
if(tl == tr) {
t[v] = val;
}else {
int tm = (tl + tr) >> 1;
if(pos <= tm) upd(pos,val,v+v,tl,tm);
else upd(pos,val,v+v+1,tm+1,tr);
t[v] = max(t[v + v],t[v + v + 1]);
}
}
int get(int v = 1,int tl = 0,int tr = n - 1){
if(t[v] <= k) return -1;
if(tl == tr){
return P[tl];
}else{
int tm = (tl + tr) >> 1;
if(t[v + v] > k) return get(v+v,tl,tm);
return get(v+v+1,tm+1,tr);
}
}
int temp[maxn],it[maxn],_j[maxn],ww[maxn];
vector<int> ii[maxn];
int vis[maxn],timer = 0,ff[maxn];
vector<pair<int,int>> bf,ch[maxn];
bool is_pr[maxn];
struct segtree{
int n;
vector<ll> c,t;
void init(int _n){
n = _n;
c.assign(_n * 4,0);
t.assign(_n * 4,0);
}
void acti(int pos,ll val,int v,int tl,int tr){
if(tl == tr){
c[v] = 1;
t[v] = val;
}else{
int tm = (tl + tr) >> 1;
if(pos <= tm) acti(pos,val,v+v,tl,tm);
else acti(pos,val,v+v+1,tm+1,tr);
c[v] = c[v + v] + c[v + v + 1];
t[v] = t[v + v] + t[v + v + 1];
}
}
ll getk(int k,int v,int tl,int tr){
if(k <= 0) return 0;
if(tl == tr) return t[v];
int tm = (tl + tr) >> 1;
if(c[v + v] >= k) return getk(k,v+v,tl,tm);
return t[v + v] + getk(k - c[v + v],v + v + 1,tm + 1,tr);
}
}tt[maxn];
int _p[maxn];
int col = 0;
void prec(int v,int pr = -1,int _w = 0) {
vector<pair<int,int>> aa;
ww[v] = _w;
tt[v].init((int)g[v].size());
_p[v] = pr;
for(auto [to,w]:g[v]) {
aa.push_back({w,to});
if(to == pr) continue;
ch[v].push_back({to,w});
dep[to] = dep[v] + 1;
prec(to,v,w);
}
sort(aa.rbegin(),aa.rend());
for(int j = 0;j < (int)aa.size();j++){
temp[aa[j].second] = j;
}
for(int j = 0;j < (int)ch[v].size();j++){
ii[v].push_back(temp[ch[v][j].first]);
}
if(pr != -1){
_j[v] = temp[pr];
col++;
assert(temp[pr] != -1);
}
it[v] = (int)ch[v].size() - 1;
}
void dfs(int v,int pr = -1){
// vis[v] = timer;
// // cout << id[v] << "A\n";
// upd(id[v],0);
// bf.push_back({id[v],(int)g[v].size()});
// ll S = 0;
// vector<ll> can;
// for(auto [to,w]:g[v]) {
// if(to == pr) continue;
// if((int)g[to].size() > k) {
// dfs(to,v);
// }else {
// dp[to][0] = dp[to][1] = 0;
// }
// S += dp[to][0] + w;
// can.push_back(dp[to][1] - (dp[to][0] + w));
// }
// sort(can.begin(),can.end());
// dp[v][0] = dp[v][1] = S;
// for(int i = 0;i < (int)can.size() && can[i] < 0;i++){
// if(i < k){
// dp[v][0] += can[i];
// }
// if(i < k - 1)
// {
// dp[v][1] += can[i];
// }
// }
vis[v] = timer;
upd(id[v],0);
bf.push_back({id[v],(int)g[v].size()});
ll S = 0;
vector<ll> can;
if(pr == -1 && v){
S += ww[v];
assert(g[_p[v]].size() <= k);
// assert(_j[v] != -1);
// tt[v].acti(_j[v],ww[v],1,0,tt[v].n-1);
can.push_back(-ww[v]);
}
for(auto [to,w]:ch[v]){
int sz = (int)g[to].size();
if(1 || sz > k){
if(sz > k) dfs(to,v);
else dp[to][0] = dp[to][1] = 0;
S += w + dp[to][0];
can.push_back(dp[to][1] - (dp[to][0] + w));
}else{
// while(it[v] >= i){
// tt[v].acti(ii[v][it[v]],ch[v][it[v]].second,1,0,tt[v].n-1);
// it[v]--;
// }
// break;
}
}
// S += tt[v].t[1];
sort(can.begin(),can.end());
ll mx0 = tt[v].getk(k,1,0,tt[v].n-1),mx1 = tt[v].getk(k-1,1,0,tt[v].n-1);
ll pre = 0;
for(int i = 0;i <(int)can.size();i++){
pre -= can[i];
if(i + 1 <= k){
mx0 = max(mx0,pre + tt[v].getk(k-i-1,1,0,tt[v].n-1));
}
if(i + 1 <= k - 1){
mx1 = max(mx1,pre + tt[v].getk(k-i-2,1,0,tt[v].n-1));
}
}
dp[v][0] = dp[v][1] = S;
dp[v][0] -= mx0;
dp[v][1] -= mx1;
}
vector<long long> minimum_closure_costs(int N,vector<int> U,vector<int> V,vector<int> W) {
n = N;
P.resize(n);
for(int i = 0;i < N - 1;i++){
g[U[i]].emplace_back(V[i],W[i]);
g[V[i]].emplace_back(U[i],W[i]);
}
for(int i = 0;i < N;i++) {
sort(g[i].begin(),g[i].end(),[&](pair<int,int> x,pair<int,int> y) {
return ((int)g[x.first].size()) > (int)g[y.first].size();
});
}
vector<ll> res;
prec(0);
assert(col == n - 1);
memset(_j,-1,sizeof(_j));
iota(P.begin(),P.end(),0);
sort(P.begin(),P.end(),[&](int x,int y){
return dep[x] < dep[y];
});
build();
for(int i = 0;i < N;i++) {
id[P[i]] = i;
}
for(int i = 0;i < N;i++) {
ll ss = 0;
++timer;
k = i;
bf.clear();
while(true){
int v = get();
if(v!=-1){
dfs(v);
ss += dp[v][0];
}else break;
}
for(auto [x,y]:bf){
upd(x,y);
}
res.push_back(ss);
// cout << ss << "x\n";
}
return res;
}
컴파일 시 표준 에러 (stderr) 메시지
roads.cpp: In function 'void prec(int, int, int)':
roads.cpp:87:14: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
87 | for(auto [to,w]:g[v]) {
| ^
In file included from /usr/include/c++/10/cassert:44,
from /usr/include/x86_64-linux-gnu/c++/10/bits/stdc++.h:33,
from roads.cpp:4:
roads.cpp: In function 'void dfs(int, int)':
roads.cpp:143:32: warning: comparison of integer expressions of different signedness: 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
143 | assert(g[_p[v]].size() <= k);
| ~~~~~~~~~~~~~~~~^~~~
roads.cpp:148:14: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
148 | for(auto [to,w]:ch[v]){
| ^
roads.cpp: In function 'std::vector<long long int> minimum_closure_costs(int, std::vector<int>, std::vector<int>, std::vector<int>)':
roads.cpp:217:18: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
217 | for(auto [x,y]:bf){
| ^
# | 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... |