답안 #534366

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
534366 2022-03-08T06:05:34 Z amunduzbaev Paths (RMI21_paths) C++17
68 / 100
600 ms 38248 KB
#include "bits/stdc++.h"
using namespace std;

#define ar array
#define int long long
const int N = 1e5 + 5;
vector<ar<int, 2>> edges[N];
int n, k, par[N][20], d[N];
int tin[N], tout[N], t;

void dfs(int u, int p = -1){
	tin[u] = ++t;
	if(p == -1) par[u][0] = u;
	for(int i=1;i<20;i++) par[u][i] = par[par[u][i-1]][i-1];
	for(auto x : edges[u]){
		if(x[0] == p) continue;
		d[x[0]] = d[u] + x[1];
		par[x[0]][0] = u;
		dfs(x[0], u);
	} tout[u] = t;
}

bool is(int a, int b) { return (tin[a] <= tin[b] && tout[b] <= tout[a]); }
int lca(int a, int b){
	if(is(a, b)) return a;
	if(is(b, a)) return b;
	for(int j=19;~j;j--){
		if(!is(par[a][j], b)) a = par[a][j];
	} return par[a][0];
}

int dis(int a, int b){
	return d[a] + d[b] - d[lca(a, b)] * 2;
}

int a[N], dp[N], up[N];
int vdp[N], vup[N];
void Dp(int u){
	vdp[u] = u;
	for(auto x : edges[u]){
		if(x[0] == par[u][0]) continue;
		Dp(x[0]);
		if(dp[x[0]] + x[1] >= dp[u]){
			dp[u] = dp[x[0]] + x[1];
			vdp[u] = vdp[x[0]];
		}
	}
}

void redp(int u){
	int tt = up[u];
	int v = vup[u];
	for(auto x : edges[u]){
		if(x[0] == par[u][0]) continue;
		if(up[x[0]] <= tt + x[1]){
			up[x[0]] = tt + x[1];
			vup[x[0]] = v;
		}
		if(tt <= dp[x[0]] + x[1]){
			tt = dp[x[0]] + x[1];
			v = vdp[x[0]];
		}
	}
	
	reverse(edges[u].begin(), edges[u].end());
	tt = 0;
	v = u;
	for(auto x : edges[u]){
		if(x[0] == par[u][0]) continue;
		if(up[x[0]] <= tt + x[1]){
			up[x[0]] = tt + x[1];
			vup[x[0]] = v;
		}
		if(tt <= dp[x[0]] + x[1]){
			tt = dp[x[0]] + x[1];
			v = vdp[x[0]];
		}
		
		redp(x[0]);
	}
}

/*

34
34
35
39
37
39
34
38
39
38
39

28
28
28
32
30
32
28
32
32
29
30

*/

