Submission #943489

# Submission time Handle Problem Language Result Execution time Memory
943489 2024-03-11T14:21:03 Z SoSmolSten Race (IOI11_race) C++17
100 / 100
261 ms 36944 KB
#include <bits/stdc++.h>
#include "race.h"
#define pp pair<int, int>
#define F first
#define S second
using namespace std;
const int SZ = 2e5 + 10, M = 1e6 + 10, INF = 1e9;
vector<pp> graph[SZ];
int mn[M], ti[M], c[SZ], n, k, ans = INF, cnt = 0;
bool del[SZ];

void dfs(int u, int p){
	c[u] = 1;
	for(auto &[v, w] : graph[u]){
		if(v == p || del[v]) continue;
		dfs(v, u);
		c[u] += c[v];
	}
}

void upd(int u, int p, int w, int d){
	if(w > k) return; 
	if(ti[w] != cnt) mn[w] = d;
	else mn[w] = min(mn[w], d);
	ti[w] = cnt;
	
	for(auto &[v, l] : graph[u]){
		if(v == p || del[v]) continue;
		upd(v, u, w + l, d + 1);
	}
}

void calc(int u, int p, int w, int d){
	if(w > k) return;
	if(ti[k - w] == cnt) ans = min(ans, mn[k - w] + d);

	for(auto &[v, l] : graph[u]){
		if(v == p || del[v]) continue;
		calc(v, u, w + l, d + 1);
	}
}

int centroid(int u, int p, int sz) {
	for(auto &[v, _] : graph[u]){
		if(v == p || del[v]) continue;
		if (c[v] > (sz >> 1)) return centroid(v, u, sz);
	}
	return u;
}

void solve(int u){
	dfs(u, 0);
	u = centroid(u, 0, c[u]);
	del[u] = 1;
	ti[0] = ++cnt;
	for(auto &[v, w] : graph[u]){
		if(del[v]) continue;
		calc(v, u, w, 1);
		upd(v, u, w, 1);
	}
	for(auto &[v, _] : graph[u]){
		if(!del[v]) solve(v);
	}
}

int best_path(int N, int K, int H[][2], int L[]){
	n = N, k = K;
	for(int i = 1, u, v, w; i < n; ++i){
		u = H[i - 1][0] + 1;
		v = H[i - 1][1] + 1;
		w = L[i - 1];
		graph[u].push_back({v, w});
		graph[v].push_back({u, w});
	}
	fill(mn, mn + M - 5, INF);
	mn[0] = 0;
	solve(1);
	if(ans == INF) return -1;
  	return ans;
}

