Submission #307516

# Submission time Handle Problem Language Result Execution time Memory
307516 2020-09-28T12:42:54 Z tengiz05 Race (IOI11_race) C++17
100 / 100
1351 ms 36076 KB
#include "race.h"
#include <bits/stdc++.h>
using namespace std;
const int NN = 2e5 + 5;
bool used[NN];
int sz[NN];
int n, k;
vector<pair<int, int>> edges[NN];
void dfs_size(int u, int p){
	sz[u] = 1;
	for(auto X : edges[u]){
		int v = X.first;
		if(used[v] || v == p)continue;
		dfs_size(v, u);
		sz[u] += sz[v];
	}
}
int centroid(int u, int p, int size){
	for(auto X : edges[u]){
		int v = X.first;
		if(used[v] || v == p)continue;
		if(sz[v] > size/2)return centroid(v, u, size);
	}
	return u;
}
vector<pair<int, int>> m;
map<int, int> len;
void dfs(int u, int p, int dist, int t){
	if(dist > k)return;
	m.push_back({dist, t});
	for(auto X : edges[u]){
		int v = X.first;
		int cost = X.second;
		if(used[v] || v == p)continue;
		dfs(v, u, dist+cost, t+1);
	}
}
void dfs2(int u, int p, int dist, int t){
	if(dist > k)return;
	if(!len.count(dist))len[dist] = t;
	else len[dist] = min(len[dist], t);
	for(auto X : edges[u]){
		int v = X.first;
		int cost = X.second;
		if(used[v] || v == p)continue;
		dfs2(v, u, dist+cost, t+1);
	}
}

