This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include<iostream>
#include<vector>
#include<cassert>
#include<algorithm>
#include "roads.h"
using namespace std;
#define mp make_pair
#define fr first
#define sc second
#define pb push_back
#define all(x) x.begin(),x.end()
#define sz(x) int32_t(x.size())
#define int long long
const int maxn = 1e5+10;
const int inf = 1e18+10;
const int mnv = -1e14-10;
const int mxv = 1e14+10;
int n, dp[maxn][2], mark[maxn], atv[maxn];
vector<int> g[maxn], gr[maxn];
vector<pair<int,int>> gesp[maxn];
int cnt[maxn];
vector<int> trq[maxn], trs[maxn], f1[maxn], f2[maxn];
//add/remove pos from id
int att(int no, int l, int r, int id, int pos, int val) {
if(l > pos || r < pos) return no;
if(no == 0) {
no = ++cnt[id];
}
while(no >= sz(f1[id])) {
trq[id].pb(0);
trs[id].pb(0);
f1[id].pb(0);
f2[id].pb(0);
}
if(l == r) {
assert(no < sz(trq[id]));
trq[id][no]+= val;
assert(no < sz(trs[id]));
trs[id][no]+= val*pos;
return no;
}
int mid = (l+r)>>1;
f1[id][no] = att(f1[id][no],l,mid,id,pos,val);
f2[id][no] = att(f2[id][no],mid+1,r,id,pos,val);
trq[id][no] = trq[id][f1[id][no]]+trq[id][f2[id][no]];
trs[id][no] = trs[id][f1[id][no]]+trs[id][f2[id][no]];
return no;
}
//the sum of the val smallest in (l,r)
int find(int no, int l, int r, int id, int val) {
if(no == 0) return 0;
if(l == r) {
return val*r;
}
int mid = (l+r)>>1;
if(trq[id][f1[id][no]] >= val) return find(f1[id][no],l,mid,id,val);
else return trs[id][f1[id][no]]+find(f2[id][no],mid+1,r,id,val-trq[id][f1[id][no]]);
}
void dfs(int u, int qtd, int ant) {
mark[u] = 1;
int ans = 0;
vector<int> use;
for(auto V : gesp[u]) if(V.fr != ant) {
int v = V.fr;
int w = V.sc;
dfs(v,qtd,u);
ans+= min(dp[v][0],dp[v][1]+w);
use.pb(-min(dp[v][0],dp[v][1]+w)+dp[v][1]+w);
att(1,mnv,mxv,u,-min(dp[v][0],dp[v][1]+w)+dp[v][1]+w,1);
}
dp[u][0] = dp[u][1] = inf;
if(sz(g[u])-qtd-1 <= 0) dp[u][1] = ans;
else dp[u][1] = ans+find(1,mnv,mxv,u,sz(g[u])-qtd-1);
if(sz(g[u])-qtd <= 0) dp[u][0] = ans;
else dp[u][0] = ans+find(1,mnv,mxv,u,sz(g[u])-qtd);
for(auto x : use) {
att(1,mnv,mxv,u,x,-1);
}
}
std::vector<int> minimum_closure_costs(int32_t N, std::vector<int32_t> U,std::vector<int32_t> V,std::vector<int32_t> W) {
n = N;
vector<int> ans;
int sumw = 0;
for(int i = 0; i < n-1; i++) {
int u = U[i];
int v = V[i];
int w = W[i];
sumw+= w;
g[u].pb(i);
g[v].pb(i);
}
for(int i = 0; i < n; i++) {
cnt[i] = 1;
gr[sz(g[i])].pb(i);
}
vector<int> esp;
for(int k = n-1; k >=1; k--) {
for(auto i : gr[k]) {
atv[i] = 1;
esp.pb(i);
for(auto id : g[i]) {
int v = i^U[id]^V[id];
int w = W[id];
if(atv[v]) {
gesp[i].pb(mp(v,w));
gesp[v].pb(mp(i,w));
att(1,mnv,mxv,v,w,-1);
}
else {
att(1,mnv,mxv,i,w,1);
}
}
}
int ans1 = 0;
for(auto x : esp) {
if(!mark[x]) {
dfs(x,k,-1);
ans1+= dp[x][0];
}
}
ans.pb(ans1);
for(auto v : esp)
mark[v] = 0;
}
ans.pb(sumw);
reverse(all(ans));
return ans;
}
# | 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... |