Submission #1064210

# Submission time Handle Problem Language Result Execution time Memory
1064210 2024-08-18T10:26:14 Z kkkkkkkk Race (IOI11_race) C++17
100 / 100
657 ms 41696 KB
#include <bits/stdc++.h>

using namespace std;

vector<pair<int,int> > G[200005];
int is_removed[200005], subtree_size[200005];
map<int,int> cekori;
int k;
int rez;

void get_subtree_size(int teme, int parent) {
	subtree_size[teme]=1;
	for (auto [next, dist]:G[teme]){
		if (next==parent||is_removed[next]) continue;
		get_subtree_size(next, teme);
		subtree_size[teme]+=subtree_size[next];
	}
}

int find_centroid(int teme, int parent, int nodes){
	for (auto [next, dist]:G[teme]){
		if (next==parent||is_removed[next]) continue;
		if (subtree_size[next]>=nodes/2)
            return find_centroid(next, teme, nodes);
	}
	return teme;
}

void check(int teme, int parent, int dolz, int depth){
	if (dolz>k) return ;
	if (cekori.find(k-dolz)!=cekori.end())
        rez=min(rez, cekori[k-dolz]+depth);
	for (auto [next, dist]:G[teme]){
		if (next==parent||is_removed[next]) continue;
		check(next, teme, dolz+dist, depth+1);
	}
}

void build(int teme, int parent, int dolz, int depth){
	if (dolz>k) return ;
	if (cekori.find(dolz)==cekori.end())
        cekori[dolz]=depth;
	else cekori[dolz]=min(cekori[dolz], depth);
	for (auto [next, dist]:G[teme]){
		if (next==parent||is_removed[next]) continue;
		build(next, teme, dolz+dist, depth+1);
	}
}

void build_centroid_decomp(int teme) {
	get_subtree_size(teme, -1);
	int centroid=find_centroid(teme, -1, subtree_size[teme]);
	cekori[0]=0;
	for (auto [next, dist]:G[centroid]) {
		if (is_removed[next]) continue;
		check(next, centroid, dist, 1);
		build(next, centroid, dist, 1);
	}
	cekori.clear();
	is_removed[centroid]=1;
	for (auto [next, dist]:G[centroid]) {
		if (is_removed[next]) continue;
		build_centroid_decomp(next);
	}
}

