Submission #832020

# Submission time Handle Problem Language Result Execution time Memory
832020 2023-08-20T20:21:05 Z serifefedartar Race (IOI11_race) C++17
100 / 100
1029 ms 46332 KB
#include "race.h"
#include <bits/stdc++.h>
using namespace std;
 
#define fast ios::sync_with_stdio(0);cin.tie(0);
typedef long long ll;
#define f first
#define s second
#define MOD 1000000007
#define LOGN 18
#define MAXN 1000005

vector<vector<pair<int,int>>> graph;
vector<int> sz, marked;
map<int,int> mp;
int req_len;
int ans = 1e8;
int get_sz(int node, int parent) {
	sz[node] = 1;
	for (auto u : graph[node]) {
		if (u.f != parent && !marked[u.f])
			sz[node] += get_sz(u.f, node);
	}
	return sz[node];
}

int find_centro(int node, int parent, int n) {
	for (auto u : graph[node]) {
		if (u.f != parent && !marked[u.f] && sz[u.f] * 2 >= n)
			return find_centro(u.f, node, n);
	}
	return node;
}

void eval(int node, int parent, int dist, int edges) {
	if (dist > req_len)
		return ;
	if (req_len-dist == 0)
		ans = min(ans, edges);
	else if (req_len-dist != 0 && mp[req_len-dist] != 0)
		ans = min(ans, mp[req_len - dist] + edges);
	for (auto u : graph[node]) {
		if (u.f != parent && !marked[u.f])
			eval(u.f, node, dist + u.s, edges + 1);
	}
}

void add(int node, int parent, int dist, int edges) {
	if (dist > req_len)
		return ;
	if (mp[dist] == 0)
		mp[dist] = edges;
	else
		mp[dist] = min(mp[dist], edges);
	for (auto u : graph[node]) {
		if (u.f != parent && !marked[u.f])
			add(u.f, node, dist + u.s, edges + 1);
	}
}

void solve(int node, int parent) {
	int n = get_sz(node, parent);
	int centro = find_centro(node, parent, n);

	marked[centro] = true;
	mp = map<int,int>();
	for (auto u : graph[centro]) {
		if (!marked[u.f]) {
			eval(u.f, centro, u.s, 1);
			add(u.f, centro, u.s, 1);
		}
	}

	for (auto u : graph[centro]) {
		if (u.f != parent && !marked[u.f])
			solve(u.f, centro);
	}
}

