Submission #692591

# Submission time Handle Problem Language Result Execution time Memory
692591 2023-02-02T00:55:58 Z moonrabbit2 Paths (RMI21_paths) C++17
100 / 100
210 ms 23500 KB
#include <bits/stdc++.h>
using namespace std;
using ll=long long;
const int N=1e5+5;
int n,k,m;
vector<array<int,2>> g[N];
ll dp[N],scd[N],vals[N];
int best[N];
struct DeletablePQ{
	priority_queue<ll,vector<ll>,greater<ll>> pq1,pq2;
	void add(ll x){
		pq1.emplace(x);
	}
	void del(ll x){
		pq2.emplace(x);
	}
	ll top(){
		assert(pq1.size()>pq2.size());
		while(pq2.size()&&pq1.top()==pq2.top()){
			pq1.pop();
			pq2.pop();
		}
		return pq1.top();
	}
}TopK,Left;
ll Ksum,ans[N];
void dfs(int u,int p){
	for(auto [v,c]: g[u]) if(p!=v){
		dfs(v,u);
		if(dp[v]+c<dp[u]) vals[++m]=dp[v]+c;
		else{
			if(dp[u]) vals[++m]=dp[u];
			dp[u]=dp[v]+c;
			best[u]=v;
		}
	}
	for(auto [v,c]: g[u]) if(p!=v&&best[u]!=v){
		scd[u]=max(scd[u],dp[v]+c);
	}
}
void insertVal(ll v){
	if(TopK.top()>=v){
		Left.add(-v);
		return;
	}
	Ksum+=v-TopK.top();
	Left.add(-TopK.top());
	TopK.del(TopK.top());
	TopK.add(v);
}
void deleteVal(ll v){
	if(TopK.top()>v){
		Left.del(-v);
		return;
	}
	Ksum-=v;
	TopK.del(v);
	ll val=-Left.top();
	Ksum+=val;
	Left.del(-val);
	TopK.add(val);
}
void dfs2(int u,int p,ll cur){
	ans[u]=Ksum;
	for(auto [v,c]: g[u]) if(p!=v){
		if(v==best[u]){
			insertVal(dp[v]);
			insertVal(max(cur,scd[u])+c);
			deleteVal(dp[v]+c);
			deleteVal(max(cur,scd[u]));
			dfs2(v,u,max(cur,scd[u])+c);
			insertVal(dp[v]+c);
			insertVal(max(cur,scd[u]));
			deleteVal(dp[v]);
			deleteVal(max(cur,scd[u])+c);
		} else{
			insertVal(dp[v]);
			insertVal(max(cur,dp[u])+c);
			deleteVal(dp[v]+c);
			deleteVal(max(cur,dp[u]));
			dfs2(v,u,max(cur,dp[u])+c);
			insertVal(dp[v]+c);
			insertVal(max(cur,dp[u]));
			deleteVal(dp[v]);
			deleteVal(max(cur,dp[u])+c);
		}
	}
}
int main(){
	ios::sync_with_stdio(false); cin.tie(0);
	cin>>n>>k;
	for(int u,v,c,i=1;i<n;i++){
		cin>>u>>v>>c;
		g[u].push_back({v,c});
		g[v].push_back({u,c});
	}
	dfs(1,0);
	vals[++m]=dp[1];
	sort(vals+1,vals+1+m,greater<>());
	for(int i=1;i<=k;i++){
		TopK.add(vals[i]);
		Ksum+=vals[i];
	}
	for(int i=k+1;i<=n;i++) Left.add(-vals[i]);
	dfs2(1,0,0);
	for(int i=1;i<=n;i++) cout<<ans[i]<<"\n";
	return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 2644 KB Output is correct
2 Correct 1 ms 2644 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 2644 KB Output is correct
2 Correct 1 ms 2644 KB Output is correct
3 Correct 2 ms 2644 KB Output is correct
4 Correct 2 ms 2644 KB Output is correct
5 Correct 2 ms 2684 KB Output is correct
6 Correct 2 ms 2684 KB Output is correct
7 Correct 2 ms 2684 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 2644 KB Output is correct
2 Correct 1 ms 2644 KB Output is correct
3 Correct 2 ms 2644 KB Output is correct
4 Correct 2 ms 2644 KB Output is correct
5 Correct 2 ms 2684 KB Output is correct
6 Correct 2 ms 2684 KB Output is correct
7 Correct 2 ms 2684 KB Output is correct
8 Correct 3 ms 2856 KB Output is correct
9 Correct 3 ms 2900 KB Output is correct
10 Correct 3 ms 2900 KB Output is correct
11 Correct 3 ms 2900 KB Output is correct
12 Correct 3 ms 2832 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 2644 KB Output is correct
2 Correct 1 ms 2644 KB Output is correct
3 Correct 2 ms 2644 KB Output is correct
4 Correct 2 ms 2644 KB Output is correct
5 Correct 2 ms 2684 KB Output is correct
6 Correct 2 ms 2684 KB Output is correct
7 Correct 2 ms 2684 KB Output is correct
8 Correct 3 ms 2856 KB Output is correct
9 Correct 3 ms 2900 KB Output is correct
10 Correct 3 ms 2900 KB Output is correct
11 Correct 3 ms 2900 KB Output is correct
12 Correct 3 ms 2832 KB Output is correct
13 Correct 5 ms 3028 KB Output is correct
14 Correct 5 ms 3196 KB Output is correct
15 Correct 4 ms 3156 KB Output is correct
16 Correct 5 ms 3128 KB Output is correct
17 Correct 5 ms 3080 KB Output is correct
18 Correct 4 ms 2972 KB Output is correct
19 Correct 5 ms 3156 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 140 ms 14948 KB Output is correct
2 Correct 161 ms 21004 KB Output is correct
3 Correct 126 ms 16268 KB Output is correct
4 Correct 162 ms 17008 KB Output is correct
5 Correct 149 ms 19552 KB Output is correct
6 Correct 130 ms 17084 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 2644 KB Output is correct
2 Correct 1 ms 2644 KB Output is correct
3 Correct 2 ms 2644 KB Output is correct
4 Correct 2 ms 2644 KB Output is correct
5 Correct 2 ms 2684 KB Output is correct
6 Correct 2 ms 2684 KB Output is correct
7 Correct 2 ms 2684 KB Output is correct
8 Correct 3 ms 2856 KB Output is correct
9 Correct 3 ms 2900 KB Output is correct
10 Correct 3 ms 2900 KB Output is correct
11 Correct 3 ms 2900 KB Output is correct
12 Correct 3 ms 2832 KB Output is correct
13 Correct 5 ms 3028 KB Output is correct
14 Correct 5 ms 3196 KB Output is correct
15 Correct 4 ms 3156 KB Output is correct
16 Correct 5 ms 3128 KB Output is correct
17 Correct 5 ms 3080 KB Output is correct
18 Correct 4 ms 2972 KB Output is correct
19 Correct 5 ms 3156 KB Output is correct
20 Correct 140 ms 14948 KB Output is correct
21 Correct 161 ms 21004 KB Output is correct
22 Correct 126 ms 16268 KB Output is correct
23 Correct 162 ms 17008 KB Output is correct
24 Correct 149 ms 19552 KB Output is correct
25 Correct 130 ms 17084 KB Output is correct
26 Correct 207 ms 20912 KB Output is correct
27 Correct 193 ms 22584 KB Output is correct
28 Correct 199 ms 23140 KB Output is correct
29 Correct 154 ms 19924 KB Output is correct
30 Correct 196 ms 21688 KB Output is correct
31 Correct 182 ms 22228 KB Output is correct
32 Correct 210 ms 21364 KB Output is correct
33 Correct 208 ms 22264 KB Output is correct
34 Correct 155 ms 19348 KB Output is correct
35 Correct 195 ms 22524 KB Output is correct
36 Correct 192 ms 23500 KB Output is correct