int best_path(int N, int K, int H[][2], int L[]) {
	k=K;
	for (int i=0;i<N-1;i++) {
		G[H[i][0]].push_back({H[i][1], L[i]});
		G[H[i][1]].push_back({H[i][0], L[i]});
	}
	rez=INT_MAX;
	build_centroid_decomp(0);
	if (rez==INT_MAX) rez=-1;
	return rez;
}
# Verdict Execution time Memory Grader output
1 Correct 2 ms 4952 KB Output is correct
2 Correct 2 ms 4956 KB Output is correct
3 Correct 2 ms 4956 KB Output is correct
4 Correct 2 ms 4956 KB Output is correct
5 Correct 2 ms 4956 KB Output is correct
6 Correct 2 ms 4956 KB Output is correct
7 Correct 2 ms 4956 KB Output is correct
8 Correct 2 ms 4956 KB Output is correct
9 Correct 2 ms 4956 KB Output is correct
10 Correct 2 ms 4952 KB Output is correct
11 Correct 4 ms 4952 KB Output is correct
12 Correct 2 ms 4956 KB Output is correct
13 Correct 2 ms 4956 KB Output is correct
14 Correct 2 ms 5096 KB Output is correct
15 Correct 2 ms 4956 KB Output is correct
16 Correct 2 ms 4956 KB Output is correct
17 Correct 2 ms 4956 KB Output is correct
18 Correct 2 ms 4956 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 4952 KB Output is correct
2 Correct 2 ms 4956 KB Output is correct
3 Correct 2 ms 4956 KB Output is correct
4 Correct 2 ms 4956 KB Output is correct
5 Correct 2 ms 4956 KB Output is correct
6 Correct 2 ms 4956 KB Output is correct
7 Correct 2 ms 4956 KB Output is correct
8 Correct 2 ms 4956 KB Output is correct
9 Correct 2 ms 4956 KB Output is correct
10 Correct 2 ms 4952 KB Output is correct
11 Correct 4 ms 4952 KB Output is correct
12 Correct 2 ms 4956 KB Output is correct
13 Correct 2 ms 4956 KB Output is correct
14 Correct 2 ms 5096 KB Output is correct
15 Correct 2 ms 4956 KB Output is correct
16 Correct 2 ms 4956 KB Output is correct
17 Correct 2 ms 4956 KB Output is correct
18 Correct 2 ms 4956 KB Output is correct
19 Correct 2 ms 4956 KB Output is correct
20 Correct 2 ms 4956 KB Output is correct
21 Correct 3 ms 5212 KB Output is correct
22 Correct 4 ms 5212 KB Output is correct
23 Correct 3 ms 5208 KB Output is correct
24 Correct 3 ms 5208 KB Output is correct
25 Correct 3 ms 5212 KB Output is correct
26 Correct 2 ms 5212 KB Output is correct
27 Correct 2 ms 5212 KB Output is correct
28 Correct 3 ms 5244 KB Output is correct
29 Correct 3 ms 5212 KB Output is correct
30 Correct 3 ms 5212 KB Output is correct
31 Correct 4 ms 5212 KB Output is correct
32 Correct 3 ms 5212 KB Output is correct
33 Correct 3 ms 5212 KB Output is correct
34 Correct 3 ms 5212 KB Output is correct
35 Correct 3 ms 5212 KB Output is correct
36 Correct 3 ms 5208 KB Output is correct
37 Correct 3 ms 5212 KB Output is correct
38 Correct 3 ms 5212 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 4952 KB Output is correct
2 Correct 2 ms 4956 KB Output is correct
3 Correct 2 ms 4956 KB Output is correct
4 Correct 2 ms 4956 KB Output is correct
5 Correct 2 ms 4956 KB Output is correct
6 Correct 2 ms 4956 KB Output is correct
7 Correct 2 ms 4956 KB Output is correct
8 Correct 2 ms 4956 KB Output is correct
9 Correct 2 ms 4956 KB Output is correct
10 Correct 2 ms 4952 KB Output is correct
11 Correct 4 ms 4952 KB Output is correct
12 Correct 2 ms 4956 KB Output is correct
13 Correct 2 ms 4956 KB Output is correct
14 Correct 2 ms 5096 KB Output is correct
15 Correct 2 ms 4956 KB Output is correct
16 Correct 2 ms 4956 KB Output is correct
17 Correct 2 ms 4956 KB Output is correct
18 Correct 2 ms 4956 KB Output is correct
19 Correct 131 ms 10584 KB Output is correct
20 Correct 128 ms 10584 KB Output is correct
21 Correct 143 ms 10580 KB Output is correct
22 Correct 132 ms 10836 KB Output is correct
23 Correct 48 ms 10836 KB Output is correct
24 Correct 48 ms 12332 KB Output is correct
25 Correct 95 ms 15700 KB Output is correct
26 Correct 71 ms 19280 KB Output is correct
27 Correct 127 ms 19284 KB Output is correct
28 Correct 169 ms 34212 KB Output is correct
29 Correct 169 ms 32660 KB Output is correct
30 Correct 129 ms 19476 KB Output is correct
31 Correct 135 ms 19500 KB Output is correct
32 Correct 133 ms 19536 KB Output is correct
33 Correct 171 ms 18148 KB Output is correct
34 Correct 123 ms 19244 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 4952 KB Output is correct
2 Correct 2 ms 4956 KB Output is correct
3 Correct 2 ms 4956 KB Output is correct
4 Correct 2 ms 4956 KB Output is correct
5 Correct 2 ms 4956 KB Output is correct
6 Correct 2 ms 4956 KB Output is correct
7 Correct 2 ms 4956 KB Output is correct
8 Correct 2 ms 4956 KB Output is correct
9 Correct 2 ms 4956 KB Output is correct
10 Correct 2 ms 4952 KB Output is correct
11 Correct 4 ms 4952 KB Output is correct
12 Correct 2 ms 4956 KB Output is correct
13 Correct 2 ms 4956 KB Output is correct
14 Correct 2 ms 5096 KB Output is correct
15 Correct 2 ms 4956 KB Output is correct
16 Correct 2 ms 4956 KB Output is correct
17 Correct 2 ms 4956 KB Output is correct
18 Correct 2 ms 4956 KB Output is correct
19 Correct 2 ms 4956 KB Output is correct
20 Correct 2 ms 4956 KB Output is correct
21 Correct 3 ms 5212 KB Output is correct
22 Correct 4 ms 5212 KB Output is correct
23 Correct 3 ms 5208 KB Output is correct
24 Correct 3 ms 5208 KB Output is correct
25 Correct 3 ms 5212 KB Output is correct
26 Correct 2 ms 5212 KB Output is correct
27 Correct 2 ms 5212 KB Output is correct
28 Correct 3 ms 5244 KB Output is correct
29 Correct 3 ms 5212 KB Output is correct
30 Correct 3 ms 5212 KB Output is correct
31 Correct 4 ms 5212 KB Output is correct
32 Correct 3 ms 5212 KB Output is correct
33 Correct 3 ms 5212 KB Output is correct
34 Correct 3 ms 5212 KB Output is correct
35 Correct 3 ms 5212 KB Output is correct
36 Correct 3 ms 5208 KB Output is correct
37 Correct 3 ms 5212 KB Output is correct
38 Correct 3 ms 5212 KB Output is correct
39 Correct 131 ms 10584 KB Output is correct
40 Correct 128 ms 10584 KB Output is correct
41 Correct 143 ms 10580 KB Output is correct
42 Correct 132 ms 10836 KB Output is correct
43 Correct 48 ms 10836 KB Output is correct
44 Correct 48 ms 12332 KB Output is correct
45 Correct 95 ms 15700 KB Output is correct
46 Correct 71 ms 19280 KB Output is correct
47 Correct 127 ms 19284 KB Output is correct
48 Correct 169 ms 34212 KB Output is correct
49 Correct 169 ms 32660 KB Output is correct
50 Correct 129 ms 19476 KB Output is correct
51 Correct 135 ms 19500 KB Output is correct
52 Correct 133 ms 19536 KB Output is correct
53 Correct 171 ms 18148 KB Output is correct
54 Correct 123 ms 19244 KB Output is correct
55 Correct 15 ms 5976 KB Output is correct
56 Correct 11 ms 5724 KB Output is correct
57 Correct 81 ms 12336 KB Output is correct
58 Correct 33 ms 11976 KB Output is correct
59 Correct 212 ms 22744 KB Output is correct
60 Correct 596 ms 41696 KB Output is correct
61 Correct 185 ms 19776 KB Output is correct
62 Correct 191 ms 19540 KB Output is correct
63 Correct 213 ms 19716 KB Output is correct
64 Correct 657 ms 24288 KB Output is correct
65 Correct 122 ms 20248 KB Output is correct
66 Correct 394 ms 30400 KB Output is correct
67 Correct 85 ms 20168 KB Output is correct
68 Correct 262 ms 23912 KB Output is correct
69 Correct 295 ms 23724 KB Output is correct
70 Correct 252 ms 23124 KB Output is correct