#include "race.h"
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
vector<vector<pair<ll,ll>>> adjlist; //n,l
vector<set<pair<ll,ll>>*> vset; //height,numnodes
vector<ll> prefsum;
vector<ll> height;
ll n,k;
ll INF = 1e18;
void dfs(ll node, ll par){
for (auto p : adjlist[node]){
if (p.first==par){continue;}
prefsum[p.first]=prefsum[node]+p.second;
height[p.first]=height[node]+1;
dfs(p.first,node);
}
}
ll minval(ll node, ll par){
if (adjlist[node].size()==1&&par!=-1){
////cout<<"x\n";
vset[node]=new set<pair<ll,ll>>();
vset[node]->insert({prefsum[node],height[node]});
return INF;
}
ll mxv = INF;
ll mxind = -1;
ll mxsize = -1;
for (auto p : adjlist[node]){
ll i = p.first;
if (i==par){continue;}
mxv=min(mxv,minval(i,node));
ll vs = vset[i]->size();
////cout<<vs<<" "<<mxsize<<'\n';
if (vs>mxsize){
mxind=i;
mxsize=vs;
}
}
////cout<<mxind<<' '<<mxsize<<'\n';
//merge everything llo vset[i]
ll kv = (par==-1)?k:(k+2*prefsum[node]);
for (auto p : adjlist[node]){
ll i = p.first;
if (i==par||i==mxind){continue;}
for (auto hn : *vset[i]){
ll heightfind = kv-hn.first;
//can we find heightfind?
auto f = vset[mxind]->lower_bound({heightfind,0});
if (f!=vset[mxind]->end()&&f->first==heightfind){
mxv=min(mxv,hn.second+f->second-2*height[node]);
}
vset[mxind]->insert(hn);
}
}
//cout<<"check "<<k+prefsum[node]<<'\n';
/*for (auto p : *vset[mxind]){
//cout<<p.first<<" "<<p.second<<", ";
}//cout<<'\n';*/
auto f = vset[mxind]->lower_bound({k+prefsum[node],-1});
if (f!=vset[mxind]->end()&&f->first==k+prefsum[node]){
mxv=min(mxv,f->second-height[node]);
}
vset[node]=vset[mxind];
vset[node]->insert({prefsum[node],height[node]});
//cout<<node<<" "<<par<<": "<<mxv<<'\n';
return mxv;
}
int best_path(int N, int K, int H[][2], int L[]){
n=N;k=K;
adjlist.resize(n);
for (ll i = 0; i<n-1; i++){
adjlist[H[i][0]].push_back({H[i][1],L[i]});
adjlist[H[i][1]].push_back({H[i][0],L[i]});
}
vset.resize(n,NULL);
prefsum.resize(n,0);
height.resize(n,0);
dfs(1,-1);
ll mv = minval(1,-1);
return (mv==INF)?-1:mv;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
384 KB |
Output is correct |
2 |
Correct |
0 ms |
384 KB |
Output is correct |
3 |
Correct |
1 ms |
384 KB |
Output is correct |
4 |
Correct |
0 ms |
384 KB |
Output is correct |
5 |
Correct |
0 ms |
384 KB |
Output is correct |
6 |
Correct |
0 ms |
384 KB |
Output is correct |
7 |
Correct |
1 ms |
384 KB |
Output is correct |
8 |
Correct |
0 ms |
384 KB |
Output is correct |
9 |
Correct |
1 ms |
384 KB |
Output is correct |
10 |
Correct |
0 ms |
384 KB |
Output is correct |
11 |
Correct |
0 ms |
384 KB |
Output is correct |
12 |
Correct |
0 ms |
384 KB |
Output is correct |
13 |
Correct |
0 ms |
384 KB |
Output is correct |
14 |
Correct |
1 ms |
384 KB |
Output is correct |
15 |
Correct |
0 ms |
384 KB |
Output is correct |
16 |
Correct |
0 ms |
384 KB |
Output is correct |
17 |
Correct |
0 ms |
384 KB |
Output is correct |
18 |
Correct |
0 ms |
384 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
384 KB |
Output is correct |
2 |
Correct |
0 ms |
384 KB |
Output is correct |
3 |
Correct |
1 ms |
384 KB |
Output is correct |
4 |
Correct |
0 ms |
384 KB |
Output is correct |
5 |
Correct |
0 ms |
384 KB |
Output is correct |
6 |
Correct |
0 ms |
384 KB |
Output is correct |
7 |
Correct |
1 ms |
384 KB |
Output is correct |
8 |
Correct |
0 ms |
384 KB |
Output is correct |
9 |
Correct |
1 ms |
384 KB |
Output is correct |
10 |
Correct |
0 ms |
384 KB |
Output is correct |
11 |
Correct |
0 ms |
384 KB |
Output is correct |
12 |
Correct |
0 ms |
384 KB |
Output is correct |
13 |
Correct |
0 ms |
384 KB |
Output is correct |
14 |
Correct |
1 ms |
384 KB |
Output is correct |
15 |
Correct |
0 ms |
384 KB |
Output is correct |
16 |
Correct |
0 ms |
384 KB |
Output is correct |
17 |
Correct |
0 ms |
384 KB |
Output is correct |
18 |
Correct |
0 ms |
384 KB |
Output is correct |
19 |
Correct |
0 ms |
384 KB |
Output is correct |
20 |
Correct |
0 ms |
384 KB |
Output is correct |
21 |
Correct |
1 ms |
640 KB |
Output is correct |
22 |
Correct |
1 ms |
640 KB |
Output is correct |
23 |
Correct |
1 ms |
640 KB |
Output is correct |
24 |
Correct |
1 ms |
640 KB |
Output is correct |
25 |
Correct |
1 ms |
768 KB |
Output is correct |
26 |
Correct |
1 ms |
640 KB |
Output is correct |
27 |
Correct |
1 ms |
512 KB |
Output is correct |
28 |
Correct |
1 ms |
640 KB |
Output is correct |
29 |
Correct |
1 ms |
640 KB |
Output is correct |
30 |
Correct |
2 ms |
640 KB |
Output is correct |
31 |
Correct |
1 ms |
640 KB |
Output is correct |
32 |
Correct |
2 ms |
640 KB |
Output is correct |
33 |
Correct |
1 ms |
640 KB |
Output is correct |
34 |
Correct |
1 ms |
640 KB |
Output is correct |
35 |
Correct |
1 ms |
512 KB |
Output is correct |
36 |
Correct |
1 ms |
512 KB |
Output is correct |
37 |
Correct |
1 ms |
512 KB |
Output is correct |
38 |
Correct |
1 ms |
640 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
384 KB |
Output is correct |
2 |
Correct |
0 ms |
384 KB |
Output is correct |
3 |
Correct |
1 ms |
384 KB |
Output is correct |
4 |
Correct |
0 ms |
384 KB |
Output is correct |
5 |
Correct |
0 ms |
384 KB |
Output is correct |
6 |
Correct |
0 ms |
384 KB |
Output is correct |
7 |
Correct |
1 ms |
384 KB |
Output is correct |
8 |
Correct |
0 ms |
384 KB |
Output is correct |
9 |
Correct |
1 ms |
384 KB |
Output is correct |
10 |
Correct |
0 ms |
384 KB |
Output is correct |
11 |
Correct |
0 ms |
384 KB |
Output is correct |
12 |
Correct |
0 ms |
384 KB |
Output is correct |
13 |
Correct |
0 ms |
384 KB |
Output is correct |
14 |
Correct |
1 ms |
384 KB |
Output is correct |
15 |
Correct |
0 ms |
384 KB |
Output is correct |
16 |
Correct |
0 ms |
384 KB |
Output is correct |
17 |
Correct |
0 ms |
384 KB |
Output is correct |
18 |
Correct |
0 ms |
384 KB |
Output is correct |
19 |
Correct |
136 ms |
30716 KB |
Output is correct |
20 |
Correct |
151 ms |
30584 KB |
Output is correct |
21 |
Correct |
149 ms |
30328 KB |
Output is correct |
22 |
Correct |
155 ms |
29944 KB |
Output is correct |
23 |
Correct |
178 ms |
45048 KB |
Output is correct |
24 |
Correct |
118 ms |
35576 KB |
Output is correct |
25 |
Correct |
118 ms |
32888 KB |
Output is correct |
26 |
Correct |
88 ms |
33912 KB |
Output is correct |
27 |
Correct |
215 ms |
45820 KB |
Output is correct |
28 |
Correct |
288 ms |
64888 KB |
Output is correct |
29 |
Correct |
301 ms |
58744 KB |
Output is correct |
30 |
Correct |
222 ms |
45944 KB |
Output is correct |
31 |
Correct |
227 ms |
45816 KB |
Output is correct |
32 |
Correct |
273 ms |
45944 KB |
Output is correct |
33 |
Correct |
210 ms |
35192 KB |
Output is correct |
34 |
Correct |
323 ms |
68216 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
384 KB |
Output is correct |
2 |
Correct |
0 ms |
384 KB |
Output is correct |
3 |
Correct |
1 ms |
384 KB |
Output is correct |
4 |
Correct |
0 ms |
384 KB |
Output is correct |
5 |
Correct |
0 ms |
384 KB |
Output is correct |
6 |
Correct |
0 ms |
384 KB |
Output is correct |
7 |
Correct |
1 ms |
384 KB |
Output is correct |
8 |
Correct |
0 ms |
384 KB |
Output is correct |
9 |
Correct |
1 ms |
384 KB |
Output is correct |
10 |
Correct |
0 ms |
384 KB |
Output is correct |
11 |
Correct |
0 ms |
384 KB |
Output is correct |
12 |
Correct |
0 ms |
384 KB |
Output is correct |
13 |
Correct |
0 ms |
384 KB |
Output is correct |
14 |
Correct |
1 ms |
384 KB |
Output is correct |
15 |
Correct |
0 ms |
384 KB |
Output is correct |
16 |
Correct |
0 ms |
384 KB |
Output is correct |
17 |
Correct |
0 ms |
384 KB |
Output is correct |
18 |
Correct |
0 ms |
384 KB |
Output is correct |
19 |
Correct |
0 ms |
384 KB |
Output is correct |
20 |
Correct |
0 ms |
384 KB |
Output is correct |
21 |
Correct |
1 ms |
640 KB |
Output is correct |
22 |
Correct |
1 ms |
640 KB |
Output is correct |
23 |
Correct |
1 ms |
640 KB |
Output is correct |
24 |
Correct |
1 ms |
640 KB |
Output is correct |
25 |
Correct |
1 ms |
768 KB |
Output is correct |
26 |
Correct |
1 ms |
640 KB |
Output is correct |
27 |
Correct |
1 ms |
512 KB |
Output is correct |
28 |
Correct |
1 ms |
640 KB |
Output is correct |
29 |
Correct |
1 ms |
640 KB |
Output is correct |
30 |
Correct |
2 ms |
640 KB |
Output is correct |
31 |
Correct |
1 ms |
640 KB |
Output is correct |
32 |
Correct |
2 ms |
640 KB |
Output is correct |
33 |
Correct |
1 ms |
640 KB |
Output is correct |
34 |
Correct |
1 ms |
640 KB |
Output is correct |
35 |
Correct |
1 ms |
512 KB |
Output is correct |
36 |
Correct |
1 ms |
512 KB |
Output is correct |
37 |
Correct |
1 ms |
512 KB |
Output is correct |
38 |
Correct |
1 ms |
640 KB |
Output is correct |
39 |
Correct |
136 ms |
30716 KB |
Output is correct |
40 |
Correct |
151 ms |
30584 KB |
Output is correct |
41 |
Correct |
149 ms |
30328 KB |
Output is correct |
42 |
Correct |
155 ms |
29944 KB |
Output is correct |
43 |
Correct |
178 ms |
45048 KB |
Output is correct |
44 |
Correct |
118 ms |
35576 KB |
Output is correct |
45 |
Correct |
118 ms |
32888 KB |
Output is correct |
46 |
Correct |
88 ms |
33912 KB |
Output is correct |
47 |
Correct |
215 ms |
45820 KB |
Output is correct |
48 |
Correct |
288 ms |
64888 KB |
Output is correct |
49 |
Correct |
301 ms |
58744 KB |
Output is correct |
50 |
Correct |
222 ms |
45944 KB |
Output is correct |
51 |
Correct |
227 ms |
45816 KB |
Output is correct |
52 |
Correct |
273 ms |
45944 KB |
Output is correct |
53 |
Correct |
210 ms |
35192 KB |
Output is correct |
54 |
Correct |
323 ms |
68216 KB |
Output is correct |
55 |
Correct |
14 ms |
4224 KB |
Output is correct |
56 |
Correct |
9 ms |
2944 KB |
Output is correct |
57 |
Correct |
76 ms |
27000 KB |
Output is correct |
58 |
Correct |
59 ms |
24424 KB |
Output is correct |
59 |
Correct |
82 ms |
33784 KB |
Output is correct |
60 |
Correct |
270 ms |
57208 KB |
Output is correct |
61 |
Correct |
245 ms |
51448 KB |
Output is correct |
62 |
Correct |
226 ms |
45816 KB |
Output is correct |
63 |
Correct |
299 ms |
45944 KB |
Output is correct |
64 |
Correct |
431 ms |
99580 KB |
Output is correct |
65 |
Correct |
439 ms |
95712 KB |
Output is correct |
66 |
Correct |
263 ms |
65656 KB |
Output is correct |
67 |
Correct |
193 ms |
49760 KB |
Output is correct |
68 |
Correct |
325 ms |
66296 KB |
Output is correct |
69 |
Correct |
324 ms |
67064 KB |
Output is correct |
70 |
Correct |
359 ms |
63480 KB |
Output is correct |