Submission #808964

# Submission time Handle Problem Language Result Execution time Memory
808964 2023-08-05T12:56:41 Z Tkm_algo Race (IOI11_race) C++17
100 / 100
374 ms 58148 KB
#include "bits/stdc++.h"
#include "race.h"

using namespace std;
using ll = long long;
const int MAXN = 1e6 + 10;
vector<pair<int, int>> adj[MAXN];
vector<int> sz(MAXN);
vector<bool> removed(MAXN);
int K, ans = 1e8;
vector<int> P(MAXN, 1e9 + 7);

int subtree_size(int u, int par = -1) {
	sz[u] = 1;
	for (auto [v, q] : adj[u]) {
		if (v == par || removed[v]) {
			continue;
		}
		sz[u] += subtree_size(v, u);
	}
	return sz[u];
}

int centroid(int u, int subsz, int par = -1) {
	for (auto [v, q] : adj[u]) {
		if (v == par || removed[v]) {
			continue;
		}
		if (sz[v] * 2 > subsz) {
			return centroid(v, subsz, u);
		}
	}
	return u;
}

void dfs(int u, int par, int dpt, int W) {
	if (W > 0 && W <= K) {
		P[W] = 1e8;
	}
	if (K > W) {
		P[K - W] = 1e8;
	}
	for (auto [v, q] : adj[u]) {
		if (v == par || removed[v]) {
			continue;
		}
		dfs(v, u, dpt + 1, W + q);
	}
}

void dfs0(int u, int par, int dpt, int W) {
	// cout << par << "--->" << u << ' ' << dpt << ' ' << W << '\n';
	if (K >= W && par != -1) {
		// cout << par << ' ' << u << ' ' << P[K - W] << ' ' << dpt << '\n';
		ans = min(ans, P[K - W] + dpt);
	}
	for (auto [v, q] : adj[u]) {
		if (v == par || removed[v]) {
			continue;
		}
		dfs0(v, u, dpt + 1, W + q);
	}
}

void dfs1(int u, int par, int dpt, int W) {
	if (K >= W && par != -1) {
		P[W] = min(P[W], dpt);
	}
	for (auto [v, q] : adj[u]) {
		if (v == par || removed[v]) {
			continue;
		}
		dfs1(v, u, dpt + 1, W + q);
	}
}

void build(int u = 0) {
	int c = centroid(u, subtree_size(u));
	// cout << c << ' ';
	removed[c] = 1;
	for (auto [v, q] : adj[c]) {
		if (removed[v]) {
			continue;
		}
		dfs(v, c, 1, q);
	}
	for (auto [v, q] : adj[c]) {
		if (removed[v]) {
			continue;
		}
		dfs0(v, c, 1, q);
		dfs1(v, c, 1, q);
	}
	for (auto [v, q] : adj[c]) {
		if (removed[v]) {
			continue;
		}
		build(v);
	}
}

