제출 #439820

#제출 시각아이디문제언어결과실행 시간메모리
439820CSQ31도로 폐쇄 (APIO21_roads)C++17
100 / 100
818 ms415760 KiB
#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); //g1 is original graph,g2 is subgraph after we deleted nodes with degree<=k vector<int>par(MAXN),szz(MAXN,1),act(MAXN); //act means whether a node is active / has degree>k 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{ //dynamic segtree ll sum=0,cnt=0; 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(L == R)return v*L; if(cnt == v)return sum; 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+w; 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],tmp + (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); //currently graph is empty so we add every single edge as "inactive edge" } 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]);}); //sort vertex by degree and we sweep 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; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...