Submission #718085

# Submission time Handle Problem Language Result Execution time Memory
718085 2023-04-03T09:56:44 Z andrei_c1 Race (IOI11_race) C++14
100 / 100
987 ms 37840 KB
#include <bits/stdc++.h>
#include "race.h"	

using namespace std;

const int INF = 1e9;

int k;
vector<int> sz;
vector<bool> rem;
vector<vector<pair<int, int>>> adj;

void minSelf(int &x, int y) {
	if(y < x) {
		x = y;
	}
}

void dfs(int u, int v = -1) {
	sz[u] = 1;
	for(const auto &it: adj[u]) if(it.first != v && !rem[it.first]) {
		dfs(it.first, u);
		sz[u] += sz[it.first];
	}
}

int centroid(int desired, int u, int v = -1) {
	for(const auto &it: adj[u]) if(it.first != v && !rem[it.first] && (sz[it.first] << 1) > desired) {
		return centroid(desired, it.first, u);
	}
	return u;
}

map<int, int> mp;

void dfsUpdate(int u, int v, int dep, int cnt = 1) {
	if(k >= dep) {
		if(mp.count(dep) == 0) {
			mp.emplace(dep, cnt);
		} else {
			minSelf(mp[dep], cnt);
		}
		for(const auto &it: adj[u]) if(it.first != v && !rem[it.first]) {
			dfsUpdate(it.first, u, dep + it.second, cnt + 1);
		}
	}
}

int dfsQuery(int u, int v, int dep, int cnt = 1) {
	int ans = INF;
	if(k >= dep) {
		if(mp.count(k - dep) > 0) {
			minSelf(ans, cnt + mp[k - dep]);
		}
		for(const auto &it: adj[u]) if(it.first != v && !rem[it.first]) {
			minSelf(ans, dfsQuery(it.first, u, dep + it.second, cnt + 1));
		}
	}
	return ans;
}

void preorder(int u, int v, int dep, int cnt = 1) {
	cout << u << " " << dep << " " << cnt << '\n';
	for(const auto &it: adj[u]) if(it.first != v && !rem[it.first]) {
		preorder(it.first, u, dep + it.second, cnt + 1);
	}
}

int solve(int u = 0) {
	dfs(u);
	int c = centroid(sz[u], u);

	int ans = INF;
	for(const auto &it: adj[c]) if(!rem[it.first]) {
		minSelf(ans, dfsQuery(it.first, c, it.second));
		dfsUpdate(it.first, c, it.second);
	}
	if(mp.count(k) > 0) {
		minSelf(ans, mp[k]);
	}

	rem[c] = 1;
	mp.clear();

	for(const auto &it: adj[c]) if(!rem[it.first]) {
		minSelf(ans, solve(it.first));
	}
	return ans;
}

