Submission #1032758

# Submission time Handle Problem Language Result Execution time Memory
1032758 2024-07-24T08:05:59 Z vjudge1 Race (IOI11_race) C++17
100 / 100
265 ms 38228 KB
#include "race.h"

#include <bits/stdc++.h>

using namespace std;

namespace mysol {

constexpr int N = 2e5 + 10;
constexpr int M = 1e6 + 10;

constexpr int INF = 1e9;

// -----------------------------------------------------------------------------

int n, k;

struct edge {
	int v, w;
	edge() = default;
	edge(int v, int w): v(v), w(w) {}
};

vector<edge> g[N];

void add(int u, int v, int w) {
	g[u].emplace_back(v, w);
}

void Add(int u, int v, int w) {
	add(u, v, w), add(v, u, w);
}

int get_root(int);

// -----------------------------------------------------------------------------

int mp[M];

int vis[N], dis[N], dep[N];

int cnt, ans = INF;

void init(int u, int fa, int is, int ep) {
	if (is > k) return;
	++cnt;
	dis[cnt] = is, dep[cnt] = ep;
	for (auto t : g[u]) {
		if (t.v == fa || vis[t.v]) continue;
		init(t.v, u, is + t.w, ep + 1);
	}
}

void calc(int u) {
	mp[0] = cnt = 0;
	for (auto t : g[u]) {
		int v = t.v, w = t.w;
		if (vis[v]) continue;
		int st = cnt + 1;
		init(v, u, w, 1);
		for (int i = st; i <= cnt; ++i)
			ans = min(ans, mp[k - dis[i]] + dep[i]);
		for (int i = st; i <= cnt; ++i)
			mp[dis[i]] = min(mp[dis[i]], dep[i]);
	}
	for (int i = 1; i <= cnt; ++i) mp[dis[i]] = INF;
}

void solve(int u) {
	vis[u] = 1, calc(u);
	for (auto t : g[u]) {
		if (vis[t.v]) continue;
		solve(get_root(t.v));
	}
}

// -----------------------------------------------------------------------------

int siz[N], max_son[N];
int tot, root;

void dfs(int u, int fa) {
	siz[u] = 1, max_son[u] = 0;
	for (auto t : g[u]) {
		int v = t.v;
		if (v == fa || vis[v]) continue;
		dfs(v, u);
		siz[u] += siz[v];
		max_son[u] = max(max_son[u], siz[v]);
	}
	max_son[u] = max(max_son[u], tot - siz[u]);
	if (max_son[u] < max_son[root]) root = u;
}

int get_root(int u) {
	tot = siz[u], root = 0;
	return dfs(u, -1), root;
}

// -----------------------------------------------------------------------------

/*signed main() {
	ios::sync_with_stdio(false);
	cin.tie(nullptr), cout.tie(nullptr);
	cin >> n >> k;
	memset(mp, 0x3f, sizeof mp);
	for (int i = 1; i < n; ++i) {
		int u, v, w;
		cin >> u >> v >> w;
		Add(u + 1, v + 1, w);
	}
	max_son[0] = n + 1;
	siz[1] = n;
	solve(get_root(1));
	cout << (ans >= n ? -1 : ans) << endl;
	return 0;
}*/

}

using namespace mysol;

