답안 #933199

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
933199 2024-02-25T09:01:05 Z SmuggingSpun 경주 (Race) (IOI11_race) C++17
100 / 100
882 ms 61976 KB
#include "race.h"
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
template<class T>void minimize(T& a, T b){
	if(a > b){
		a = b;
	}
}
const int lim = 2e5 + 5;
const int LIM = 1e6 + 5;
const int INF = 1e9;
vector<pair<int, int>>e[lim];
vector<int>g[lim];
int n, k, ans = INT_MAX, h[lim], up[lim][18], cnt[lim], value[LIM];
bitset<lim>in_centroid;
ll f[lim];
void dfs(int s, int p = -1){
	cnt[s] = 1;
	for(auto& [d, w] : e[s]){
		if(d != p){
			f[d] = f[up[d][0] = s] + w;
			h[d] = h[s] + 1;
			for(int i = 1; i < 18; i++){
				up[d][i] = up[up[d][i - 1]][i - 1];
			}
			dfs(d, s);
		}
	}
}
int lca(int u, int v){
	if(h[u] < h[v]){
		swap(u, v);
	}
	int k = h[u] - h[v];
	while(k > 0){
		int x = __builtin_ctz(k);
		u = up[u][x];
		k ^= 1 << x;
	}
	if(u == v){
		return u;
	}
	for(int i = 17; i > -1; i--){
		int U = up[u][i], V = up[v][i];
		if(U != V){
			u = U;
			v = V;
		}
	}
	return up[u][0];
}
ll get_distance(int u, int v){
	return f[u] + f[v] - (f[lca(u, v)] << 1);
}
int get_distance_height(int u, int v){
	return h[u] + h[v] - (h[lca(u, v)] << 1);
}
void build_cnt(int s, int p = -1){
	cnt[s] = 1;
	for(auto& [d, w] : e[s]){
		if(d != p && !in_centroid.test(d)){
			build_cnt(d, s);
			cnt[s] += cnt[d];
		}
	}
}
int get_centroid(int s, int n, int p = -1){
	for(auto& [d, w] : e[s]){
		if(d != p && !in_centroid.test(d) && cnt[d] > (n >> 1)){
			return get_centroid(d, n, s);
		}
	}
	return s;
} 
int centroid_decomposition(int s, int p = -1){
	build_cnt(s, p);
	int centroid = get_centroid(s, cnt[s], p);
	in_centroid.set(centroid);
	for(auto& [d, w] : e[centroid]){
		if(!in_centroid.test(d)){
			g[centroid].emplace_back(centroid_decomposition(d, centroid));
		}
	}
	return centroid;
}
vector<int>vertex;
void dfs_centroid_support(int root, int s, bool is_add){
	ll D = get_distance(root, s);
	if(D <= k){
		if(is_add){
			vertex.emplace_back(s);
		}
		else{
			value[D] = INF;
		}
	}
	for(int& d : g[s]){
		dfs_centroid_support(root, d, is_add);
	}
}
void dfs_centroid(int s){
	value[0] = 0;
	for(int& d : g[s]){
		vertex.clear();
		dfs_centroid_support(s, d, true);
		for(int& x : vertex){
			minimize(ans, get_distance_height(s, x) + value[k - get_distance(s, x)]);
		}
		for(int& x : vertex){
			minimize(value[get_distance(s, x)], get_distance_height(s, x)); 
		}
	}
	dfs_centroid_support(s, s, false);
	for(int& d : g[s]){
		dfs_centroid(d);
	}
}
int best_path(int N, int K, int H[][2], int L[]){
	n = N;
	k = K;
	for(int i = 0; i < n; i++){
		e[H[i][0]].emplace_back(H[i][1], L[i]);
		e[H[i][1]].emplace_back(H[i][0], L[i]);
	}
	memset(up, h[0] = f[0] = 0, sizeof(up));
	dfs(0);
	in_centroid.reset();
	memset(value, 0x0f, sizeof(value));
	dfs_centroid(centroid_decomposition(0));
	return ans > n ? -1 : ans;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 34392 KB Output is correct
2 Correct 6 ms 34572 KB Output is correct
3 Correct 7 ms 34396 KB Output is correct
4 Correct 6 ms 34484 KB Output is correct
5 Correct 7 ms 34392 KB Output is correct
6 Correct 6 ms 34648 KB Output is correct
7 Correct 6 ms 34396 KB Output is correct
8 Correct 8 ms 34620 KB Output is correct
9 Correct 6 ms 34640 KB Output is correct
10 Correct 6 ms 34392 KB Output is correct
11 Correct 6 ms 34616 KB Output is correct
12 Correct 6 ms 34396 KB Output is correct
13 Correct 7 ms 34396 KB Output is correct
14 Correct 6 ms 34620 KB Output is correct
15 Correct 6 ms 34396 KB Output is correct
16 Correct 6 ms 34392 KB Output is correct
17 Correct 6 ms 34396 KB Output is correct
18 Correct 6 ms 34652 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 34392 KB Output is correct
2 Correct 6 ms 34572 KB Output is correct
3 Correct 7 ms 34396 KB Output is correct
4 Correct 6 ms 34484 KB Output is correct
5 Correct 7 ms 34392 KB Output is correct
6 Correct 6 ms 34648 KB Output is correct
7 Correct 6 ms 34396 KB Output is correct
8 Correct 8 ms 34620 KB Output is correct
9 Correct 6 ms 34640 KB Output is correct
10 Correct 6 ms 34392 KB Output is correct
11 Correct 6 ms 34616 KB Output is correct
12 Correct 6 ms 34396 KB Output is correct
13 Correct 7 ms 34396 KB Output is correct
14 Correct 6 ms 34620 KB Output is correct
15 Correct 6 ms 34396 KB Output is correct
16 Correct 6 ms 34392 KB Output is correct
17 Correct 6 ms 34396 KB Output is correct
18 Correct 6 ms 34652 KB Output is correct
19 Correct 6 ms 34396 KB Output is correct
20 Correct 6 ms 34652 KB Output is correct
21 Correct 8 ms 34652 KB Output is correct
22 Correct 7 ms 34652 KB Output is correct
23 Correct 7 ms 34648 KB Output is correct
24 Correct 7 ms 34652 KB Output is correct
25 Correct 8 ms 34488 KB Output is correct
26 Correct 7 ms 34652 KB Output is correct
27 Correct 8 ms 34648 KB Output is correct
28 Correct 7 ms 34652 KB Output is correct
29 Correct 8 ms 34648 KB Output is correct
30 Correct 7 ms 34652 KB Output is correct
31 Correct 8 ms 34652 KB Output is correct
32 Correct 7 ms 34652 KB Output is correct
33 Correct 8 ms 34648 KB Output is correct
34 Correct 7 ms 34652 KB Output is correct
35 Correct 7 ms 34652 KB Output is correct
36 Correct 7 ms 34652 KB Output is correct
37 Correct 7 ms 34704 KB Output is correct
38 Correct 8 ms 34652 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 34392 KB Output is correct
2 Correct 6 ms 34572 KB Output is correct
3 Correct 7 ms 34396 KB Output is correct
4 Correct 6 ms 34484 KB Output is correct
5 Correct 7 ms 34392 KB Output is correct
6 Correct 6 ms 34648 KB Output is correct
7 Correct 6 ms 34396 KB Output is correct
8 Correct 8 ms 34620 KB Output is correct
9 Correct 6 ms 34640 KB Output is correct
10 Correct 6 ms 34392 KB Output is correct
11 Correct 6 ms 34616 KB Output is correct
12 Correct 6 ms 34396 KB Output is correct
13 Correct 7 ms 34396 KB Output is correct
14 Correct 6 ms 34620 KB Output is correct
15 Correct 6 ms 34396 KB Output is correct
16 Correct 6 ms 34392 KB Output is correct
17 Correct 6 ms 34396 KB Output is correct
18 Correct 6 ms 34652 KB Output is correct
19 Correct 228 ms 41772 KB Output is correct
20 Correct 230 ms 42092 KB Output is correct
21 Correct 223 ms 42064 KB Output is correct
22 Correct 228 ms 42240 KB Output is correct
23 Correct 218 ms 43688 KB Output is correct
24 Correct 142 ms 42456 KB Output is correct
25 Correct 290 ms 46344 KB Output is correct
26 Correct 231 ms 49504 KB Output is correct
27 Correct 268 ms 48316 KB Output is correct
28 Correct 874 ms 61376 KB Output is correct
29 Correct 879 ms 60676 KB Output is correct
30 Correct 294 ms 49192 KB Output is correct
31 Correct 263 ms 48984 KB Output is correct
32 Correct 483 ms 49040 KB Output is correct
33 Correct 596 ms 48976 KB Output is correct
34 Correct 576 ms 49240 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 34392 KB Output is correct
2 Correct 6 ms 34572 KB Output is correct
3 Correct 7 ms 34396 KB Output is correct
4 Correct 6 ms 34484 KB Output is correct
5 Correct 7 ms 34392 KB Output is correct
6 Correct 6 ms 34648 KB Output is correct
7 Correct 6 ms 34396 KB Output is correct
8 Correct 8 ms 34620 KB Output is correct
9 Correct 6 ms 34640 KB Output is correct
10 Correct 6 ms 34392 KB Output is correct
11 Correct 6 ms 34616 KB Output is correct
12 Correct 6 ms 34396 KB Output is correct
13 Correct 7 ms 34396 KB Output is correct
14 Correct 6 ms 34620 KB Output is correct
15 Correct 6 ms 34396 KB Output is correct
16 Correct 6 ms 34392 KB Output is correct
17 Correct 6 ms 34396 KB Output is correct
18 Correct 6 ms 34652 KB Output is correct
19 Correct 6 ms 34396 KB Output is correct
20 Correct 6 ms 34652 KB Output is correct
21 Correct 8 ms 34652 KB Output is correct
22 Correct 7 ms 34652 KB Output is correct
23 Correct 7 ms 34648 KB Output is correct
24 Correct 7 ms 34652 KB Output is correct
25 Correct 8 ms 34488 KB Output is correct
26 Correct 7 ms 34652 KB Output is correct
27 Correct 8 ms 34648 KB Output is correct
28 Correct 7 ms 34652 KB Output is correct
29 Correct 8 ms 34648 KB Output is correct
30 Correct 7 ms 34652 KB Output is correct
31 Correct 8 ms 34652 KB Output is correct
32 Correct 7 ms 34652 KB Output is correct
33 Correct 8 ms 34648 KB Output is correct
34 Correct 7 ms 34652 KB Output is correct
35 Correct 7 ms 34652 KB Output is correct
36 Correct 7 ms 34652 KB Output is correct
37 Correct 7 ms 34704 KB Output is correct
38 Correct 8 ms 34652 KB Output is correct
39 Correct 228 ms 41772 KB Output is correct
40 Correct 230 ms 42092 KB Output is correct
41 Correct 223 ms 42064 KB Output is correct
42 Correct 228 ms 42240 KB Output is correct
43 Correct 218 ms 43688 KB Output is correct
44 Correct 142 ms 42456 KB Output is correct
45 Correct 290 ms 46344 KB Output is correct
46 Correct 231 ms 49504 KB Output is correct
47 Correct 268 ms 48316 KB Output is correct
48 Correct 874 ms 61376 KB Output is correct
49 Correct 879 ms 60676 KB Output is correct
50 Correct 294 ms 49192 KB Output is correct
51 Correct 263 ms 48984 KB Output is correct
52 Correct 483 ms 49040 KB Output is correct
53 Correct 596 ms 48976 KB Output is correct
54 Correct 576 ms 49240 KB Output is correct
55 Correct 20 ms 35164 KB Output is correct
56 Correct 24 ms 35296 KB Output is correct
57 Correct 123 ms 43096 KB Output is correct
58 Correct 55 ms 42668 KB Output is correct
59 Correct 193 ms 49616 KB Output is correct
60 Correct 882 ms 61976 KB Output is correct
61 Correct 277 ms 48216 KB Output is correct
62 Correct 264 ms 49000 KB Output is correct
63 Correct 503 ms 49976 KB Output is correct
64 Correct 753 ms 49104 KB Output is correct
65 Correct 546 ms 50312 KB Output is correct
66 Correct 727 ms 56364 KB Output is correct
67 Correct 297 ms 51092 KB Output is correct
68 Correct 486 ms 49356 KB Output is correct
69 Correct 455 ms 47028 KB Output is correct
70 Correct 409 ms 49024 KB Output is correct