int best_path(int N, int K, int H[][2], int L[]) {
	req_len = K;
	graph = vector<vector<pair<int,int>>>(N, vector<pair<int,int>>());
	sz = vector<int>(N);
	marked = vector<int>(N, false);
	for (int i = 0; i < N-1; i++) {
		graph[H[i][0]].push_back({H[i][1], L[i]});
		graph[H[i][1]].push_back({H[i][0], L[i]});
	}
	solve(0, 0);

	if (ans >= 1e8)
		return -1;
	else
		return ans;
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 1 ms 212 KB Output is correct
7 Correct 0 ms 212 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 1 ms 212 KB Output is correct
10 Correct 0 ms 212 KB Output is correct
11 Correct 0 ms 212 KB Output is correct
12 Correct 1 ms 212 KB Output is correct
13 Correct 1 ms 212 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 212 KB Output is correct
17 Correct 1 ms 212 KB Output is correct
18 Correct 1 ms 340 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 1 ms 212 KB Output is correct
7 Correct 0 ms 212 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 1 ms 212 KB Output is correct
10 Correct 0 ms 212 KB Output is correct
11 Correct 0 ms 212 KB Output is correct
12 Correct 1 ms 212 KB Output is correct
13 Correct 1 ms 212 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 212 KB Output is correct
17 Correct 1 ms 212 KB Output is correct
18 Correct 1 ms 340 KB Output is correct
19 Correct 1 ms 308 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 2 ms 340 KB Output is correct
24 Correct 1 ms 340 KB Output is correct
25 Correct 2 ms 468 KB Output is correct
26 Correct 2 ms 340 KB Output is correct
27 Correct 1 ms 320 KB Output is correct
28 Correct 2 ms 340 KB Output is correct
29 Correct 2 ms 468 KB Output is correct
30 Correct 2 ms 468 KB Output is correct
31 Correct 2 ms 440 KB Output is correct
32 Correct 2 ms 468 KB Output is correct
33 Correct 2 ms 468 KB Output is correct
34 Correct 2 ms 340 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 468 KB Output is correct
38 Correct 2 ms 468 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 1 ms 212 KB Output is correct
7 Correct 0 ms 212 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 1 ms 212 KB Output is correct
10 Correct 0 ms 212 KB Output is correct
11 Correct 0 ms 212 KB Output is correct
12 Correct 1 ms 212 KB Output is correct
13 Correct 1 ms 212 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 212 KB Output is correct
17 Correct 1 ms 212 KB Output is correct
18 Correct 1 ms 340 KB Output is correct
19 Correct 186 ms 9260 KB Output is correct
20 Correct 169 ms 9628 KB Output is correct
21 Correct 194 ms 9648 KB Output is correct
22 Correct 166 ms 9704 KB Output is correct
23 Correct 67 ms 9932 KB Output is correct
24 Correct 62 ms 9852 KB Output is correct
25 Correct 169 ms 12396 KB Output is correct
26 Correct 99 ms 15356 KB Output is correct
27 Correct 156 ms 19340 KB Output is correct
28 Correct 182 ms 30728 KB Output is correct
29 Correct 183 ms 29936 KB Output is correct
30 Correct 166 ms 19336 KB Output is correct
31 Correct 158 ms 19292 KB Output is correct
32 Correct 163 ms 19344 KB Output is correct
33 Correct 273 ms 18196 KB Output is correct
34 Correct 109 ms 19020 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 1 ms 212 KB Output is correct
7 Correct 0 ms 212 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 1 ms 212 KB Output is correct
10 Correct 0 ms 212 KB Output is correct
11 Correct 0 ms 212 KB Output is correct
12 Correct 1 ms 212 KB Output is correct
13 Correct 1 ms 212 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 212 KB Output is correct
17 Correct 1 ms 212 KB Output is correct
18 Correct 1 ms 340 KB Output is correct
19 Correct 1 ms 308 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 2 ms 340 KB Output is correct
24 Correct 1 ms 340 KB Output is correct
25 Correct 2 ms 468 KB Output is correct
26 Correct 2 ms 340 KB Output is correct
27 Correct 1 ms 320 KB Output is correct
28 Correct 2 ms 340 KB Output is correct
29 Correct 2 ms 468 KB Output is correct
30 Correct 2 ms 468 KB Output is correct
31 Correct 2 ms 440 KB Output is correct
32 Correct 2 ms 468 KB Output is correct
33 Correct 2 ms 468 KB Output is correct
34 Correct 2 ms 340 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 468 KB Output is correct
38 Correct 2 ms 468 KB Output is correct
39 Correct 186 ms 9260 KB Output is correct
40 Correct 169 ms 9628 KB Output is correct
41 Correct 194 ms 9648 KB Output is correct
42 Correct 166 ms 9704 KB Output is correct
43 Correct 67 ms 9932 KB Output is correct
44 Correct 62 ms 9852 KB Output is correct
45 Correct 169 ms 12396 KB Output is correct
46 Correct 99 ms 15356 KB Output is correct
47 Correct 156 ms 19340 KB Output is correct
48 Correct 182 ms 30728 KB Output is correct
49 Correct 183 ms 29936 KB Output is correct
50 Correct 166 ms 19336 KB Output is correct
51 Correct 158 ms 19292 KB Output is correct
52 Correct 163 ms 19344 KB Output is correct
53 Correct 273 ms 18196 KB Output is correct
54 Correct 109 ms 19020 KB Output is correct
55 Correct 22 ms 1340 KB Output is correct
56 Correct 14 ms 1248 KB Output is correct
57 Correct 152 ms 9864 KB Output is correct
58 Correct 37 ms 9564 KB Output is correct
59 Correct 326 ms 21964 KB Output is correct
60 Correct 1029 ms 46332 KB Output is correct
61 Correct 269 ms 19800 KB Output is correct
62 Correct 278 ms 19748 KB Output is correct
63 Correct 339 ms 19652 KB Output is correct
64 Correct 970 ms 28828 KB Output is correct
65 Correct 116 ms 20272 KB Output is correct
66 Correct 615 ms 28208 KB Output is correct
67 Correct 95 ms 20240 KB Output is correct
68 Correct 400 ms 27260 KB Output is correct
69 Correct 418 ms 27224 KB Output is correct
70 Correct 370 ms 26140 KB Output is correct