# Verdict Execution time Memory Grader output
1 Correct 3 ms 15448 KB Output is correct
2 Correct 3 ms 15448 KB Output is correct
3 Correct 3 ms 15448 KB Output is correct
4 Correct 3 ms 15704 KB Output is correct
5 Correct 3 ms 15452 KB Output is correct
6 Correct 3 ms 15448 KB Output is correct
7 Correct 3 ms 15452 KB Output is correct
8 Correct 3 ms 15452 KB Output is correct
9 Correct 3 ms 15452 KB Output is correct
10 Correct 3 ms 15452 KB Output is correct
11 Correct 3 ms 15452 KB Output is correct
12 Correct 3 ms 15452 KB Output is correct
13 Correct 3 ms 15452 KB Output is correct
14 Correct 4 ms 15448 KB Output is correct
15 Correct 3 ms 15452 KB Output is correct
16 Correct 3 ms 15452 KB Output is correct
17 Correct 3 ms 15452 KB Output is correct
18 Correct 4 ms 15452 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 15448 KB Output is correct
2 Correct 3 ms 15448 KB Output is correct
3 Correct 3 ms 15448 KB Output is correct
4 Correct 3 ms 15704 KB Output is correct
5 Correct 3 ms 15452 KB Output is correct
6 Correct 3 ms 15448 KB Output is correct
7 Correct 3 ms 15452 KB Output is correct
8 Correct 3 ms 15452 KB Output is correct
9 Correct 3 ms 15452 KB Output is correct
10 Correct 3 ms 15452 KB Output is correct
11 Correct 3 ms 15452 KB Output is correct
12 Correct 3 ms 15452 KB Output is correct
13 Correct 3 ms 15452 KB Output is correct
14 Correct 4 ms 15448 KB Output is correct
15 Correct 3 ms 15452 KB Output is correct
16 Correct 3 ms 15452 KB Output is correct
17 Correct 3 ms 15452 KB Output is correct
18 Correct 4 ms 15452 KB Output is correct
19 Correct 3 ms 15452 KB Output is correct
20 Correct 3 ms 15288 KB Output is correct
21 Correct 3 ms 15536 KB Output is correct
22 Correct 4 ms 16988 KB Output is correct
23 Correct 4 ms 16476 KB Output is correct
24 Correct 5 ms 16988 KB Output is correct
25 Correct 4 ms 15820 KB Output is correct
26 Correct 3 ms 15452 KB Output is correct
27 Correct 3 ms 15452 KB Output is correct
28 Correct 3 ms 15452 KB Output is correct
29 Correct 3 ms 15452 KB Output is correct
30 Correct 3 ms 15452 KB Output is correct
31 Correct 4 ms 15708 KB Output is correct
32 Correct 3 ms 15708 KB Output is correct
33 Correct 4 ms 16476 KB Output is correct
34 Correct 4 ms 16220 KB Output is correct
35 Correct 4 ms 16728 KB Output is correct
36 Correct 4 ms 17244 KB Output is correct
37 Correct 3 ms 15964 KB Output is correct
38 Correct 4 ms 15452 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 15448 KB Output is correct
2 Correct 3 ms 15448 KB Output is correct
3 Correct 3 ms 15448 KB Output is correct
4 Correct 3 ms 15704 KB Output is correct
5 Correct 3 ms 15452 KB Output is correct
6 Correct 3 ms 15448 KB Output is correct
7 Correct 3 ms 15452 KB Output is correct
8 Correct 3 ms 15452 KB Output is correct
9 Correct 3 ms 15452 KB Output is correct
10 Correct 3 ms 15452 KB Output is correct
11 Correct 3 ms 15452 KB Output is correct
12 Correct 3 ms 15452 KB Output is correct
13 Correct 3 ms 15452 KB Output is correct
14 Correct 4 ms 15448 KB Output is correct
15 Correct 3 ms 15452 KB Output is correct
16 Correct 3 ms 15452 KB Output is correct
17 Correct 3 ms 15452 KB Output is correct
18 Correct 4 ms 15452 KB Output is correct
19 Correct 79 ms 21332 KB Output is correct
20 Correct 77 ms 21328 KB Output is correct
21 Correct 79 ms 21336 KB Output is correct
22 Correct 76 ms 21412 KB Output is correct
23 Correct 45 ms 21588 KB Output is correct
24 Correct 38 ms 21340 KB Output is correct
25 Correct 75 ms 23932 KB Output is correct
26 Correct 67 ms 26944 KB Output is correct
27 Correct 100 ms 25172 KB Output is correct
28 Correct 174 ms 36408 KB Output is correct
29 Correct 157 ms 35408 KB Output is correct
30 Correct 111 ms 25172 KB Output is correct
31 Correct 112 ms 25428 KB Output is correct
32 Correct 116 ms 25168 KB Output is correct
33 Correct 121 ms 24144 KB Output is correct
34 Correct 127 ms 24324 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 15448 KB Output is correct
2 Correct 3 ms 15448 KB Output is correct
3 Correct 3 ms 15448 KB Output is correct
4 Correct 3 ms 15704 KB Output is correct
5 Correct 3 ms 15452 KB Output is correct
6 Correct 3 ms 15448 KB Output is correct
7 Correct 3 ms 15452 KB Output is correct
8 Correct 3 ms 15452 KB Output is correct
9 Correct 3 ms 15452 KB Output is correct
10 Correct 3 ms 15452 KB Output is correct
11 Correct 3 ms 15452 KB Output is correct
12 Correct 3 ms 15452 KB Output is correct
13 Correct 3 ms 15452 KB Output is correct
14 Correct 4 ms 15448 KB Output is correct
15 Correct 3 ms 15452 KB Output is correct
16 Correct 3 ms 15452 KB Output is correct
17 Correct 3 ms 15452 KB Output is correct
18 Correct 4 ms 15452 KB Output is correct
19 Correct 3 ms 15452 KB Output is correct
20 Correct 3 ms 15288 KB Output is correct
21 Correct 3 ms 15536 KB Output is correct
22 Correct 4 ms 16988 KB Output is correct
23 Correct 4 ms 16476 KB Output is correct
24 Correct 5 ms 16988 KB Output is correct
25 Correct 4 ms 15820 KB Output is correct
26 Correct 3 ms 15452 KB Output is correct
27 Correct 3 ms 15452 KB Output is correct
28 Correct 3 ms 15452 KB Output is correct
29 Correct 3 ms 15452 KB Output is correct
30 Correct 3 ms 15452 KB Output is correct
31 Correct 4 ms 15708 KB Output is correct
32 Correct 3 ms 15708 KB Output is correct
33 Correct 4 ms 16476 KB Output is correct
34 Correct 4 ms 16220 KB Output is correct
35 Correct 4 ms 16728 KB Output is correct
36 Correct 4 ms 17244 KB Output is correct
37 Correct 3 ms 15964 KB Output is correct
38 Correct 4 ms 15452 KB Output is correct
39 Correct 79 ms 21332 KB Output is correct
40 Correct 77 ms 21328 KB Output is correct
41 Correct 79 ms 21336 KB Output is correct
42 Correct 76 ms 21412 KB Output is correct
43 Correct 45 ms 21588 KB Output is correct
44 Correct 38 ms 21340 KB Output is correct
45 Correct 75 ms 23932 KB Output is correct
46 Correct 67 ms 26944 KB Output is correct
47 Correct 100 ms 25172 KB Output is correct
48 Correct 174 ms 36408 KB Output is correct
49 Correct 157 ms 35408 KB Output is correct
50 Correct 111 ms 25172 KB Output is correct
51 Correct 112 ms 25428 KB Output is correct
52 Correct 116 ms 25168 KB Output is correct
53 Correct 121 ms 24144 KB Output is correct
54 Correct 127 ms 24324 KB Output is correct
55 Correct 8 ms 15708 KB Output is correct
56 Correct 9 ms 15708 KB Output is correct
57 Correct 51 ms 21644 KB Output is correct
58 Correct 27 ms 21660 KB Output is correct
59 Correct 68 ms 27112 KB Output is correct
60 Correct 253 ms 36944 KB Output is correct
61 Correct 112 ms 25172 KB Output is correct
62 Correct 114 ms 25168 KB Output is correct
63 Correct 149 ms 25172 KB Output is correct
64 Correct 261 ms 24912 KB Output is correct
65 Correct 113 ms 25144 KB Output is correct
66 Correct 222 ms 35396 KB Output is correct
67 Correct 70 ms 25540 KB Output is correct
68 Correct 166 ms 26420 KB Output is correct
69 Correct 150 ms 26452 KB Output is correct
70 Correct 153 ms 26448 KB Output is correct