signed main(){
	ios::sync_with_stdio(0); cin.tie(0);
	
	cin>>n>>k;
	for(int i=1;i<n;i++){
		int a, b, c; cin>>a>>b>>c;
		edges[a].push_back({b, c});
		edges[b].push_back({a, c});
	}
	
	if(n <= 2){
		int res = (n == 2 ? edges[1].back()[1] : 0);
		for(int i=1;i<=n;i++) cout<<res<<"\n";
		return 0;
	}
	
	int rot = -1;
	for(int i=1;i<=n;i++){
		if((int)edges[i].size() > 1){
			rot = i;
			break; 
		}
	}
	
	//~ assert(~rot);
	dfs(rot);
	Dp(rot);
	vup[rot] = rot;
	redp(rot);
	
	multiset<ar<int, 2>> tot, god;
	int sum = 0;
	auto upd = [&](int u, int p){
		int v = dis(a[u], u);
		if(god.count({v, u})){
			god.erase({v, u});
			sum -= v;
			if(!tot.empty()){
				god.insert(*tot.rbegin()), 
				sum += (*tot.rbegin())[0];
				tot.erase(--tot.end());
			}
		} tot.erase({v, u});
		
		a[u] = p, v = dis(p, u);
		god.insert({v, u});
		sum += v;
		if((int)god.size() > k){
			tot.insert(*god.begin());
			sum -= (*god.begin())[0];
			god.erase(god.begin());
		}
	};
	
	for(int i=1;i<=n;i++){
		if((int)edges[i].size() > 1) continue;
		int x = i; a[i] = i;
		for(int j=19;~j;j--){
			if(vdp[par[x][j]] == i) x = par[x][j];
		}
		
		x = par[x][0];
		//~ cout<<i<<" "<<lca(x, i)<<" "<<d[i]<<" "<<dis(x, i)<<"\n";
		upd(i, x);
	}
	
	vector<int> res(n + 1);
	function<void(int)> dfs = [&](int u){
		res[u] = sum;
		for(auto x : edges[u]){
			if(x[0] == par[u][0]) continue;
			//~ assert(a[vdp[x[0]]] == u);
			//~ assert(a[vup[x[0]]] == u);
			upd(vdp[x[0]], x[0]);
			upd(vup[x[0]], x[0]);
			dfs(x[0]);
			upd(vdp[x[0]], u);
			upd(vup[x[0]], u);
		}
	};
	
	dfs(rot);
	for(int i=1;i<=n;i++) cout<<res[i]<<"\n";
}
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 2636 KB Output is correct
2 Correct 2 ms 2636 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 2636 KB Output is correct
2 Correct 2 ms 2636 KB Output is correct
3 Correct 3 ms 2764 KB Output is correct
4 Correct 2 ms 2764 KB Output is correct
5 Correct 3 ms 2708 KB Output is correct
6 Correct 3 ms 2764 KB Output is correct
7 Correct 2 ms 2764 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 2636 KB Output is correct
2 Correct 2 ms 2636 KB Output is correct
3 Correct 3 ms 2764 KB Output is correct
4 Correct 2 ms 2764 KB Output is correct
5 Correct 3 ms 2708 KB Output is correct
6 Correct 3 ms 2764 KB Output is correct
7 Correct 2 ms 2764 KB Output is correct
8 Correct 5 ms 3020 KB Output is correct
9 Correct 7 ms 3020 KB Output is correct
10 Correct 4 ms 2952 KB Output is correct
11 Correct 6 ms 3020 KB Output is correct
12 Correct 4 ms 3020 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 2636 KB Output is correct
2 Correct 2 ms 2636 KB Output is correct
3 Correct 3 ms 2764 KB Output is correct
4 Correct 2 ms 2764 KB Output is correct
5 Correct 3 ms 2708 KB Output is correct
6 Correct 3 ms 2764 KB Output is correct
7 Correct 2 ms 2764 KB Output is correct
8 Correct 5 ms 3020 KB Output is correct
9 Correct 7 ms 3020 KB Output is correct
10 Correct 4 ms 2952 KB Output is correct
11 Correct 6 ms 3020 KB Output is correct
12 Correct 4 ms 3020 KB Output is correct
13 Correct 7 ms 3336 KB Output is correct
14 Correct 6 ms 3404 KB Output is correct
15 Correct 7 ms 3308 KB Output is correct
16 Correct 10 ms 3276 KB Output is correct
17 Correct 11 ms 3276 KB Output is correct
18 Correct 9 ms 3192 KB Output is correct
19 Correct 11 ms 3276 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 444 ms 34740 KB Output is correct
2 Correct 487 ms 36488 KB Output is correct
3 Correct 323 ms 31996 KB Output is correct
4 Correct 426 ms 34708 KB Output is correct
5 Correct 465 ms 35640 KB Output is correct
6 Correct 417 ms 34824 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 2636 KB Output is correct
2 Correct 2 ms 2636 KB Output is correct
3 Correct 3 ms 2764 KB Output is correct
4 Correct 2 ms 2764 KB Output is correct
5 Correct 3 ms 2708 KB Output is correct
6 Correct 3 ms 2764 KB Output is correct
7 Correct 2 ms 2764 KB Output is correct
8 Correct 5 ms 3020 KB Output is correct
9 Correct 7 ms 3020 KB Output is correct
10 Correct 4 ms 2952 KB Output is correct
11 Correct 6 ms 3020 KB Output is correct
12 Correct 4 ms 3020 KB Output is correct
13 Correct 7 ms 3336 KB Output is correct
14 Correct 6 ms 3404 KB Output is correct
15 Correct 7 ms 3308 KB Output is correct
16 Correct 10 ms 3276 KB Output is correct
17 Correct 11 ms 3276 KB Output is correct
18 Correct 9 ms 3192 KB Output is correct
19 Correct 11 ms 3276 KB Output is correct
20 Correct 444 ms 34740 KB Output is correct
21 Correct 487 ms 36488 KB Output is correct
22 Correct 323 ms 31996 KB Output is correct
23 Correct 426 ms 34708 KB Output is correct
24 Correct 465 ms 35640 KB Output is correct
25 Correct 417 ms 34824 KB Output is correct
26 Correct 591 ms 35224 KB Output is correct
27 Correct 527 ms 36524 KB Output is correct
28 Correct 527 ms 36876 KB Output is correct
29 Correct 421 ms 32108 KB Output is correct
30 Correct 487 ms 35180 KB Output is correct
31 Correct 509 ms 35444 KB Output is correct
32 Execution timed out 616 ms 38248 KB Time limit exceeded
33 Halted 0 ms 0 KB -