#include <iostream>
#include <cstdio>
#include <iomanip>
#include <chrono>
#include <vector>
using namespace std;
const int maxn=2e5+7;
const int maxk=1e6+7;
const int inf=1e9+7;
struct Edges{
int x,y,w;
};
int n,k,currentLab,res;
int subSize[maxn],lab[maxn],d[maxn],lv[maxn],f[maxk];
bool disabled[maxn];
vector<int> adj[maxn],tobeProcessed,depthList;
Edges e[maxn];
inline void CalcSize(const int &u){
lab[u]=currentLab;
subSize[u]=1;
for(int i: adj[u]){
int v=e[i].x+e[i].y-u;
if(disabled[v] || lab[v]==currentLab) continue;
CalcSize(v);
subSize[u]+=subSize[v];
}
}
inline int FindCentroid(int u){
int halfSize=subSize[u]/2;
while(true){
int heavyVertex=-1;
for(int i: adj[u]){
int v=e[i].x+e[i].y-u;
if(disabled[v] || subSize[v]>subSize[u]) continue;
if(subSize[v]>halfSize){
heavyVertex=v;
break;
}
}
if(heavyVertex==-1) break;
u=heavyVertex;
}
return u;
}
inline void CalcDepth(const int &u){
lab[u]=currentLab;
if(d[u]>k) return;
depthList.push_back(d[u]);
for(int i: adj[u]){
int v=e[i].x+e[i].y-u;
if(disabled[v] || lab[v]==currentLab) continue;
d[v]=d[u]+e[i].w;
lv[v]=lv[u]+1;
CalcDepth(v);
}
}
inline void Query(const int &u){
lab[u]=currentLab;
if(d[u]>k) return;
res=min(res,f[k-d[u]]+lv[u]);
for(int i: adj[u]){
int v=e[i].x+e[i].y-u;
if(disabled[v] || lab[v]==currentLab) continue;
Query(v);
}
}
inline void Update(const int &u){
lab[u]=currentLab;
if(d[u]>k) return;
f[d[u]]=min(f[d[u]],lv[u]);
for(int i: adj[u]){
int v=e[i].x+e[i].y-u;
if(disabled[v] || lab[v]==currentLab) continue;
Update(v);
}
}
inline void ProcessTree(int u){
currentLab++;
CalcSize(u);
u=FindCentroid(u);
disabled[u]=true;
for(int x: depthList) f[x]=inf;
depthList.resize(0);
lv[u]=d[u]=0;
currentLab++;
CalcDepth(u);
f[0]=0;
for(int i: adj[u]){
int v=e[i].x+e[i].y-u;
if(disabled[v]) continue;
currentLab++; Query(v);
currentLab++; Update(v);
tobeProcessed.push_back(v);
}
}
int best_path(int N, int K, int H[][2], int L[]){
tie(n,k)=tie(N,K);
for(int i=0;i<n-1;i++){
e[i]={H[i][0]+1,H[i][1]+1,L[i]};
adj[e[i].x].push_back(i);
adj[e[i].y].push_back(i);
}
fill(f,f+maxk+1,inf);
res=inf; tobeProcessed.push_back(1);
while(tobeProcessed.size()){
int u=tobeProcessed.back();
tobeProcessed.pop_back();
ProcessTree(u);
}
return (res==inf)? -1 : res;
}
Compilation message
race.cpp: In function 'int best_path(int, int, int (*)[2], int*)':
race.cpp:112:8: warning: array subscript is above array bounds [-Warray-bounds]
fill(f,f+maxk+1,inf);
~~~~^~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
10 ms |
8952 KB |
Output is correct |
2 |
Correct |
10 ms |
8952 KB |
Output is correct |
3 |
Correct |
10 ms |
8952 KB |
Output is correct |
4 |
Correct |
10 ms |
8952 KB |
Output is correct |
5 |
Correct |
10 ms |
8952 KB |
Output is correct |
6 |
Correct |
10 ms |
8952 KB |
Output is correct |
7 |
Correct |
10 ms |
8952 KB |
Output is correct |
8 |
Correct |
10 ms |
8952 KB |
Output is correct |
9 |
Correct |
10 ms |
8952 KB |
Output is correct |
10 |
Correct |
10 ms |
8952 KB |
Output is correct |
11 |
Correct |
10 ms |
8952 KB |
Output is correct |
12 |
Correct |
10 ms |
8952 KB |
Output is correct |
13 |
Correct |
10 ms |
8952 KB |
Output is correct |
14 |
Correct |
13 ms |
8952 KB |
Output is correct |
15 |
Correct |
12 ms |
8952 KB |
Output is correct |
16 |
Correct |
10 ms |
8952 KB |
Output is correct |
17 |
Correct |
10 ms |
8952 KB |
Output is correct |
18 |
Correct |
10 ms |
8924 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
10 ms |
8952 KB |
Output is correct |
2 |
Correct |
10 ms |
8952 KB |
Output is correct |
3 |
Correct |
10 ms |
8952 KB |
Output is correct |
4 |
Correct |
10 ms |
8952 KB |
Output is correct |
5 |
Correct |
10 ms |
8952 KB |
Output is correct |
6 |
Correct |
10 ms |
8952 KB |
Output is correct |
7 |
Correct |
10 ms |
8952 KB |
Output is correct |
8 |
Correct |
10 ms |
8952 KB |
Output is correct |
9 |
Correct |
10 ms |
8952 KB |
Output is correct |
10 |
Correct |
10 ms |
8952 KB |
Output is correct |
11 |
Correct |
10 ms |
8952 KB |
Output is correct |
12 |
Correct |
10 ms |
8952 KB |
Output is correct |
13 |
Correct |
10 ms |
8952 KB |
Output is correct |
14 |
Correct |
13 ms |
8952 KB |
Output is correct |
15 |
Correct |
12 ms |
8952 KB |
Output is correct |
16 |
Correct |
10 ms |
8952 KB |
Output is correct |
17 |
Correct |
10 ms |
8952 KB |
Output is correct |
18 |
Correct |
10 ms |
8924 KB |
Output is correct |
19 |
Correct |
10 ms |
9080 KB |
Output is correct |
20 |
Correct |
10 ms |
8952 KB |
Output is correct |
21 |
Correct |
11 ms |
9080 KB |
Output is correct |
22 |
Correct |
11 ms |
9080 KB |
Output is correct |
23 |
Correct |
11 ms |
9080 KB |
Output is correct |
24 |
Correct |
11 ms |
9080 KB |
Output is correct |
25 |
Correct |
11 ms |
9080 KB |
Output is correct |
26 |
Correct |
11 ms |
9080 KB |
Output is correct |
27 |
Correct |
11 ms |
9080 KB |
Output is correct |
28 |
Correct |
12 ms |
9088 KB |
Output is correct |
29 |
Correct |
11 ms |
9080 KB |
Output is correct |
30 |
Correct |
11 ms |
9080 KB |
Output is correct |
31 |
Correct |
11 ms |
9080 KB |
Output is correct |
32 |
Correct |
11 ms |
9148 KB |
Output is correct |
33 |
Correct |
14 ms |
9080 KB |
Output is correct |
34 |
Correct |
11 ms |
9080 KB |
Output is correct |
35 |
Correct |
12 ms |
9080 KB |
Output is correct |
36 |
Correct |
11 ms |
9084 KB |
Output is correct |
37 |
Correct |
11 ms |
9080 KB |
Output is correct |
38 |
Correct |
11 ms |
9080 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
10 ms |
8952 KB |
Output is correct |
2 |
Correct |
10 ms |
8952 KB |
Output is correct |
3 |
Correct |
10 ms |
8952 KB |
Output is correct |
4 |
Correct |
10 ms |
8952 KB |
Output is correct |
5 |
Correct |
10 ms |
8952 KB |
Output is correct |
6 |
Correct |
10 ms |
8952 KB |
Output is correct |
7 |
Correct |
10 ms |
8952 KB |
Output is correct |
8 |
Correct |
10 ms |
8952 KB |
Output is correct |
9 |
Correct |
10 ms |
8952 KB |
Output is correct |
10 |
Correct |
10 ms |
8952 KB |
Output is correct |
11 |
Correct |
10 ms |
8952 KB |
Output is correct |
12 |
Correct |
10 ms |
8952 KB |
Output is correct |
13 |
Correct |
10 ms |
8952 KB |
Output is correct |
14 |
Correct |
13 ms |
8952 KB |
Output is correct |
15 |
Correct |
12 ms |
8952 KB |
Output is correct |
16 |
Correct |
10 ms |
8952 KB |
Output is correct |
17 |
Correct |
10 ms |
8952 KB |
Output is correct |
18 |
Correct |
10 ms |
8924 KB |
Output is correct |
19 |
Correct |
298 ms |
17740 KB |
Output is correct |
20 |
Correct |
282 ms |
17628 KB |
Output is correct |
21 |
Correct |
347 ms |
17784 KB |
Output is correct |
22 |
Correct |
282 ms |
18168 KB |
Output is correct |
23 |
Correct |
157 ms |
17528 KB |
Output is correct |
24 |
Correct |
96 ms |
17784 KB |
Output is correct |
25 |
Correct |
270 ms |
19336 KB |
Output is correct |
26 |
Correct |
173 ms |
21356 KB |
Output is correct |
27 |
Correct |
352 ms |
26836 KB |
Output is correct |
28 |
Correct |
463 ms |
33092 KB |
Output is correct |
29 |
Correct |
470 ms |
32604 KB |
Output is correct |
30 |
Correct |
306 ms |
26936 KB |
Output is correct |
31 |
Correct |
303 ms |
26744 KB |
Output is correct |
32 |
Correct |
388 ms |
26896 KB |
Output is correct |
33 |
Correct |
480 ms |
26132 KB |
Output is correct |
34 |
Correct |
385 ms |
27012 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
10 ms |
8952 KB |
Output is correct |
2 |
Correct |
10 ms |
8952 KB |
Output is correct |
3 |
Correct |
10 ms |
8952 KB |
Output is correct |
4 |
Correct |
10 ms |
8952 KB |
Output is correct |
5 |
Correct |
10 ms |
8952 KB |
Output is correct |
6 |
Correct |
10 ms |
8952 KB |
Output is correct |
7 |
Correct |
10 ms |
8952 KB |
Output is correct |
8 |
Correct |
10 ms |
8952 KB |
Output is correct |
9 |
Correct |
10 ms |
8952 KB |
Output is correct |
10 |
Correct |
10 ms |
8952 KB |
Output is correct |
11 |
Correct |
10 ms |
8952 KB |
Output is correct |
12 |
Correct |
10 ms |
8952 KB |
Output is correct |
13 |
Correct |
10 ms |
8952 KB |
Output is correct |
14 |
Correct |
13 ms |
8952 KB |
Output is correct |
15 |
Correct |
12 ms |
8952 KB |
Output is correct |
16 |
Correct |
10 ms |
8952 KB |
Output is correct |
17 |
Correct |
10 ms |
8952 KB |
Output is correct |
18 |
Correct |
10 ms |
8924 KB |
Output is correct |
19 |
Correct |
10 ms |
9080 KB |
Output is correct |
20 |
Correct |
10 ms |
8952 KB |
Output is correct |
21 |
Correct |
11 ms |
9080 KB |
Output is correct |
22 |
Correct |
11 ms |
9080 KB |
Output is correct |
23 |
Correct |
11 ms |
9080 KB |
Output is correct |
24 |
Correct |
11 ms |
9080 KB |
Output is correct |
25 |
Correct |
11 ms |
9080 KB |
Output is correct |
26 |
Correct |
11 ms |
9080 KB |
Output is correct |
27 |
Correct |
11 ms |
9080 KB |
Output is correct |
28 |
Correct |
12 ms |
9088 KB |
Output is correct |
29 |
Correct |
11 ms |
9080 KB |
Output is correct |
30 |
Correct |
11 ms |
9080 KB |
Output is correct |
31 |
Correct |
11 ms |
9080 KB |
Output is correct |
32 |
Correct |
11 ms |
9148 KB |
Output is correct |
33 |
Correct |
14 ms |
9080 KB |
Output is correct |
34 |
Correct |
11 ms |
9080 KB |
Output is correct |
35 |
Correct |
12 ms |
9080 KB |
Output is correct |
36 |
Correct |
11 ms |
9084 KB |
Output is correct |
37 |
Correct |
11 ms |
9080 KB |
Output is correct |
38 |
Correct |
11 ms |
9080 KB |
Output is correct |
39 |
Correct |
298 ms |
17740 KB |
Output is correct |
40 |
Correct |
282 ms |
17628 KB |
Output is correct |
41 |
Correct |
347 ms |
17784 KB |
Output is correct |
42 |
Correct |
282 ms |
18168 KB |
Output is correct |
43 |
Correct |
157 ms |
17528 KB |
Output is correct |
44 |
Correct |
96 ms |
17784 KB |
Output is correct |
45 |
Correct |
270 ms |
19336 KB |
Output is correct |
46 |
Correct |
173 ms |
21356 KB |
Output is correct |
47 |
Correct |
352 ms |
26836 KB |
Output is correct |
48 |
Correct |
463 ms |
33092 KB |
Output is correct |
49 |
Correct |
470 ms |
32604 KB |
Output is correct |
50 |
Correct |
306 ms |
26936 KB |
Output is correct |
51 |
Correct |
303 ms |
26744 KB |
Output is correct |
52 |
Correct |
388 ms |
26896 KB |
Output is correct |
53 |
Correct |
480 ms |
26132 KB |
Output is correct |
54 |
Correct |
385 ms |
27012 KB |
Output is correct |
55 |
Correct |
26 ms |
9848 KB |
Output is correct |
56 |
Correct |
29 ms |
9976 KB |
Output is correct |
57 |
Correct |
159 ms |
17944 KB |
Output is correct |
58 |
Correct |
61 ms |
18676 KB |
Output is correct |
59 |
Correct |
182 ms |
21548 KB |
Output is correct |
60 |
Correct |
1101 ms |
33644 KB |
Output is correct |
61 |
Correct |
355 ms |
27252 KB |
Output is correct |
62 |
Correct |
421 ms |
27756 KB |
Output is correct |
63 |
Correct |
612 ms |
27888 KB |
Output is correct |
64 |
Correct |
1185 ms |
27796 KB |
Output is correct |
65 |
Correct |
421 ms |
27540 KB |
Output is correct |
66 |
Correct |
738 ms |
31736 KB |
Output is correct |
67 |
Correct |
173 ms |
29552 KB |
Output is correct |
68 |
Correct |
518 ms |
27888 KB |
Output is correct |
69 |
Correct |
538 ms |
28032 KB |
Output is correct |
70 |
Correct |
505 ms |
27084 KB |
Output is correct |