#include <bits/stdc++.h>
#include "race.h"
using namespace std;
typedef long long ll;
vector<pair<int,int>> adj[200001];
int sz[200001];
bool marked[200001]={0};
int m[1000001];
int k;
int dfs_sizes(int v, int pV){
sz[v]=0;
for(auto [w,d] : adj[v]){
if(w!=pV&&!marked[w]){
sz[v]+=dfs_sizes(w,v);
}
}
return sz[v];
}
int dfs_centroid(int v, int pV, int subtree_sz){
for(auto [w,d] : adj[v]){
if(pV!=w&&!marked[w]&&sz[w]>=(subtree_sz/2)){
return dfs_centroid(w,v,subtree_sz);
}
}
return v;
}
void dfs_paths(int v, int pV,int dist,int l, vector<pair<int,int>> &a){
for(auto [w,d] : adj[v]){
if(w!=pV&&!marked[w]){
if(dist+d<=k){
a.push_back({dist+d,l+1});
dfs_paths(w,v,dist+d,l+1,a);
}
}
}
}
int centroid_decomposition(int v){
int c = dfs_centroid(v,v,dfs_sizes(v,v));
marked[c]=1;
int ans=1000001;
vector<int> sus;
for(auto [w,d] : adj[c]){
if(!marked[w]){
vector<pair<int,int>> a;
dfs_paths(w,c,d,1,a);
for(auto [dist,l] : a){
if(m[k-dist]!=1000001){
ans=min(ans,l+m[k-dist]);
}
}
for(auto [dist,l] : a){
if(m[dist]!=1000001)m[dist]=min(m[dist],l);
else{
m[dist]=l;
sus.push_back(dist);
}
}
}
}
for(int i : sus)m[i]=1000001;
for(auto [w,d] : adj[c]){
if(!marked[w]){
ans = min(ans,centroid_decomposition(w));
}
}
return ans;
}
int best_path(int N, int K, int H[][2], int L[]){
for(ll i=0;i<N-1;i++){
adj[H[i][0]].push_back({H[i][1],L[i]});
adj[H[i][1]].push_back({H[i][0],L[i]});
}
k=K;
for(int i=0;i<=k;i++)m[i]=1000001;
m[0]=0;
ll ans = centroid_decomposition(0);
return (ans==1000001?-1:ans);
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
11608 KB |
Output is correct |
2 |
Correct |
2 ms |
11608 KB |
Output is correct |
3 |
Correct |
2 ms |
11604 KB |
Output is correct |
4 |
Correct |
2 ms |
11612 KB |
Output is correct |
5 |
Correct |
2 ms |
11612 KB |
Output is correct |
6 |
Correct |
2 ms |
11612 KB |
Output is correct |
7 |
Correct |
2 ms |
11612 KB |
Output is correct |
8 |
Correct |
2 ms |
11612 KB |
Output is correct |
9 |
Correct |
2 ms |
11612 KB |
Output is correct |
10 |
Correct |
2 ms |
11612 KB |
Output is correct |
11 |
Correct |
2 ms |
11860 KB |
Output is correct |
12 |
Correct |
2 ms |
11608 KB |
Output is correct |
13 |
Correct |
2 ms |
11612 KB |
Output is correct |
14 |
Correct |
2 ms |
11612 KB |
Output is correct |
15 |
Correct |
2 ms |
11612 KB |
Output is correct |
16 |
Correct |
2 ms |
11612 KB |
Output is correct |
17 |
Correct |
2 ms |
11452 KB |
Output is correct |
18 |
Correct |
2 ms |
11448 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
11608 KB |
Output is correct |
2 |
Correct |
2 ms |
11608 KB |
Output is correct |
3 |
Correct |
2 ms |
11604 KB |
Output is correct |
4 |
Correct |
2 ms |
11612 KB |
Output is correct |
5 |
Correct |
2 ms |
11612 KB |
Output is correct |
6 |
Correct |
2 ms |
11612 KB |
Output is correct |
7 |
Correct |
2 ms |
11612 KB |
Output is correct |
8 |
Correct |
2 ms |
11612 KB |
Output is correct |
9 |
Correct |
2 ms |
11612 KB |
Output is correct |
10 |
Correct |
2 ms |
11612 KB |
Output is correct |
11 |
Correct |
2 ms |
11860 KB |
Output is correct |
12 |
Correct |
2 ms |
11608 KB |
Output is correct |
13 |
Correct |
2 ms |
11612 KB |
Output is correct |
14 |
Correct |
2 ms |
11612 KB |
Output is correct |
15 |
Correct |
2 ms |
11612 KB |
Output is correct |
16 |
Correct |
2 ms |
11612 KB |
Output is correct |
17 |
Correct |
2 ms |
11452 KB |
Output is correct |
18 |
Correct |
2 ms |
11448 KB |
Output is correct |
19 |
Correct |
2 ms |
11612 KB |
Output is correct |
20 |
Correct |
3 ms |
11612 KB |
Output is correct |
21 |
Correct |
13 ms |
13124 KB |
Output is correct |
22 |
Correct |
6 ms |
13916 KB |
Output is correct |
23 |
Correct |
6 ms |
13916 KB |
Output is correct |
24 |
Correct |
6 ms |
13916 KB |
Output is correct |
25 |
Correct |
14 ms |
16476 KB |
Output is correct |
26 |
Correct |
5 ms |
13724 KB |
Output is correct |
27 |
Correct |
6 ms |
13660 KB |
Output is correct |
28 |
Correct |
7 ms |
14176 KB |
Output is correct |
29 |
Correct |
10 ms |
15196 KB |
Output is correct |
30 |
Correct |
11 ms |
15196 KB |
Output is correct |
31 |
Correct |
14 ms |
16360 KB |
Output is correct |
32 |
Correct |
16 ms |
16468 KB |
Output is correct |
33 |
Correct |
12 ms |
15708 KB |
Output is correct |
34 |
Correct |
7 ms |
13916 KB |
Output is correct |
35 |
Correct |
8 ms |
14052 KB |
Output is correct |
36 |
Correct |
7 ms |
13916 KB |
Output is correct |
37 |
Correct |
12 ms |
15708 KB |
Output is correct |
38 |
Correct |
14 ms |
16112 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
11608 KB |
Output is correct |
2 |
Correct |
2 ms |
11608 KB |
Output is correct |
3 |
Correct |
2 ms |
11604 KB |
Output is correct |
4 |
Correct |
2 ms |
11612 KB |
Output is correct |
5 |
Correct |
2 ms |
11612 KB |
Output is correct |
6 |
Correct |
2 ms |
11612 KB |
Output is correct |
7 |
Correct |
2 ms |
11612 KB |
Output is correct |
8 |
Correct |
2 ms |
11612 KB |
Output is correct |
9 |
Correct |
2 ms |
11612 KB |
Output is correct |
10 |
Correct |
2 ms |
11612 KB |
Output is correct |
11 |
Correct |
2 ms |
11860 KB |
Output is correct |
12 |
Correct |
2 ms |
11608 KB |
Output is correct |
13 |
Correct |
2 ms |
11612 KB |
Output is correct |
14 |
Correct |
2 ms |
11612 KB |
Output is correct |
15 |
Correct |
2 ms |
11612 KB |
Output is correct |
16 |
Correct |
2 ms |
11612 KB |
Output is correct |
17 |
Correct |
2 ms |
11452 KB |
Output is correct |
18 |
Correct |
2 ms |
11448 KB |
Output is correct |
19 |
Execution timed out |
3049 ms |
19252 KB |
Time limit exceeded |
20 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
11608 KB |
Output is correct |
2 |
Correct |
2 ms |
11608 KB |
Output is correct |
3 |
Correct |
2 ms |
11604 KB |
Output is correct |
4 |
Correct |
2 ms |
11612 KB |
Output is correct |
5 |
Correct |
2 ms |
11612 KB |
Output is correct |
6 |
Correct |
2 ms |
11612 KB |
Output is correct |
7 |
Correct |
2 ms |
11612 KB |
Output is correct |
8 |
Correct |
2 ms |
11612 KB |
Output is correct |
9 |
Correct |
2 ms |
11612 KB |
Output is correct |
10 |
Correct |
2 ms |
11612 KB |
Output is correct |
11 |
Correct |
2 ms |
11860 KB |
Output is correct |
12 |
Correct |
2 ms |
11608 KB |
Output is correct |
13 |
Correct |
2 ms |
11612 KB |
Output is correct |
14 |
Correct |
2 ms |
11612 KB |
Output is correct |
15 |
Correct |
2 ms |
11612 KB |
Output is correct |
16 |
Correct |
2 ms |
11612 KB |
Output is correct |
17 |
Correct |
2 ms |
11452 KB |
Output is correct |
18 |
Correct |
2 ms |
11448 KB |
Output is correct |
19 |
Correct |
2 ms |
11612 KB |
Output is correct |
20 |
Correct |
3 ms |
11612 KB |
Output is correct |
21 |
Correct |
13 ms |
13124 KB |
Output is correct |
22 |
Correct |
6 ms |
13916 KB |
Output is correct |
23 |
Correct |
6 ms |
13916 KB |
Output is correct |
24 |
Correct |
6 ms |
13916 KB |
Output is correct |
25 |
Correct |
14 ms |
16476 KB |
Output is correct |
26 |
Correct |
5 ms |
13724 KB |
Output is correct |
27 |
Correct |
6 ms |
13660 KB |
Output is correct |
28 |
Correct |
7 ms |
14176 KB |
Output is correct |
29 |
Correct |
10 ms |
15196 KB |
Output is correct |
30 |
Correct |
11 ms |
15196 KB |
Output is correct |
31 |
Correct |
14 ms |
16360 KB |
Output is correct |
32 |
Correct |
16 ms |
16468 KB |
Output is correct |
33 |
Correct |
12 ms |
15708 KB |
Output is correct |
34 |
Correct |
7 ms |
13916 KB |
Output is correct |
35 |
Correct |
8 ms |
14052 KB |
Output is correct |
36 |
Correct |
7 ms |
13916 KB |
Output is correct |
37 |
Correct |
12 ms |
15708 KB |
Output is correct |
38 |
Correct |
14 ms |
16112 KB |
Output is correct |
39 |
Execution timed out |
3049 ms |
19252 KB |
Time limit exceeded |
40 |
Halted |
0 ms |
0 KB |
- |