#include <bits/stdc++.h>
#include "race.h"
using namespace std;
int subSize(int st, vector<pair<int,int>>(&g)[], int sz[], int par, bool rem[]){
sz[st]=1;
for(pair<int,int> p : g[st]){
int c = p.first;
if(rem[c]||c==par) continue;
sz[st]+=subSize(c,g,sz,st,rem);
}
return sz[st];
}
int findCentroid(int node, vector<pair<int,int>>(&g)[], int siz, bool rem[], int par, int sizes[]){
for(pair<int,int> p : g[node]){
int c = p.first;
if(c==par||rem[c]) continue;
if(sizes[c]*2>siz){
return findCentroid(c,g,siz,rem,node,sizes);
}
}
return node;
}
void findLens(int st, vector<pair<int,int>>(&g)[], bool rem[], int par, unordered_map<int,int>(&mp), int dep, int len){
for(pair<int,int> p : g[st]){
int c = p.first;
int d = p.second;
if(c==par||rem[c]) continue;
if(mp[dep+d]==0){
mp[dep+d]=len+1;
}
else{
mp[dep+d]=min(mp[dep+d],len+1);
}
findLens(c,g,rem,st,mp,dep+d,len+1);
}
}
int buildDecomp(int node, vector<pair<int,int>>(&g)[], bool rem[], int sz[], int k){
int centroid = findCentroid(node,g,subSize(node,g,sz,-1,rem),rem,-1,sz);
int ans = 2e9;
unordered_map<int,int>mp;
rem[centroid]=1;
for(pair<int,int> p : g[centroid]){
int c = p.first;
if(rem[c]) continue;
unordered_map<int,int>temp;
temp[p.second]=1;
findLens(c,g,rem,centroid,temp,p.second,1);
for(pair<int,int>pa : temp){
if(mp[k-pa.first]){
ans=min(ans,mp[k-pa.first]+pa.second);
}
}
for(pair<int,int>pa:temp){
if(mp[pa.first]){
mp[pa.first]=min(mp[pa.first],pa.second);
}
else{
mp[pa.first]=pa.second;
}
}
}
if(mp[k]){
ans=min(ans,mp[k]);
}
for(pair<int,int>p:g[centroid]){
int c = p.first;
if(rem[c])continue;
ans=min(ans,buildDecomp(c,g,rem,sz,k));
}
return ans;
}
int best_path(int n, int k, int e[][2], int w[])
{
vector<pair<int,int>>g[n];
for(int i = 0;i<n-1;i++){
g[e[i][0]].push_back({e[i][1],w[i]});
g[e[i][1]].push_back({e[i][0],w[i]});
}
bool rem[n];
int sz[n];
fill(rem,rem+n,0);
int ans = buildDecomp(0,g,rem,sz,k);
if(ans==2e9){
return -1;
}
else{
return ans;
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
344 KB |
Output is correct |
3 |
Correct |
1 ms |
344 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
1 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
604 KB |
Output is correct |
17 |
Correct |
1 ms |
348 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
344 KB |
Output is correct |
3 |
Correct |
1 ms |
344 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
1 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
604 KB |
Output is correct |
17 |
Correct |
1 ms |
348 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
0 ms |
344 KB |
Output is correct |
20 |
Correct |
0 ms |
348 KB |
Output is correct |
21 |
Correct |
1 ms |
348 KB |
Output is correct |
22 |
Correct |
2 ms |
604 KB |
Output is correct |
23 |
Correct |
2 ms |
604 KB |
Output is correct |
24 |
Correct |
2 ms |
604 KB |
Output is correct |
25 |
Correct |
2 ms |
604 KB |
Output is correct |
26 |
Correct |
2 ms |
604 KB |
Output is correct |
27 |
Correct |
1 ms |
348 KB |
Output is correct |
28 |
Correct |
2 ms |
604 KB |
Output is correct |
29 |
Correct |
2 ms |
604 KB |
Output is correct |
30 |
Correct |
2 ms |
608 KB |
Output is correct |
31 |
Correct |
2 ms |
680 KB |
Output is correct |
32 |
Correct |
2 ms |
604 KB |
Output is correct |
33 |
Correct |
4 ms |
604 KB |
Output is correct |
34 |
Correct |
2 ms |
604 KB |
Output is correct |
35 |
Correct |
2 ms |
604 KB |
Output is correct |
36 |
Correct |
2 ms |
604 KB |
Output is correct |
37 |
Correct |
2 ms |
604 KB |
Output is correct |
38 |
Correct |
2 ms |
604 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
344 KB |
Output is correct |
3 |
Correct |
1 ms |
344 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
1 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
604 KB |
Output is correct |
17 |
Correct |
1 ms |
348 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
140 ms |
9416 KB |
Output is correct |
20 |
Correct |
155 ms |
9564 KB |
Output is correct |
21 |
Correct |
134 ms |
9532 KB |
Output is correct |
22 |
Correct |
122 ms |
9560 KB |
Output is correct |
23 |
Correct |
203 ms |
10068 KB |
Output is correct |
24 |
Correct |
128 ms |
9716 KB |
Output is correct |
25 |
Correct |
134 ms |
16784 KB |
Output is correct |
26 |
Correct |
84 ms |
16748 KB |
Output is correct |
27 |
Correct |
255 ms |
19184 KB |
Output is correct |
28 |
Correct |
1079 ms |
69240 KB |
Output is correct |
29 |
Correct |
1058 ms |
67956 KB |
Output is correct |
30 |
Correct |
325 ms |
19540 KB |
Output is correct |
31 |
Correct |
309 ms |
19124 KB |
Output is correct |
32 |
Correct |
292 ms |
19420 KB |
Output is correct |
33 |
Correct |
397 ms |
18624 KB |
Output is correct |
34 |
Correct |
857 ms |
48672 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
344 KB |
Output is correct |
3 |
Correct |
1 ms |
344 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
1 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
604 KB |
Output is correct |
17 |
Correct |
1 ms |
348 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
0 ms |
344 KB |
Output is correct |
20 |
Correct |
0 ms |
348 KB |
Output is correct |
21 |
Correct |
1 ms |
348 KB |
Output is correct |
22 |
Correct |
2 ms |
604 KB |
Output is correct |
23 |
Correct |
2 ms |
604 KB |
Output is correct |
24 |
Correct |
2 ms |
604 KB |
Output is correct |
25 |
Correct |
2 ms |
604 KB |
Output is correct |
26 |
Correct |
2 ms |
604 KB |
Output is correct |
27 |
Correct |
1 ms |
348 KB |
Output is correct |
28 |
Correct |
2 ms |
604 KB |
Output is correct |
29 |
Correct |
2 ms |
604 KB |
Output is correct |
30 |
Correct |
2 ms |
608 KB |
Output is correct |
31 |
Correct |
2 ms |
680 KB |
Output is correct |
32 |
Correct |
2 ms |
604 KB |
Output is correct |
33 |
Correct |
4 ms |
604 KB |
Output is correct |
34 |
Correct |
2 ms |
604 KB |
Output is correct |
35 |
Correct |
2 ms |
604 KB |
Output is correct |
36 |
Correct |
2 ms |
604 KB |
Output is correct |
37 |
Correct |
2 ms |
604 KB |
Output is correct |
38 |
Correct |
2 ms |
604 KB |
Output is correct |
39 |
Correct |
140 ms |
9416 KB |
Output is correct |
40 |
Correct |
155 ms |
9564 KB |
Output is correct |
41 |
Correct |
134 ms |
9532 KB |
Output is correct |
42 |
Correct |
122 ms |
9560 KB |
Output is correct |
43 |
Correct |
203 ms |
10068 KB |
Output is correct |
44 |
Correct |
128 ms |
9716 KB |
Output is correct |
45 |
Correct |
134 ms |
16784 KB |
Output is correct |
46 |
Correct |
84 ms |
16748 KB |
Output is correct |
47 |
Correct |
255 ms |
19184 KB |
Output is correct |
48 |
Correct |
1079 ms |
69240 KB |
Output is correct |
49 |
Correct |
1058 ms |
67956 KB |
Output is correct |
50 |
Correct |
325 ms |
19540 KB |
Output is correct |
51 |
Correct |
309 ms |
19124 KB |
Output is correct |
52 |
Correct |
292 ms |
19420 KB |
Output is correct |
53 |
Correct |
397 ms |
18624 KB |
Output is correct |
54 |
Correct |
857 ms |
48672 KB |
Output is correct |
55 |
Correct |
22 ms |
2136 KB |
Output is correct |
56 |
Correct |
16 ms |
1360 KB |
Output is correct |
57 |
Correct |
123 ms |
9564 KB |
Output is correct |
58 |
Correct |
37 ms |
9424 KB |
Output is correct |
59 |
Correct |
322 ms |
28836 KB |
Output is correct |
60 |
Correct |
1020 ms |
62908 KB |
Output is correct |
61 |
Correct |
310 ms |
21468 KB |
Output is correct |
62 |
Correct |
315 ms |
19352 KB |
Output is correct |
63 |
Correct |
308 ms |
19284 KB |
Output is correct |
64 |
Correct |
685 ms |
39264 KB |
Output is correct |
65 |
Correct |
701 ms |
51252 KB |
Output is correct |
66 |
Correct |
1112 ms |
67964 KB |
Output is correct |
67 |
Correct |
159 ms |
19652 KB |
Output is correct |
68 |
Correct |
443 ms |
39476 KB |
Output is correct |
69 |
Correct |
491 ms |
40040 KB |
Output is correct |
70 |
Correct |
417 ms |
38076 KB |
Output is correct |