int best_path(int n, int k, int h[][2], int l[]) {
	for (int i = 0; i < n - 1; i++) {
		adj[h[i][0]].push_back({h[i][1], l[i]});
		adj[h[i][1]].push_back({h[i][0], l[i]});
	}
	K = k;
	P[0] = 0;
	build();
	if (ans > n) {
		return -1;
	}
	return ans;
}
# Verdict Execution time Memory Grader output
1 Correct 15 ms 31700 KB Output is correct
2 Correct 14 ms 31728 KB Output is correct
3 Correct 15 ms 31716 KB Output is correct
4 Correct 14 ms 31700 KB Output is correct
5 Correct 15 ms 31760 KB Output is correct
6 Correct 15 ms 31768 KB Output is correct
7 Correct 14 ms 31752 KB Output is correct
8 Correct 17 ms 31740 KB Output is correct
9 Correct 15 ms 31760 KB Output is correct
10 Correct 14 ms 31664 KB Output is correct
11 Correct 13 ms 31700 KB Output is correct
12 Correct 13 ms 31700 KB Output is correct
13 Correct 13 ms 31660 KB Output is correct
14 Correct 13 ms 31760 KB Output is correct
15 Correct 13 ms 31700 KB Output is correct
16 Correct 16 ms 31700 KB Output is correct
17 Correct 13 ms 31756 KB Output is correct
18 Correct 12 ms 31700 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 15 ms 31700 KB Output is correct
2 Correct 14 ms 31728 KB Output is correct
3 Correct 15 ms 31716 KB Output is correct
4 Correct 14 ms 31700 KB Output is correct
5 Correct 15 ms 31760 KB Output is correct
6 Correct 15 ms 31768 KB Output is correct
7 Correct 14 ms 31752 KB Output is correct
8 Correct 17 ms 31740 KB Output is correct
9 Correct 15 ms 31760 KB Output is correct
10 Correct 14 ms 31664 KB Output is correct
11 Correct 13 ms 31700 KB Output is correct
12 Correct 13 ms 31700 KB Output is correct
13 Correct 13 ms 31660 KB Output is correct
14 Correct 13 ms 31760 KB Output is correct
15 Correct 13 ms 31700 KB Output is correct
16 Correct 16 ms 31700 KB Output is correct
17 Correct 13 ms 31756 KB Output is correct
18 Correct 12 ms 31700 KB Output is correct
19 Correct 14 ms 31692 KB Output is correct
20 Correct 19 ms 31760 KB Output is correct
21 Correct 15 ms 31828 KB Output is correct
22 Correct 19 ms 31764 KB Output is correct
23 Correct 15 ms 31780 KB Output is correct
24 Correct 13 ms 31828 KB Output is correct
25 Correct 14 ms 31768 KB Output is correct
26 Correct 16 ms 31772 KB Output is correct
27 Correct 16 ms 31720 KB Output is correct
28 Correct 15 ms 31776 KB Output is correct
29 Correct 15 ms 31700 KB Output is correct
30 Correct 15 ms 31760 KB Output is correct
31 Correct 14 ms 31828 KB Output is correct
32 Correct 16 ms 31772 KB Output is correct
33 Correct 15 ms 31752 KB Output is correct
34 Correct 14 ms 31828 KB Output is correct
35 Correct 16 ms 31740 KB Output is correct
36 Correct 14 ms 31828 KB Output is correct
37 Correct 15 ms 31772 KB Output is correct
38 Correct 15 ms 31700 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 15 ms 31700 KB Output is correct
2 Correct 14 ms 31728 KB Output is correct
3 Correct 15 ms 31716 KB Output is correct
4 Correct 14 ms 31700 KB Output is correct
5 Correct 15 ms 31760 KB Output is correct
6 Correct 15 ms 31768 KB Output is correct
7 Correct 14 ms 31752 KB Output is correct
8 Correct 17 ms 31740 KB Output is correct
9 Correct 15 ms 31760 KB Output is correct
10 Correct 14 ms 31664 KB Output is correct
11 Correct 13 ms 31700 KB Output is correct
12 Correct 13 ms 31700 KB Output is correct
13 Correct 13 ms 31660 KB Output is correct
14 Correct 13 ms 31760 KB Output is correct
15 Correct 13 ms 31700 KB Output is correct
16 Correct 16 ms 31700 KB Output is correct
17 Correct 13 ms 31756 KB Output is correct
18 Correct 12 ms 31700 KB Output is correct
19 Correct 125 ms 36976 KB Output is correct
20 Correct 133 ms 36924 KB Output is correct
21 Correct 126 ms 36900 KB Output is correct
22 Correct 112 ms 37056 KB Output is correct
23 Correct 105 ms 37248 KB Output is correct
24 Correct 60 ms 37056 KB Output is correct
25 Correct 140 ms 40568 KB Output is correct
26 Correct 109 ms 44264 KB Output is correct
27 Correct 189 ms 42044 KB Output is correct
28 Correct 325 ms 56240 KB Output is correct
29 Correct 357 ms 55204 KB Output is correct
30 Correct 193 ms 41928 KB Output is correct
31 Correct 145 ms 41940 KB Output is correct
32 Correct 221 ms 42064 KB Output is correct
33 Correct 216 ms 40940 KB Output is correct
34 Correct 248 ms 40928 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 15 ms 31700 KB Output is correct
2 Correct 14 ms 31728 KB Output is correct
3 Correct 15 ms 31716 KB Output is correct
4 Correct 14 ms 31700 KB Output is correct
5 Correct 15 ms 31760 KB Output is correct
6 Correct 15 ms 31768 KB Output is correct
7 Correct 14 ms 31752 KB Output is correct
8 Correct 17 ms 31740 KB Output is correct
9 Correct 15 ms 31760 KB Output is correct
10 Correct 14 ms 31664 KB Output is correct
11 Correct 13 ms 31700 KB Output is correct
12 Correct 13 ms 31700 KB Output is correct
13 Correct 13 ms 31660 KB Output is correct
14 Correct 13 ms 31760 KB Output is correct
15 Correct 13 ms 31700 KB Output is correct
16 Correct 16 ms 31700 KB Output is correct
17 Correct 13 ms 31756 KB Output is correct
18 Correct 12 ms 31700 KB Output is correct
19 Correct 14 ms 31692 KB Output is correct
20 Correct 19 ms 31760 KB Output is correct
21 Correct 15 ms 31828 KB Output is correct
22 Correct 19 ms 31764 KB Output is correct
23 Correct 15 ms 31780 KB Output is correct
24 Correct 13 ms 31828 KB Output is correct
25 Correct 14 ms 31768 KB Output is correct
26 Correct 16 ms 31772 KB Output is correct
27 Correct 16 ms 31720 KB Output is correct
28 Correct 15 ms 31776 KB Output is correct
29 Correct 15 ms 31700 KB Output is correct
30 Correct 15 ms 31760 KB Output is correct
31 Correct 14 ms 31828 KB Output is correct
32 Correct 16 ms 31772 KB Output is correct
33 Correct 15 ms 31752 KB Output is correct
34 Correct 14 ms 31828 KB Output is correct
35 Correct 16 ms 31740 KB Output is correct
36 Correct 14 ms 31828 KB Output is correct
37 Correct 15 ms 31772 KB Output is correct
38 Correct 15 ms 31700 KB Output is correct
39 Correct 125 ms 36976 KB Output is correct
40 Correct 133 ms 36924 KB Output is correct
41 Correct 126 ms 36900 KB Output is correct
42 Correct 112 ms 37056 KB Output is correct
43 Correct 105 ms 37248 KB Output is correct
44 Correct 60 ms 37056 KB Output is correct
45 Correct 140 ms 40568 KB Output is correct
46 Correct 109 ms 44264 KB Output is correct
47 Correct 189 ms 42044 KB Output is correct
48 Correct 325 ms 56240 KB Output is correct
49 Correct 357 ms 55204 KB Output is correct
50 Correct 193 ms 41928 KB Output is correct
51 Correct 145 ms 41940 KB Output is correct
52 Correct 221 ms 42064 KB Output is correct
53 Correct 216 ms 40940 KB Output is correct
54 Correct 248 ms 40928 KB Output is correct
55 Correct 28 ms 32376 KB Output is correct
56 Correct 22 ms 32248 KB Output is correct
57 Correct 69 ms 37108 KB Output is correct
58 Correct 40 ms 37184 KB Output is correct
59 Correct 87 ms 45116 KB Output is correct
60 Correct 339 ms 58148 KB Output is correct
61 Correct 176 ms 44628 KB Output is correct
62 Correct 150 ms 44636 KB Output is correct
63 Correct 206 ms 44676 KB Output is correct
64 Correct 374 ms 44876 KB Output is correct
65 Correct 227 ms 45380 KB Output is correct
66 Correct 349 ms 55572 KB Output is correct
67 Correct 87 ms 45236 KB Output is correct
68 Correct 265 ms 45380 KB Output is correct
69 Correct 205 ms 45580 KB Output is correct
70 Correct 175 ms 44844 KB Output is correct