#include "race.h"
#include <bits/stdc++.h>
using namespace std;
#define len(a) ((int)a.size())
const int N = 200005;
int ans = -1;
vector< pair<int, int> > g[N];
int sz[N], all, k;
bool used[N];
void update(int nans){
if(ans == -1 || nans < ans){
ans = nans;
}
}
void sizes(int v, int p){
sz[v] = 1;
for(int i = 0; i < len(g[v]); i++){
int to = g[v][i].first;
if(to == p || used[to]) continue;
sizes(to, v);
sz[v] += sz[to];
}
}
int cfind(int v, int p){
if(p == -1){
all = sz[v];
}
for(int i = 0; i < len(g[v]); i++){
int to = g[v][i].first;
if(to == p || used[to]) continue;
if(sz[to] > (all / 2)) {
return cfind(to, v);
}
}
return v;
}
vector< pair<int, int> > mas;
map<int, int> mp;
void solve(int v, int p, int d, int l){
if(p != -1){
mas.push_back(make_pair(d, l));
}
if(l == k) update(d);
if(l < k && mp.find(k - l) != mp.end()) update(mp[k - l] + d);
for(int i = 0; i < len(g[v]); i++){
int to = g[v][i].first, len = g[v][i].second;
if(to == p || used[to]) continue;
solve(to, v, d + 1, l + len);
if(p == -1){
for(int j = 0; j < len(mas); j++){
int dep = mas[j].first, dis = mas[j].second;
if(dis > k) continue;
if(mp.find(dis) != mp.end()){
mp[dis] = min(mp[dis], dep);
}
else {
mp[dis] = dep;
}
}
mas.clear();
}
}
}
void centroid_decomposition(){
queue<int> q;
q.push(1);
while(!q.empty()){
int v = q.front();
q.pop();
sizes(v, -1);
v = cfind(v, -1);
sizes(v, -1);
mp.clear();
solve(v, -1, 0, 0);
used[v] = true;
for(int i = 0; i < len(g[v]); i++){
int to = g[v][i].first;
if(used[to]) continue;
if(sz[to] == 1) continue;
q.push(to);
}
}
}
int best_path(int N, int K, int H[][2], int L[]) {
k = K;
for(int i = 0; i < N - 1; i++){
int u = H[i][0], v = H[i][1], d = L[i];
g[u].push_back(make_pair(v, d));
g[v].push_back(make_pair(u, d));
}
centroid_decomposition();
return ans;
}
//int main(){
// int L[] = {3, 4, 5, 4, 6, 3, 2, 5, 6, 7};
// int H[][2] = {{0, 1}, {0, 2}, {2, 3}, {3, 4}, {4, 5}, {0, 6}, {6, 7}, {6, 8}, {8, 9}, {8, 10}};
//
// cout << best_path(11, 12, H, L) << endl;
// return 0;
//}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4940 KB |
Output is correct |
2 |
Correct |
3 ms |
4964 KB |
Output is correct |
3 |
Correct |
3 ms |
4940 KB |
Output is correct |
4 |
Correct |
3 ms |
4908 KB |
Output is correct |
5 |
Correct |
3 ms |
4940 KB |
Output is correct |
6 |
Correct |
3 ms |
4940 KB |
Output is correct |
7 |
Correct |
3 ms |
4940 KB |
Output is correct |
8 |
Correct |
3 ms |
4940 KB |
Output is correct |
9 |
Correct |
3 ms |
4940 KB |
Output is correct |
10 |
Correct |
3 ms |
4940 KB |
Output is correct |
11 |
Correct |
3 ms |
4940 KB |
Output is correct |
12 |
Correct |
3 ms |
4940 KB |
Output is correct |
13 |
Correct |
3 ms |
4940 KB |
Output is correct |
14 |
Correct |
3 ms |
4940 KB |
Output is correct |
15 |
Correct |
3 ms |
4940 KB |
Output is correct |
16 |
Correct |
3 ms |
4940 KB |
Output is correct |
17 |
Correct |
3 ms |
4940 KB |
Output is correct |
18 |
Correct |
3 ms |
4940 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4940 KB |
Output is correct |
2 |
Correct |
3 ms |
4964 KB |
Output is correct |
3 |
Correct |
3 ms |
4940 KB |
Output is correct |
4 |
Correct |
3 ms |
4908 KB |
Output is correct |
5 |
Correct |
3 ms |
4940 KB |
Output is correct |
6 |
Correct |
3 ms |
4940 KB |
Output is correct |
7 |
Correct |
3 ms |
4940 KB |
Output is correct |
8 |
Correct |
3 ms |
4940 KB |
Output is correct |
9 |
Correct |
3 ms |
4940 KB |
Output is correct |
10 |
Correct |
3 ms |
4940 KB |
Output is correct |
11 |
Correct |
3 ms |
4940 KB |
Output is correct |
12 |
Correct |
3 ms |
4940 KB |
Output is correct |
13 |
Correct |
3 ms |
4940 KB |
Output is correct |
14 |
Correct |
3 ms |
4940 KB |
Output is correct |
15 |
Correct |
3 ms |
4940 KB |
Output is correct |
16 |
Correct |
3 ms |
4940 KB |
Output is correct |
17 |
Correct |
3 ms |
4940 KB |
Output is correct |
18 |
Correct |
3 ms |
4940 KB |
Output is correct |
19 |
Correct |
3 ms |
4940 KB |
Output is correct |
20 |
Correct |
3 ms |
4940 KB |
Output is correct |
21 |
Correct |
4 ms |
5068 KB |
Output is correct |
22 |
Correct |
4 ms |
5068 KB |
Output is correct |
23 |
Correct |
4 ms |
5068 KB |
Output is correct |
24 |
Correct |
4 ms |
5068 KB |
Output is correct |
25 |
Correct |
4 ms |
5068 KB |
Output is correct |
26 |
Correct |
4 ms |
5068 KB |
Output is correct |
27 |
Correct |
4 ms |
5068 KB |
Output is correct |
28 |
Correct |
5 ms |
5068 KB |
Output is correct |
29 |
Correct |
7 ms |
5068 KB |
Output is correct |
30 |
Correct |
4 ms |
5068 KB |
Output is correct |
31 |
Correct |
5 ms |
5068 KB |
Output is correct |
32 |
Correct |
4 ms |
5068 KB |
Output is correct |
33 |
Correct |
4 ms |
5068 KB |
Output is correct |
34 |
Correct |
4 ms |
5068 KB |
Output is correct |
35 |
Correct |
4 ms |
5068 KB |
Output is correct |
36 |
Correct |
4 ms |
5068 KB |
Output is correct |
37 |
Correct |
4 ms |
5068 KB |
Output is correct |
38 |
Correct |
6 ms |
5068 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4940 KB |
Output is correct |
2 |
Correct |
3 ms |
4964 KB |
Output is correct |
3 |
Correct |
3 ms |
4940 KB |
Output is correct |
4 |
Correct |
3 ms |
4908 KB |
Output is correct |
5 |
Correct |
3 ms |
4940 KB |
Output is correct |
6 |
Correct |
3 ms |
4940 KB |
Output is correct |
7 |
Correct |
3 ms |
4940 KB |
Output is correct |
8 |
Correct |
3 ms |
4940 KB |
Output is correct |
9 |
Correct |
3 ms |
4940 KB |
Output is correct |
10 |
Correct |
3 ms |
4940 KB |
Output is correct |
11 |
Correct |
3 ms |
4940 KB |
Output is correct |
12 |
Correct |
3 ms |
4940 KB |
Output is correct |
13 |
Correct |
3 ms |
4940 KB |
Output is correct |
14 |
Correct |
3 ms |
4940 KB |
Output is correct |
15 |
Correct |
3 ms |
4940 KB |
Output is correct |
16 |
Correct |
3 ms |
4940 KB |
Output is correct |
17 |
Correct |
3 ms |
4940 KB |
Output is correct |
18 |
Correct |
3 ms |
4940 KB |
Output is correct |
19 |
Correct |
287 ms |
10772 KB |
Output is correct |
20 |
Correct |
289 ms |
10816 KB |
Output is correct |
21 |
Correct |
298 ms |
10860 KB |
Output is correct |
22 |
Correct |
260 ms |
10944 KB |
Output is correct |
23 |
Correct |
155 ms |
11116 KB |
Output is correct |
24 |
Correct |
95 ms |
10876 KB |
Output is correct |
25 |
Correct |
206 ms |
17352 KB |
Output is correct |
26 |
Correct |
135 ms |
18148 KB |
Output is correct |
27 |
Correct |
349 ms |
15920 KB |
Output is correct |
28 |
Correct |
538 ms |
30100 KB |
Output is correct |
29 |
Correct |
576 ms |
28116 KB |
Output is correct |
30 |
Correct |
297 ms |
15940 KB |
Output is correct |
31 |
Correct |
303 ms |
16040 KB |
Output is correct |
32 |
Correct |
414 ms |
15940 KB |
Output is correct |
33 |
Correct |
481 ms |
15728 KB |
Output is correct |
34 |
Correct |
383 ms |
15656 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4940 KB |
Output is correct |
2 |
Correct |
3 ms |
4964 KB |
Output is correct |
3 |
Correct |
3 ms |
4940 KB |
Output is correct |
4 |
Correct |
3 ms |
4908 KB |
Output is correct |
5 |
Correct |
3 ms |
4940 KB |
Output is correct |
6 |
Correct |
3 ms |
4940 KB |
Output is correct |
7 |
Correct |
3 ms |
4940 KB |
Output is correct |
8 |
Correct |
3 ms |
4940 KB |
Output is correct |
9 |
Correct |
3 ms |
4940 KB |
Output is correct |
10 |
Correct |
3 ms |
4940 KB |
Output is correct |
11 |
Correct |
3 ms |
4940 KB |
Output is correct |
12 |
Correct |
3 ms |
4940 KB |
Output is correct |
13 |
Correct |
3 ms |
4940 KB |
Output is correct |
14 |
Correct |
3 ms |
4940 KB |
Output is correct |
15 |
Correct |
3 ms |
4940 KB |
Output is correct |
16 |
Correct |
3 ms |
4940 KB |
Output is correct |
17 |
Correct |
3 ms |
4940 KB |
Output is correct |
18 |
Correct |
3 ms |
4940 KB |
Output is correct |
19 |
Correct |
3 ms |
4940 KB |
Output is correct |
20 |
Correct |
3 ms |
4940 KB |
Output is correct |
21 |
Correct |
4 ms |
5068 KB |
Output is correct |
22 |
Correct |
4 ms |
5068 KB |
Output is correct |
23 |
Correct |
4 ms |
5068 KB |
Output is correct |
24 |
Correct |
4 ms |
5068 KB |
Output is correct |
25 |
Correct |
4 ms |
5068 KB |
Output is correct |
26 |
Correct |
4 ms |
5068 KB |
Output is correct |
27 |
Correct |
4 ms |
5068 KB |
Output is correct |
28 |
Correct |
5 ms |
5068 KB |
Output is correct |
29 |
Correct |
7 ms |
5068 KB |
Output is correct |
30 |
Correct |
4 ms |
5068 KB |
Output is correct |
31 |
Correct |
5 ms |
5068 KB |
Output is correct |
32 |
Correct |
4 ms |
5068 KB |
Output is correct |
33 |
Correct |
4 ms |
5068 KB |
Output is correct |
34 |
Correct |
4 ms |
5068 KB |
Output is correct |
35 |
Correct |
4 ms |
5068 KB |
Output is correct |
36 |
Correct |
4 ms |
5068 KB |
Output is correct |
37 |
Correct |
4 ms |
5068 KB |
Output is correct |
38 |
Correct |
6 ms |
5068 KB |
Output is correct |
39 |
Correct |
287 ms |
10772 KB |
Output is correct |
40 |
Correct |
289 ms |
10816 KB |
Output is correct |
41 |
Correct |
298 ms |
10860 KB |
Output is correct |
42 |
Correct |
260 ms |
10944 KB |
Output is correct |
43 |
Correct |
155 ms |
11116 KB |
Output is correct |
44 |
Correct |
95 ms |
10876 KB |
Output is correct |
45 |
Correct |
206 ms |
17352 KB |
Output is correct |
46 |
Correct |
135 ms |
18148 KB |
Output is correct |
47 |
Correct |
349 ms |
15920 KB |
Output is correct |
48 |
Correct |
538 ms |
30100 KB |
Output is correct |
49 |
Correct |
576 ms |
28116 KB |
Output is correct |
50 |
Correct |
297 ms |
15940 KB |
Output is correct |
51 |
Correct |
303 ms |
16040 KB |
Output is correct |
52 |
Correct |
414 ms |
15940 KB |
Output is correct |
53 |
Correct |
481 ms |
15728 KB |
Output is correct |
54 |
Correct |
383 ms |
15656 KB |
Output is correct |
55 |
Correct |
22 ms |
5708 KB |
Output is correct |
56 |
Correct |
23 ms |
5636 KB |
Output is correct |
57 |
Correct |
160 ms |
11024 KB |
Output is correct |
58 |
Correct |
49 ms |
10368 KB |
Output is correct |
59 |
Correct |
314 ms |
21444 KB |
Output is correct |
60 |
Correct |
1088 ms |
36496 KB |
Output is correct |
61 |
Correct |
361 ms |
16148 KB |
Output is correct |
62 |
Correct |
355 ms |
15984 KB |
Output is correct |
63 |
Correct |
512 ms |
16196 KB |
Output is correct |
64 |
Correct |
1258 ms |
20936 KB |
Output is correct |
65 |
Correct |
544 ms |
16948 KB |
Output is correct |
66 |
Correct |
875 ms |
28724 KB |
Output is correct |
67 |
Correct |
138 ms |
16176 KB |
Output is correct |
68 |
Correct |
542 ms |
20484 KB |
Output is correct |
69 |
Correct |
559 ms |
20384 KB |
Output is correct |
70 |
Correct |
538 ms |
19680 KB |
Output is correct |