Submission #439811

#TimeUsernameProblemLanguageResultExecution timeMemory
439811CSQ31Road Closures (APIO21_roads)C++17
31 / 100
2085 ms53800 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];
}
vector<multiset<ll>>s(MAXN);
ll query(int i,int c){
	ll res = 0;
	for(int x:s[i]){
		if(c<=0)break;
		res+=x;
		c--;
	}
	return res;
}
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(sz(s[v]) >= need)dp[0][v] = cur + query(v,need);
	if(sz(s[v]) >= need-1)dp[1][v] = cur + w + query(v,need-1); //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(sz(s[v])>=take)dp[0][v] = min(dp[0][v],cur + query(v,take));
	}
	need--;
	for(int i=0;i<min(sz(choice),need);i++){
		tmp+=choice[i];
		int take = need-i-1;
		if(sz(s[v])>=take)dp[1][v] = min(dp[1][v],tmp + query(v,take));
	}
}

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++){
		for(auto x:g1[i])s[i].insert(x.se);
	}
	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]){
					s[x.fi].erase(s[x.fi].find(x.se)); //these edges become active now,remove them from segtree
					s[d].erase(s[d].find(x.se));
					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...