int find_min(int u, int p=-1){
	int ans = 1e9;
	len.clear();
	dfs_size(u, u);
	u = centroid(u, u, sz[u]);
	len[0] = 0;
	used[u] = true;//delete centroid
	for(auto X : edges[u]){
		m.clear();
		int v = X.first;
		int cost = X.second;
		if(used[v])continue;
		dfs(v, u, cost, 1);
		for(auto X : m){
			int dist = X.first;
			int time = X.second;
			if(len.count(k-dist)){
				ans = min(ans, len[k-dist]+time);
			}//cout << dist << ' ' << time << '\n';
		}
		dfs2(v, u, cost, 1);
	}
	for(auto X : edges[u]){
		int v = X.first;
		if(used[v] || p == v)continue;
		ans = min(ans, find_min(v, u));
	}
	return ans;
}
int best_path(int N, int K, int H[][2], int L[]){
	n = N;
	k = K;
	for(int i=0;i<n-1;i++){
		edges[H[i][0]].push_back({H[i][1],L[i]});
		edges[H[i][1]].push_back({H[i][0],L[i]}); 
	}
	if(k == 0){
		return 0;
	}
	int ans = find_min(0);
//	cout << '\n' << ans << '\n';
	if(ans == 1e9)ans = -1;
	return ans;
}
# Verdict Execution time Memory Grader output
1 Correct 3 ms 4992 KB Output is correct
2 Correct 3 ms 5120 KB Output is correct
3 Correct 3 ms 4992 KB Output is correct
4 Correct 3 ms 4992 KB Output is correct
5 Correct 3 ms 4992 KB Output is correct
6 Correct 3 ms 4992 KB Output is correct
7 Correct 3 ms 4992 KB Output is correct
8 Correct 4 ms 5120 KB Output is correct
9 Correct 3 ms 4992 KB Output is correct
10 Correct 3 ms 5120 KB Output is correct
11 Correct 3 ms 4992 KB Output is correct
12 Correct 4 ms 4992 KB Output is correct
13 Correct 4 ms 4992 KB Output is correct
14 Correct 3 ms 4992 KB Output is correct
15 Correct 4 ms 5120 KB Output is correct
16 Correct 4 ms 4992 KB Output is correct
17 Correct 4 ms 4992 KB Output is correct
18 Correct 3 ms 4992 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 4992 KB Output is correct
2 Correct 3 ms 5120 KB Output is correct
3 Correct 3 ms 4992 KB Output is correct
4 Correct 3 ms 4992 KB Output is correct
5 Correct 3 ms 4992 KB Output is correct
6 Correct 3 ms 4992 KB Output is correct
7 Correct 3 ms 4992 KB Output is correct
8 Correct 4 ms 5120 KB Output is correct
9 Correct 3 ms 4992 KB Output is correct
10 Correct 3 ms 5120 KB Output is correct
11 Correct 3 ms 4992 KB Output is correct
12 Correct 4 ms 4992 KB Output is correct
13 Correct 4 ms 4992 KB Output is correct
14 Correct 3 ms 4992 KB Output is correct
15 Correct 4 ms 5120 KB Output is correct
16 Correct 4 ms 4992 KB Output is correct
17 Correct 4 ms 4992 KB Output is correct
18 Correct 3 ms 4992 KB Output is correct
19 Correct 4 ms 4992 KB Output is correct
20 Correct 4 ms 4992 KB Output is correct
21 Correct 5 ms 5120 KB Output is correct
22 Correct 4 ms 5120 KB Output is correct
23 Correct 4 ms 5120 KB Output is correct
24 Correct 4 ms 5120 KB Output is correct
25 Correct 5 ms 5120 KB Output is correct
26 Correct 4 ms 5120 KB Output is correct
27 Correct 6 ms 5248 KB Output is correct
28 Correct 5 ms 5120 KB Output is correct
29 Correct 5 ms 5120 KB Output is correct
30 Correct 5 ms 5120 KB Output is correct
31 Correct 5 ms 5120 KB Output is correct
32 Correct 5 ms 5120 KB Output is correct
33 Correct 6 ms 5120 KB Output is correct
34 Correct 4 ms 5120 KB Output is correct
35 Correct 5 ms 5120 KB Output is correct
36 Correct 5 ms 5120 KB Output is correct
37 Correct 5 ms 5120 KB Output is correct
38 Correct 5 ms 5120 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 4992 KB Output is correct
2 Correct 3 ms 5120 KB Output is correct
3 Correct 3 ms 4992 KB Output is correct
4 Correct 3 ms 4992 KB Output is correct
5 Correct 3 ms 4992 KB Output is correct
6 Correct 3 ms 4992 KB Output is correct
7 Correct 3 ms 4992 KB Output is correct
8 Correct 4 ms 5120 KB Output is correct
9 Correct 3 ms 4992 KB Output is correct
10 Correct 3 ms 5120 KB Output is correct
11 Correct 3 ms 4992 KB Output is correct
12 Correct 4 ms 4992 KB Output is correct
13 Correct 4 ms 4992 KB Output is correct
14 Correct 3 ms 4992 KB Output is correct
15 Correct 4 ms 5120 KB Output is correct
16 Correct 4 ms 4992 KB Output is correct
17 Correct 4 ms 4992 KB Output is correct
18 Correct 3 ms 4992 KB Output is correct
19 Correct 269 ms 10616 KB Output is correct
20 Correct 260 ms 10488 KB Output is correct
21 Correct 285 ms 10620 KB Output is correct
22 Correct 305 ms 10868 KB Output is correct
23 Correct 126 ms 10620 KB Output is correct
24 Correct 108 ms 10616 KB Output is correct
25 Correct 185 ms 13176 KB Output is correct
26 Correct 149 ms 16756 KB Output is correct
27 Correct 283 ms 15864 KB Output is correct
28 Correct 366 ms 27128 KB Output is correct
29 Correct 370 ms 26104 KB Output is correct
30 Correct 279 ms 15864 KB Output is correct
31 Correct 278 ms 15992 KB Output is correct
32 Correct 319 ms 15992 KB Output is correct
33 Correct 394 ms 14840 KB Output is correct
34 Correct 263 ms 14840 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 4992 KB Output is correct
2 Correct 3 ms 5120 KB Output is correct
3 Correct 3 ms 4992 KB Output is correct
4 Correct 3 ms 4992 KB Output is correct
5 Correct 3 ms 4992 KB Output is correct
6 Correct 3 ms 4992 KB Output is correct
7 Correct 3 ms 4992 KB Output is correct
8 Correct 4 ms 5120 KB Output is correct
9 Correct 3 ms 4992 KB Output is correct
10 Correct 3 ms 5120 KB Output is correct
11 Correct 3 ms 4992 KB Output is correct
12 Correct 4 ms 4992 KB Output is correct
13 Correct 4 ms 4992 KB Output is correct
14 Correct 3 ms 4992 KB Output is correct
15 Correct 4 ms 5120 KB Output is correct
16 Correct 4 ms 4992 KB Output is correct
17 Correct 4 ms 4992 KB Output is correct
18 Correct 3 ms 4992 KB Output is correct
19 Correct 4 ms 4992 KB Output is correct
20 Correct 4 ms 4992 KB Output is correct
21 Correct 5 ms 5120 KB Output is correct
22 Correct 4 ms 5120 KB Output is correct
23 Correct 4 ms 5120 KB Output is correct
24 Correct 4 ms 5120 KB Output is correct
25 Correct 5 ms 5120 KB Output is correct
26 Correct 4 ms 5120 KB Output is correct
27 Correct 6 ms 5248 KB Output is correct
28 Correct 5 ms 5120 KB Output is correct
29 Correct 5 ms 5120 KB Output is correct
30 Correct 5 ms 5120 KB Output is correct
31 Correct 5 ms 5120 KB Output is correct
32 Correct 5 ms 5120 KB Output is correct
33 Correct 6 ms 5120 KB Output is correct
34 Correct 4 ms 5120 KB Output is correct
35 Correct 5 ms 5120 KB Output is correct
36 Correct 5 ms 5120 KB Output is correct
37 Correct 5 ms 5120 KB Output is correct
38 Correct 5 ms 5120 KB Output is correct
39 Correct 269 ms 10616 KB Output is correct
40 Correct 260 ms 10488 KB Output is correct
41 Correct 285 ms 10620 KB Output is correct
42 Correct 305 ms 10868 KB Output is correct
43 Correct 126 ms 10620 KB Output is correct
44 Correct 108 ms 10616 KB Output is correct
45 Correct 185 ms 13176 KB Output is correct
46 Correct 149 ms 16756 KB Output is correct
47 Correct 283 ms 15864 KB Output is correct
48 Correct 366 ms 27128 KB Output is correct
49 Correct 370 ms 26104 KB Output is correct
50 Correct 279 ms 15864 KB Output is correct
51 Correct 278 ms 15992 KB Output is correct
52 Correct 319 ms 15992 KB Output is correct
53 Correct 394 ms 14840 KB Output is correct
54 Correct 263 ms 14840 KB Output is correct
55 Correct 25 ms 5760 KB Output is correct
56 Correct 21 ms 5632 KB Output is correct
57 Correct 187 ms 11128 KB Output is correct
58 Correct 65 ms 10604 KB Output is correct
59 Correct 357 ms 20092 KB Output is correct
60 Correct 1159 ms 36076 KB Output is correct
61 Correct 367 ms 16248 KB Output is correct
62 Correct 399 ms 16120 KB Output is correct
63 Correct 475 ms 15992 KB Output is correct
64 Correct 1351 ms 20972 KB Output is correct
65 Correct 303 ms 15864 KB Output is correct
66 Correct 731 ms 24312 KB Output is correct
67 Correct 187 ms 16232 KB Output is correct
68 Correct 562 ms 19656 KB Output is correct
69 Correct 565 ms 19632 KB Output is correct
70 Correct 521 ms 19064 KB Output is correct