#include "race.h"
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
struct edge{
int to,cost;
};
vector<edge>E[200000];
int N,K,res,c;
int tree_id[200000],subtree_size[200000],dist[200000];
ll cost[200000];
int get_centroid(int x,int from,int size,int id){
subtree_size[x]=0;
if(tree_id[x]!=id)return -1;
subtree_size[x]=1;
for(edge e:E[x]){
if(e.to==from)continue;
int p=get_centroid(e.to,x,size,id);
if(p!=-1)return p;
subtree_size[x]+=subtree_size[e.to];
}
int t=x;
if(size-subtree_size[x]<=size/2){
for(edge e:E[x]){
if(subtree_size[e.to]>size/2)t=-1;
}
}
else t=-1;
return t;
}
void dfs(int x,int from,int id){
if(tree_id[x]!=id)return;
for(edge e:E[x]){
if(e.to==from)continue;
dist[e.to]=dist[x]+1;
cost[e.to]=cost[x]+e.cost;
dfs(e.to,x,id);
}
}
void dfs2(vector<int>&v,int x,int from,int id){
if(tree_id[x]!=id)return;
v.push_back(x);
for(edge e:E[x]){
if(e.to==from)continue;
dfs2(v,e.to,x,id);
}
}
void F(vector<int>&v,int id){
if(v.empty())return;
c++;
for(int i:v){
tree_id[i]=id;
}
int cent=get_centroid(v.front(),v.front(),v.size(),id);
dist[cent]=cost[cent]=0;
dfs(cent,cent,id);
vector<vector<int>>vv;
for(edge e:E[cent]){
vv.push_back({});
dfs2(vv.back(),e.to,cent,id);
}
set<pair<pair<ll,int>,int>>st;
for(int i:v){
st.insert({{cost[i],dist[i]},i});
}
auto it=st.lower_bound({{K,-1},-1});
if(it!=st.end()&&it->first.first==K){
res=min(res,it->first.second);
}
for(vector<int>&V:vv){
for(int i:V){
st.erase({{cost[i],dist[i]},i});
}
for(int i:V){
auto it=st.lower_bound({{K-cost[i],-1},-1});
if(it!=st.end()&&it->first.first==K-cost[i]){
res=min(res,it->first.second+dist[i]);
}
}
for(int i:V){
st.insert({{cost[i],dist[i]},i});
}
}
for(vector<int>&V:vv){
F(V,c);
}
}
int best_path(int n, int k, int H[][2], int L[]){
N=n;
K=k;
res=0xE869120;
for(int i=0;i<N-1;i++){
E[H[i][0]].push_back({H[i][1],L[i]});
E[H[i][1]].push_back({H[i][0],L[i]});
}
vector<int>v;
for(int i=0;i<N;i++){
v.push_back(i);
}
F(v,c);
if(res==0xE869120)res=-1;
return res;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
5120 KB |
Output is correct |
2 |
Correct |
4 ms |
5120 KB |
Output is correct |
3 |
Correct |
4 ms |
5120 KB |
Output is correct |
4 |
Correct |
4 ms |
5120 KB |
Output is correct |
5 |
Correct |
4 ms |
5120 KB |
Output is correct |
6 |
Correct |
5 ms |
5120 KB |
Output is correct |
7 |
Correct |
4 ms |
5120 KB |
Output is correct |
8 |
Correct |
4 ms |
5120 KB |
Output is correct |
9 |
Correct |
4 ms |
5120 KB |
Output is correct |
10 |
Correct |
4 ms |
5120 KB |
Output is correct |
11 |
Correct |
4 ms |
5120 KB |
Output is correct |
12 |
Correct |
4 ms |
5120 KB |
Output is correct |
13 |
Correct |
4 ms |
5120 KB |
Output is correct |
14 |
Correct |
4 ms |
5120 KB |
Output is correct |
15 |
Correct |
4 ms |
5120 KB |
Output is correct |
16 |
Correct |
4 ms |
5120 KB |
Output is correct |
17 |
Correct |
4 ms |
5120 KB |
Output is correct |
18 |
Correct |
4 ms |
5120 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
5120 KB |
Output is correct |
2 |
Correct |
4 ms |
5120 KB |
Output is correct |
3 |
Correct |
4 ms |
5120 KB |
Output is correct |
4 |
Correct |
4 ms |
5120 KB |
Output is correct |
5 |
Correct |
4 ms |
5120 KB |
Output is correct |
6 |
Correct |
5 ms |
5120 KB |
Output is correct |
7 |
Correct |
4 ms |
5120 KB |
Output is correct |
8 |
Correct |
4 ms |
5120 KB |
Output is correct |
9 |
Correct |
4 ms |
5120 KB |
Output is correct |
10 |
Correct |
4 ms |
5120 KB |
Output is correct |
11 |
Correct |
4 ms |
5120 KB |
Output is correct |
12 |
Correct |
4 ms |
5120 KB |
Output is correct |
13 |
Correct |
4 ms |
5120 KB |
Output is correct |
14 |
Correct |
4 ms |
5120 KB |
Output is correct |
15 |
Correct |
4 ms |
5120 KB |
Output is correct |
16 |
Correct |
4 ms |
5120 KB |
Output is correct |
17 |
Correct |
4 ms |
5120 KB |
Output is correct |
18 |
Correct |
4 ms |
5120 KB |
Output is correct |
19 |
Correct |
4 ms |
4992 KB |
Output is correct |
20 |
Correct |
4 ms |
4992 KB |
Output is correct |
21 |
Correct |
7 ms |
5248 KB |
Output is correct |
22 |
Correct |
8 ms |
5248 KB |
Output is correct |
23 |
Correct |
7 ms |
5248 KB |
Output is correct |
24 |
Correct |
7 ms |
5248 KB |
Output is correct |
25 |
Correct |
7 ms |
5248 KB |
Output is correct |
26 |
Correct |
7 ms |
5248 KB |
Output is correct |
27 |
Correct |
7 ms |
5248 KB |
Output is correct |
28 |
Correct |
7 ms |
5248 KB |
Output is correct |
29 |
Correct |
7 ms |
5248 KB |
Output is correct |
30 |
Correct |
7 ms |
5248 KB |
Output is correct |
31 |
Correct |
7 ms |
5248 KB |
Output is correct |
32 |
Correct |
7 ms |
5248 KB |
Output is correct |
33 |
Correct |
7 ms |
5248 KB |
Output is correct |
34 |
Correct |
7 ms |
5248 KB |
Output is correct |
35 |
Correct |
7 ms |
5248 KB |
Output is correct |
36 |
Correct |
7 ms |
5248 KB |
Output is correct |
37 |
Correct |
6 ms |
5248 KB |
Output is correct |
38 |
Correct |
7 ms |
5248 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
5120 KB |
Output is correct |
2 |
Correct |
4 ms |
5120 KB |
Output is correct |
3 |
Correct |
4 ms |
5120 KB |
Output is correct |
4 |
Correct |
4 ms |
5120 KB |
Output is correct |
5 |
Correct |
4 ms |
5120 KB |
Output is correct |
6 |
Correct |
5 ms |
5120 KB |
Output is correct |
7 |
Correct |
4 ms |
5120 KB |
Output is correct |
8 |
Correct |
4 ms |
5120 KB |
Output is correct |
9 |
Correct |
4 ms |
5120 KB |
Output is correct |
10 |
Correct |
4 ms |
5120 KB |
Output is correct |
11 |
Correct |
4 ms |
5120 KB |
Output is correct |
12 |
Correct |
4 ms |
5120 KB |
Output is correct |
13 |
Correct |
4 ms |
5120 KB |
Output is correct |
14 |
Correct |
4 ms |
5120 KB |
Output is correct |
15 |
Correct |
4 ms |
5120 KB |
Output is correct |
16 |
Correct |
4 ms |
5120 KB |
Output is correct |
17 |
Correct |
4 ms |
5120 KB |
Output is correct |
18 |
Correct |
4 ms |
5120 KB |
Output is correct |
19 |
Correct |
1094 ms |
25564 KB |
Output is correct |
20 |
Correct |
1004 ms |
25872 KB |
Output is correct |
21 |
Correct |
995 ms |
26276 KB |
Output is correct |
22 |
Correct |
978 ms |
25804 KB |
Output is correct |
23 |
Correct |
1183 ms |
25372 KB |
Output is correct |
24 |
Correct |
668 ms |
22348 KB |
Output is correct |
25 |
Correct |
984 ms |
31536 KB |
Output is correct |
26 |
Correct |
684 ms |
36228 KB |
Output is correct |
27 |
Correct |
859 ms |
36336 KB |
Output is correct |
28 |
Correct |
2269 ms |
67300 KB |
Output is correct |
29 |
Correct |
2340 ms |
65772 KB |
Output is correct |
30 |
Correct |
935 ms |
36328 KB |
Output is correct |
31 |
Correct |
878 ms |
36208 KB |
Output is correct |
32 |
Correct |
1158 ms |
36340 KB |
Output is correct |
33 |
Correct |
2224 ms |
46912 KB |
Output is correct |
34 |
Correct |
2099 ms |
46056 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
5120 KB |
Output is correct |
2 |
Correct |
4 ms |
5120 KB |
Output is correct |
3 |
Correct |
4 ms |
5120 KB |
Output is correct |
4 |
Correct |
4 ms |
5120 KB |
Output is correct |
5 |
Correct |
4 ms |
5120 KB |
Output is correct |
6 |
Correct |
5 ms |
5120 KB |
Output is correct |
7 |
Correct |
4 ms |
5120 KB |
Output is correct |
8 |
Correct |
4 ms |
5120 KB |
Output is correct |
9 |
Correct |
4 ms |
5120 KB |
Output is correct |
10 |
Correct |
4 ms |
5120 KB |
Output is correct |
11 |
Correct |
4 ms |
5120 KB |
Output is correct |
12 |
Correct |
4 ms |
5120 KB |
Output is correct |
13 |
Correct |
4 ms |
5120 KB |
Output is correct |
14 |
Correct |
4 ms |
5120 KB |
Output is correct |
15 |
Correct |
4 ms |
5120 KB |
Output is correct |
16 |
Correct |
4 ms |
5120 KB |
Output is correct |
17 |
Correct |
4 ms |
5120 KB |
Output is correct |
18 |
Correct |
4 ms |
5120 KB |
Output is correct |
19 |
Correct |
4 ms |
4992 KB |
Output is correct |
20 |
Correct |
4 ms |
4992 KB |
Output is correct |
21 |
Correct |
7 ms |
5248 KB |
Output is correct |
22 |
Correct |
8 ms |
5248 KB |
Output is correct |
23 |
Correct |
7 ms |
5248 KB |
Output is correct |
24 |
Correct |
7 ms |
5248 KB |
Output is correct |
25 |
Correct |
7 ms |
5248 KB |
Output is correct |
26 |
Correct |
7 ms |
5248 KB |
Output is correct |
27 |
Correct |
7 ms |
5248 KB |
Output is correct |
28 |
Correct |
7 ms |
5248 KB |
Output is correct |
29 |
Correct |
7 ms |
5248 KB |
Output is correct |
30 |
Correct |
7 ms |
5248 KB |
Output is correct |
31 |
Correct |
7 ms |
5248 KB |
Output is correct |
32 |
Correct |
7 ms |
5248 KB |
Output is correct |
33 |
Correct |
7 ms |
5248 KB |
Output is correct |
34 |
Correct |
7 ms |
5248 KB |
Output is correct |
35 |
Correct |
7 ms |
5248 KB |
Output is correct |
36 |
Correct |
7 ms |
5248 KB |
Output is correct |
37 |
Correct |
6 ms |
5248 KB |
Output is correct |
38 |
Correct |
7 ms |
5248 KB |
Output is correct |
39 |
Correct |
1094 ms |
25564 KB |
Output is correct |
40 |
Correct |
1004 ms |
25872 KB |
Output is correct |
41 |
Correct |
995 ms |
26276 KB |
Output is correct |
42 |
Correct |
978 ms |
25804 KB |
Output is correct |
43 |
Correct |
1183 ms |
25372 KB |
Output is correct |
44 |
Correct |
668 ms |
22348 KB |
Output is correct |
45 |
Correct |
984 ms |
31536 KB |
Output is correct |
46 |
Correct |
684 ms |
36228 KB |
Output is correct |
47 |
Correct |
859 ms |
36336 KB |
Output is correct |
48 |
Correct |
2269 ms |
67300 KB |
Output is correct |
49 |
Correct |
2340 ms |
65772 KB |
Output is correct |
50 |
Correct |
935 ms |
36328 KB |
Output is correct |
51 |
Correct |
878 ms |
36208 KB |
Output is correct |
52 |
Correct |
1158 ms |
36340 KB |
Output is correct |
53 |
Correct |
2224 ms |
46912 KB |
Output is correct |
54 |
Correct |
2099 ms |
46056 KB |
Output is correct |
55 |
Correct |
52 ms |
7168 KB |
Output is correct |
56 |
Correct |
59 ms |
7180 KB |
Output is correct |
57 |
Correct |
903 ms |
25144 KB |
Output is correct |
58 |
Correct |
189 ms |
25316 KB |
Output is correct |
59 |
Correct |
695 ms |
35956 KB |
Output is correct |
60 |
Correct |
2177 ms |
66196 KB |
Output is correct |
61 |
Correct |
879 ms |
36516 KB |
Output is correct |
62 |
Correct |
856 ms |
36340 KB |
Output is correct |
63 |
Correct |
1147 ms |
36572 KB |
Output is correct |
64 |
Correct |
2504 ms |
48168 KB |
Output is correct |
65 |
Correct |
2170 ms |
47132 KB |
Output is correct |
66 |
Correct |
2376 ms |
62728 KB |
Output is correct |
67 |
Correct |
627 ms |
46496 KB |
Output is correct |
68 |
Correct |
1425 ms |
43076 KB |
Output is correct |
69 |
Correct |
1328 ms |
43852 KB |
Output is correct |
70 |
Correct |
1305 ms |
41644 KB |
Output is correct |