int best_path(int N, int K, int H[][2], int L[]) {
	adj = vector<vector<pair<int, int>>> (N);

	for(int i = 0; i < N - 1; i++) {
		adj[H[i][0]].emplace_back(H[i][1], L[i]);
		adj[H[i][1]].emplace_back(H[i][0], L[i]);
	}

	sz = vector<int> (N);
	rem = vector<bool> (N);
	k = K;

	int ans = solve();
	if(ans == INF) {
		return -1;
	} else {
		return ans;
	}
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 308 KB Output is correct
2 Correct 1 ms 300 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 0 ms 304 KB Output is correct
6 Correct 1 ms 212 KB Output is correct
7 Correct 1 ms 304 KB Output is correct
8 Correct 0 ms 212 KB Output is correct
9 Correct 1 ms 304 KB Output is correct
10 Correct 1 ms 304 KB Output is correct
11 Correct 1 ms 212 KB Output is correct
12 Correct 1 ms 212 KB Output is correct
13 Correct 1 ms 312 KB Output is correct
14 Correct 1 ms 212 KB Output is correct
15 Correct 1 ms 212 KB Output is correct
16 Correct 1 ms 340 KB Output is correct
17 Correct 1 ms 212 KB Output is correct
18 Correct 1 ms 308 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 308 KB Output is correct
2 Correct 1 ms 300 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 0 ms 304 KB Output is correct
6 Correct 1 ms 212 KB Output is correct
7 Correct 1 ms 304 KB Output is correct
8 Correct 0 ms 212 KB Output is correct
9 Correct 1 ms 304 KB Output is correct
10 Correct 1 ms 304 KB Output is correct
11 Correct 1 ms 212 KB Output is correct
12 Correct 1 ms 212 KB Output is correct
13 Correct 1 ms 312 KB Output is correct
14 Correct 1 ms 212 KB Output is correct
15 Correct 1 ms 212 KB Output is correct
16 Correct 1 ms 340 KB Output is correct
17 Correct 1 ms 212 KB Output is correct
18 Correct 1 ms 308 KB Output is correct
19 Correct 1 ms 212 KB Output is correct
20 Correct 1 ms 212 KB Output is correct
21 Correct 2 ms 340 KB Output is correct
22 Correct 1 ms 340 KB Output is correct
23 Correct 1 ms 340 KB Output is correct
24 Correct 1 ms 324 KB Output is correct
25 Correct 2 ms 340 KB Output is correct
26 Correct 1 ms 340 KB Output is correct
27 Correct 1 ms 340 KB Output is correct
28 Correct 2 ms 340 KB Output is correct
29 Correct 2 ms 320 KB Output is correct
30 Correct 3 ms 440 KB Output is correct
31 Correct 2 ms 340 KB Output is correct
32 Correct 3 ms 340 KB Output is correct
33 Correct 2 ms 340 KB Output is correct
34 Correct 2 ms 316 KB Output is correct
35 Correct 2 ms 340 KB Output is correct
36 Correct 2 ms 340 KB Output is correct
37 Correct 2 ms 340 KB Output is correct
38 Correct 2 ms 340 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 308 KB Output is correct
2 Correct 1 ms 300 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 0 ms 304 KB Output is correct
6 Correct 1 ms 212 KB Output is correct
7 Correct 1 ms 304 KB Output is correct
8 Correct 0 ms 212 KB Output is correct
9 Correct 1 ms 304 KB Output is correct
10 Correct 1 ms 304 KB Output is correct
11 Correct 1 ms 212 KB Output is correct
12 Correct 1 ms 212 KB Output is correct
13 Correct 1 ms 312 KB Output is correct
14 Correct 1 ms 212 KB Output is correct
15 Correct 1 ms 212 KB Output is correct
16 Correct 1 ms 340 KB Output is correct
17 Correct 1 ms 212 KB Output is correct
18 Correct 1 ms 308 KB Output is correct
19 Correct 188 ms 9248 KB Output is correct
20 Correct 200 ms 9248 KB Output is correct
21 Correct 219 ms 9292 KB Output is correct
22 Correct 209 ms 9260 KB Output is correct
23 Correct 100 ms 9548 KB Output is correct
24 Correct 84 ms 9524 KB Output is correct
25 Correct 161 ms 12004 KB Output is correct
26 Correct 106 ms 14976 KB Output is correct
27 Correct 243 ms 18584 KB Output is correct
28 Correct 307 ms 29928 KB Output is correct
29 Correct 280 ms 29072 KB Output is correct
30 Correct 210 ms 18584 KB Output is correct
31 Correct 222 ms 18588 KB Output is correct
32 Correct 274 ms 18688 KB Output is correct
33 Correct 291 ms 17428 KB Output is correct
34 Correct 206 ms 18328 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 308 KB Output is correct
2 Correct 1 ms 300 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 0 ms 304 KB Output is correct
6 Correct 1 ms 212 KB Output is correct
7 Correct 1 ms 304 KB Output is correct
8 Correct 0 ms 212 KB Output is correct
9 Correct 1 ms 304 KB Output is correct
10 Correct 1 ms 304 KB Output is correct
11 Correct 1 ms 212 KB Output is correct
12 Correct 1 ms 212 KB Output is correct
13 Correct 1 ms 312 KB Output is correct
14 Correct 1 ms 212 KB Output is correct
15 Correct 1 ms 212 KB Output is correct
16 Correct 1 ms 340 KB Output is correct
17 Correct 1 ms 212 KB Output is correct
18 Correct 1 ms 308 KB Output is correct
19 Correct 1 ms 212 KB Output is correct
20 Correct 1 ms 212 KB Output is correct
21 Correct 2 ms 340 KB Output is correct
22 Correct 1 ms 340 KB Output is correct
23 Correct 1 ms 340 KB Output is correct
24 Correct 1 ms 324 KB Output is correct
25 Correct 2 ms 340 KB Output is correct
26 Correct 1 ms 340 KB Output is correct
27 Correct 1 ms 340 KB Output is correct
28 Correct 2 ms 340 KB Output is correct
29 Correct 2 ms 320 KB Output is correct
30 Correct 3 ms 440 KB Output is correct
31 Correct 2 ms 340 KB Output is correct
32 Correct 3 ms 340 KB Output is correct
33 Correct 2 ms 340 KB Output is correct
34 Correct 2 ms 316 KB Output is correct
35 Correct 2 ms 340 KB Output is correct
36 Correct 2 ms 340 KB Output is correct
37 Correct 2 ms 340 KB Output is correct
38 Correct 2 ms 340 KB Output is correct
39 Correct 188 ms 9248 KB Output is correct
40 Correct 200 ms 9248 KB Output is correct
41 Correct 219 ms 9292 KB Output is correct
42 Correct 209 ms 9260 KB Output is correct
43 Correct 100 ms 9548 KB Output is correct
44 Correct 84 ms 9524 KB Output is correct
45 Correct 161 ms 12004 KB Output is correct
46 Correct 106 ms 14976 KB Output is correct
47 Correct 243 ms 18584 KB Output is correct
48 Correct 307 ms 29928 KB Output is correct
49 Correct 280 ms 29072 KB Output is correct
50 Correct 210 ms 18584 KB Output is correct
51 Correct 222 ms 18588 KB Output is correct
52 Correct 274 ms 18688 KB Output is correct
53 Correct 291 ms 17428 KB Output is correct
54 Correct 206 ms 18328 KB Output is correct
55 Correct 22 ms 1340 KB Output is correct
56 Correct 15 ms 1204 KB Output is correct
57 Correct 145 ms 9484 KB Output is correct
58 Correct 44 ms 9148 KB Output is correct
59 Correct 295 ms 18492 KB Output is correct
60 Correct 978 ms 37840 KB Output is correct
61 Correct 304 ms 18868 KB Output is correct
62 Correct 329 ms 18876 KB Output is correct
63 Correct 374 ms 18836 KB Output is correct
64 Correct 987 ms 23508 KB Output is correct
65 Correct 249 ms 19516 KB Output is correct
66 Correct 667 ms 27344 KB Output is correct
67 Correct 199 ms 19520 KB Output is correct
68 Correct 409 ms 22952 KB Output is correct
69 Correct 400 ms 23072 KB Output is correct
70 Correct 371 ms 22028 KB Output is correct