#include "roads.h"
#include<bits/stdc++.h>
using namespace std;
#define pb push_back
#define fi first
#define se second
#define sz(a) (int)(a.size())
#define all(a) a.begin(),a.end()
#define lb lower_bound
#define ub upper_bound
#define owo ios_base::sync_with_stdio(0);cin.tie(0);
#define INF (ll)(1e18)
#define debug(...) fprintf(stderr, __VA_ARGS__),fflush(stderr)
#define time__(d) for(long blockTime = 0; (blockTime == 0 ? (blockTime=clock()) != 0 : false);\
debug("%s time : %.4fs\n", d, (double)(clock() - blockTime) / CLOCKS_PER_SEC))
typedef long long int ll;
typedef long double ld;
typedef pair<ll,ll> PII;
typedef pair<int,int> pii;
typedef vector<vector<int>> vii;
typedef vector<vector<ll>> VII;
ll gcd(ll a,ll b){if(!b)return a;else return gcd(b,a%b);}
const int MAXN = 2e5+5;
vector<vector<pii>> g1(MAXN),g2(MAXN);
vector<int>par(MAXN),szz(MAXN,1),act(MAXN);
vector<ll>ans;
VII dp(2,vector<ll>(MAXN,0));
int find(int x){
if(x == par[x])return x;
else return par[x] = find(par[x]);
}
void unite(int a,int b){
a = find(a);
b = find(b);
if(a==b)return;
if(szz[a] > szz[b])swap(a,b);
par[a] = b;
szz[b]+=szz[a];
}
struct node{
ll sum,cnt;
ll L,R;
node *lf=nullptr,*rg = nullptr;
node(){}
node(ll _L,ll _R){
L = _L;
R = _R;
}
void upd(ll pos,ll c){
if(L==R){
sum+=c*pos;
cnt+=c;
return;
}
ll tm = (L+R)/2;
if(pos<=tm){
if(lf == nullptr)lf = new node(L,tm);
lf->upd(pos,c);
}else{
if(rg == nullptr)rg = new node(tm+1,R);
rg->upd(pos,c);
}
sum=cnt=0;
if(lf != nullptr){
sum+=lf->sum;
cnt+=lf->cnt;
}
if(rg != nullptr){
sum+=rg->sum;
cnt+=rg->cnt;
}
}
ll query(ll v){
//cout<<L<<" "<<R<<" "<<cnt<<" "<<v<<'\n';
if(cnt == v)return sum;
if(L == R)return v*L;
ll tm = (L+R)/2;
if(lf == nullptr)lf = new node(L,tm);
if(lf->cnt >= v)return lf->query(v);
else{
if(rg == nullptr)rg = new node(tm+1,R);
return lf->sum + rg->query(v-lf->cnt);
}
}
};
vector<node>seg(MAXN);
void dfs(int v,int u,ll w,int k){
int need = sz(g1[v])-k;
ll cur = 0;
vector<ll>choice;
for(auto x:g2[v]){
if(x.fi != u){
dfs(x.fi,v,x.se,k);
if(dp[1][x.fi] <= dp[0][x.fi]){ //we will take this edge anyways
cur+=dp[1][x.fi];
need--;
}else{
cur+=dp[0][x.fi];
choice.pb(dp[1][x.fi]-dp[0][x.fi]);
}
}
}
dp[0][v] = dp[1][v] = INF;
sort(all(choice));
if(need<=0)dp[0][v] = cur;
if(need-1<=0)dp[1][v] = w+cur;
if(seg[v].cnt >= need)dp[0][v] = cur + (need>0?seg[v].query(need):0);
if(seg[v].cnt >= need-1)dp[1][v] = cur + w + (need-1>0?seg[v].query(need-1):0); //take extra edges only from inactive
ll tmp = cur;
for(int i=0;i<min(sz(choice),need);i++){
cur+=choice[i];
int take = need-i-1;
if(seg[v].cnt>=take)dp[0][v] = min(dp[0][v],cur + (take>0?seg[v].query(take):0));
}
need--;
for(int i=0;i<min(sz(choice),need);i++){
tmp+=choice[i];
int take = need-i-1;
if(seg[v].cnt>=take)dp[1][v] = min(dp[1][v],cur + (take>0?seg[v].query(take):0));
}
}
vector<ll> minimum_closure_costs(int n, vector<int> u,vector<int>v,vector<int>w){
ans.resize(n);
for(int i=0;i<n-1;i++){
g1[u[i]].pb({v[i],w[i]});
g1[v[i]].pb({u[i],w[i]});
}
for(int i=0;i<n;i++){
seg[i] = node(1,1e9);
for(auto x:g1[i])seg[i].upd(x.se,1);
}
vector<int>c(n);
for(int i=0;i<n;i++){c[i] = i;par[i] = i;}
sort(all(c),[&](int x,int y){return sz(g1[x]) > sz(g1[y]);});
int ptr = -1;
vector<int>vis(n);
for(int i=n-1;i>=0;i--){
while(ptr+1<n && sz(g1[c[ptr+1]]) > i){
ptr++;
int d = c[ptr];
act[d] = 1;
for(auto x:g1[d]){
if(act[x.fi]){
seg[x.fi].upd(x.se,-1); //these edges become active now,remove them from segtree
seg[d].upd(x.se,-1);
g2[x.fi].pb({d,x.se});
g2[d].pb({x.fi,x.se});
unite(x.fi,d);
}
}
}
vector<int>meet;
for(int j=0;j<=ptr;j++){
int a = find(c[j]);
if(vis[a])continue;
else{
dfs(a,-1,0,i);
meet.pb(a);
vis[a] = 1;
ans[i]+=dp[0][a];
}
}
for(int x:meet)vis[x] = 0;
}
return ans;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
15 ms |
24496 KB |
Output is correct |
2 |
Correct |
26 ms |
30544 KB |
Output is correct |
3 |
Correct |
25 ms |
31028 KB |
Output is correct |
4 |
Correct |
26 ms |
28436 KB |
Output is correct |
5 |
Correct |
16 ms |
25008 KB |
Output is correct |
6 |
Correct |
16 ms |
25264 KB |
Output is correct |
7 |
Correct |
14 ms |
24968 KB |
Output is correct |
8 |
Correct |
19 ms |
27932 KB |
Output is correct |
9 |
Correct |
22 ms |
28592 KB |
Output is correct |
10 |
Correct |
15 ms |
24880 KB |
Output is correct |
11 |
Correct |
15 ms |
24868 KB |
Output is correct |
12 |
Correct |
227 ms |
145476 KB |
Output is correct |
13 |
Correct |
424 ms |
225608 KB |
Output is correct |
14 |
Correct |
682 ms |
280372 KB |
Output is correct |
15 |
Correct |
774 ms |
313448 KB |
Output is correct |
16 |
Correct |
790 ms |
313988 KB |
Output is correct |
17 |
Correct |
469 ms |
220172 KB |
Output is correct |
18 |
Correct |
14 ms |
24516 KB |
Output is correct |
19 |
Correct |
377 ms |
206212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
14 ms |
24548 KB |
Output is correct |
2 |
Correct |
554 ms |
344736 KB |
Output is correct |
3 |
Correct |
646 ms |
391312 KB |
Output is correct |
4 |
Correct |
678 ms |
414540 KB |
Output is correct |
5 |
Correct |
389 ms |
229828 KB |
Output is correct |
6 |
Correct |
27 ms |
31148 KB |
Output is correct |
7 |
Correct |
32 ms |
32360 KB |
Output is correct |
8 |
Correct |
20 ms |
28196 KB |
Output is correct |
9 |
Correct |
16 ms |
25164 KB |
Output is correct |
10 |
Correct |
15 ms |
25264 KB |
Output is correct |
11 |
Correct |
17 ms |
24976 KB |
Output is correct |
12 |
Correct |
254 ms |
160168 KB |
Output is correct |
13 |
Correct |
404 ms |
252696 KB |
Output is correct |
14 |
Correct |
15 ms |
24520 KB |
Output is correct |
15 |
Correct |
386 ms |
215060 KB |
Output is correct |
16 |
Correct |
390 ms |
236092 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
15 ms |
24496 KB |
Output is correct |
2 |
Correct |
13 ms |
24556 KB |
Output is correct |
3 |
Correct |
14 ms |
24540 KB |
Output is correct |
4 |
Incorrect |
16 ms |
25008 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
15 ms |
24496 KB |
Output is correct |
2 |
Correct |
13 ms |
24556 KB |
Output is correct |
3 |
Correct |
14 ms |
24540 KB |
Output is correct |
4 |
Incorrect |
16 ms |
25008 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
598 ms |
217996 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
598 ms |
217996 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
15 ms |
24496 KB |
Output is correct |
2 |
Correct |
26 ms |
30544 KB |
Output is correct |
3 |
Correct |
25 ms |
31028 KB |
Output is correct |
4 |
Correct |
26 ms |
28436 KB |
Output is correct |
5 |
Correct |
16 ms |
25008 KB |
Output is correct |
6 |
Correct |
16 ms |
25264 KB |
Output is correct |
7 |
Correct |
14 ms |
24968 KB |
Output is correct |
8 |
Correct |
19 ms |
27932 KB |
Output is correct |
9 |
Correct |
22 ms |
28592 KB |
Output is correct |
10 |
Correct |
15 ms |
24880 KB |
Output is correct |
11 |
Correct |
15 ms |
24868 KB |
Output is correct |
12 |
Correct |
227 ms |
145476 KB |
Output is correct |
13 |
Correct |
424 ms |
225608 KB |
Output is correct |
14 |
Correct |
682 ms |
280372 KB |
Output is correct |
15 |
Correct |
774 ms |
313448 KB |
Output is correct |
16 |
Correct |
790 ms |
313988 KB |
Output is correct |
17 |
Correct |
469 ms |
220172 KB |
Output is correct |
18 |
Correct |
14 ms |
24516 KB |
Output is correct |
19 |
Correct |
377 ms |
206212 KB |
Output is correct |
20 |
Correct |
14 ms |
24548 KB |
Output is correct |
21 |
Correct |
554 ms |
344736 KB |
Output is correct |
22 |
Correct |
646 ms |
391312 KB |
Output is correct |
23 |
Correct |
678 ms |
414540 KB |
Output is correct |
24 |
Correct |
389 ms |
229828 KB |
Output is correct |
25 |
Correct |
27 ms |
31148 KB |
Output is correct |
26 |
Correct |
32 ms |
32360 KB |
Output is correct |
27 |
Correct |
20 ms |
28196 KB |
Output is correct |
28 |
Correct |
16 ms |
25164 KB |
Output is correct |
29 |
Correct |
15 ms |
25264 KB |
Output is correct |
30 |
Correct |
17 ms |
24976 KB |
Output is correct |
31 |
Correct |
254 ms |
160168 KB |
Output is correct |
32 |
Correct |
404 ms |
252696 KB |
Output is correct |
33 |
Correct |
15 ms |
24520 KB |
Output is correct |
34 |
Correct |
386 ms |
215060 KB |
Output is correct |
35 |
Correct |
390 ms |
236092 KB |
Output is correct |
36 |
Correct |
15 ms |
24496 KB |
Output is correct |
37 |
Correct |
13 ms |
24556 KB |
Output is correct |
38 |
Correct |
14 ms |
24540 KB |
Output is correct |
39 |
Incorrect |
16 ms |
25008 KB |
Output isn't correct |
40 |
Halted |
0 ms |
0 KB |
- |