int best_path(int N, int K, int H[][2], int L[])
{
	n = N, k = K;
	memset(mp, 0x3f, sizeof mp);
	for (int i = 0; i < n - 1; ++i) {
		int u = H[i][0], v = H[i][1], w = L[i];
		Add(u + 1, v + 1, w);
	}
	max_son[0] = n + 1;
	siz[1] = n;
	solve(get_root(1));
	return ans >= n ? -1 : ans;
}
# Verdict Execution time Memory Grader output
1 Correct 4 ms 9048 KB Output is correct
2 Correct 4 ms 9052 KB Output is correct
3 Correct 4 ms 9052 KB Output is correct
4 Correct 3 ms 9052 KB Output is correct
5 Correct 3 ms 9056 KB Output is correct
6 Correct 4 ms 9056 KB Output is correct
7 Correct 3 ms 9052 KB Output is correct
8 Correct 4 ms 9048 KB Output is correct
9 Correct 4 ms 9052 KB Output is correct
10 Correct 3 ms 9052 KB Output is correct
11 Correct 3 ms 9052 KB Output is correct
12 Correct 3 ms 9052 KB Output is correct
13 Correct 4 ms 8960 KB Output is correct
14 Correct 4 ms 9052 KB Output is correct
15 Correct 3 ms 9052 KB Output is correct
16 Correct 4 ms 9060 KB Output is correct
17 Correct 5 ms 9052 KB Output is correct
18 Correct 4 ms 9052 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 9048 KB Output is correct
2 Correct 4 ms 9052 KB Output is correct
3 Correct 4 ms 9052 KB Output is correct
4 Correct 3 ms 9052 KB Output is correct
5 Correct 3 ms 9056 KB Output is correct
6 Correct 4 ms 9056 KB Output is correct
7 Correct 3 ms 9052 KB Output is correct
8 Correct 4 ms 9048 KB Output is correct
9 Correct 4 ms 9052 KB Output is correct
10 Correct 3 ms 9052 KB Output is correct
11 Correct 3 ms 9052 KB Output is correct
12 Correct 3 ms 9052 KB Output is correct
13 Correct 4 ms 8960 KB Output is correct
14 Correct 4 ms 9052 KB Output is correct
15 Correct 3 ms 9052 KB Output is correct
16 Correct 4 ms 9060 KB Output is correct
17 Correct 5 ms 9052 KB Output is correct
18 Correct 4 ms 9052 KB Output is correct
19 Correct 4 ms 9052 KB Output is correct
20 Correct 4 ms 9052 KB Output is correct
21 Correct 5 ms 8920 KB Output is correct
22 Correct 4 ms 9052 KB Output is correct
23 Correct 4 ms 8996 KB Output is correct
24 Correct 4 ms 9048 KB Output is correct
25 Correct 4 ms 9168 KB Output is correct
26 Correct 4 ms 9052 KB Output is correct
27 Correct 4 ms 9004 KB Output is correct
28 Correct 4 ms 9096 KB Output is correct
29 Correct 4 ms 9016 KB Output is correct
30 Correct 4 ms 9052 KB Output is correct
31 Correct 4 ms 9052 KB Output is correct
32 Correct 4 ms 9052 KB Output is correct
33 Correct 4 ms 9140 KB Output is correct
34 Correct 4 ms 9052 KB Output is correct
35 Correct 4 ms 9052 KB Output is correct
36 Correct 4 ms 9052 KB Output is correct
37 Correct 4 ms 9172 KB Output is correct
38 Correct 4 ms 9172 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 9048 KB Output is correct
2 Correct 4 ms 9052 KB Output is correct
3 Correct 4 ms 9052 KB Output is correct
4 Correct 3 ms 9052 KB Output is correct
5 Correct 3 ms 9056 KB Output is correct
6 Correct 4 ms 9056 KB Output is correct
7 Correct 3 ms 9052 KB Output is correct
8 Correct 4 ms 9048 KB Output is correct
9 Correct 4 ms 9052 KB Output is correct
10 Correct 3 ms 9052 KB Output is correct
11 Correct 3 ms 9052 KB Output is correct
12 Correct 3 ms 9052 KB Output is correct
13 Correct 4 ms 8960 KB Output is correct
14 Correct 4 ms 9052 KB Output is correct
15 Correct 3 ms 9052 KB Output is correct
16 Correct 4 ms 9060 KB Output is correct
17 Correct 5 ms 9052 KB Output is correct
18 Correct 4 ms 9052 KB Output is correct
19 Correct 74 ms 15252 KB Output is correct
20 Correct 88 ms 15956 KB Output is correct
21 Correct 80 ms 15956 KB Output is correct
22 Correct 69 ms 17124 KB Output is correct
23 Correct 54 ms 16720 KB Output is correct
24 Correct 37 ms 16412 KB Output is correct
25 Correct 78 ms 19868 KB Output is correct
26 Correct 59 ms 23756 KB Output is correct
27 Correct 99 ms 22864 KB Output is correct
28 Correct 149 ms 37088 KB Output is correct
29 Correct 168 ms 36432 KB Output is correct
30 Correct 89 ms 23380 KB Output is correct
31 Correct 94 ms 23376 KB Output is correct
32 Correct 118 ms 23372 KB Output is correct
33 Correct 157 ms 22356 KB Output is correct
34 Correct 124 ms 22552 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 9048 KB Output is correct
2 Correct 4 ms 9052 KB Output is correct
3 Correct 4 ms 9052 KB Output is correct
4 Correct 3 ms 9052 KB Output is correct
5 Correct 3 ms 9056 KB Output is correct
6 Correct 4 ms 9056 KB Output is correct
7 Correct 3 ms 9052 KB Output is correct
8 Correct 4 ms 9048 KB Output is correct
9 Correct 4 ms 9052 KB Output is correct
10 Correct 3 ms 9052 KB Output is correct
11 Correct 3 ms 9052 KB Output is correct
12 Correct 3 ms 9052 KB Output is correct
13 Correct 4 ms 8960 KB Output is correct
14 Correct 4 ms 9052 KB Output is correct
15 Correct 3 ms 9052 KB Output is correct
16 Correct 4 ms 9060 KB Output is correct
17 Correct 5 ms 9052 KB Output is correct
18 Correct 4 ms 9052 KB Output is correct
19 Correct 4 ms 9052 KB Output is correct
20 Correct 4 ms 9052 KB Output is correct
21 Correct 5 ms 8920 KB Output is correct
22 Correct 4 ms 9052 KB Output is correct
23 Correct 4 ms 8996 KB Output is correct
24 Correct 4 ms 9048 KB Output is correct
25 Correct 4 ms 9168 KB Output is correct
26 Correct 4 ms 9052 KB Output is correct
27 Correct 4 ms 9004 KB Output is correct
28 Correct 4 ms 9096 KB Output is correct
29 Correct 4 ms 9016 KB Output is correct
30 Correct 4 ms 9052 KB Output is correct
31 Correct 4 ms 9052 KB Output is correct
32 Correct 4 ms 9052 KB Output is correct
33 Correct 4 ms 9140 KB Output is correct
34 Correct 4 ms 9052 KB Output is correct
35 Correct 4 ms 9052 KB Output is correct
36 Correct 4 ms 9052 KB Output is correct
37 Correct 4 ms 9172 KB Output is correct
38 Correct 4 ms 9172 KB Output is correct
39 Correct 74 ms 15252 KB Output is correct
40 Correct 88 ms 15956 KB Output is correct
41 Correct 80 ms 15956 KB Output is correct
42 Correct 69 ms 17124 KB Output is correct
43 Correct 54 ms 16720 KB Output is correct
44 Correct 37 ms 16412 KB Output is correct
45 Correct 78 ms 19868 KB Output is correct
46 Correct 59 ms 23756 KB Output is correct
47 Correct 99 ms 22864 KB Output is correct
48 Correct 149 ms 37088 KB Output is correct
49 Correct 168 ms 36432 KB Output is correct
50 Correct 89 ms 23380 KB Output is correct
51 Correct 94 ms 23376 KB Output is correct
52 Correct 118 ms 23372 KB Output is correct
53 Correct 157 ms 22356 KB Output is correct
54 Correct 124 ms 22552 KB Output is correct
55 Correct 9 ms 9820 KB Output is correct
56 Correct 9 ms 9616 KB Output is correct
57 Correct 47 ms 16208 KB Output is correct
58 Correct 25 ms 17092 KB Output is correct
59 Correct 70 ms 24144 KB Output is correct
60 Correct 226 ms 38228 KB Output is correct
61 Correct 142 ms 22868 KB Output is correct
62 Correct 101 ms 24876 KB Output is correct
63 Correct 130 ms 24896 KB Output is correct
64 Correct 265 ms 23360 KB Output is correct
65 Correct 145 ms 24404 KB Output is correct
66 Correct 223 ms 31760 KB Output is correct
67 Correct 62 ms 26400 KB Output is correct
68 Correct 125 ms 24200 KB Output is correct
69 Correct 138 ms 22224 KB Output is correct
70 Correct 120 ms 24144 KB Output is correct