#include "race.h"
#include <bits/stdc++.h>
#define sp <<" "
#define el <<"\n"
#define S second
#define F first
#define pb push_back
#define all(ar) ar.begin(),ar.end()
#define pii pair<ll,ll>
using namespace std;
using ll = long long;
using ld = long double;
const ll mxr = 200005;
const ll big = 2000000999;
ll n,k,path[1000011],mac,ans = big;
vector<vector<pii>> g;
bitset<mxr> ded;
vector<ll> sz(mxr);
void dfs(ll v,ll p){
sz[v] = 1;
for(pii r: g[v]){
if(r.F == p || ded[r.F]) continue;
dfs(r.F,v);
sz[v] += sz[r.F];
}
return;
}
ll fcen(ll v,ll p,ll lim){
for(pii r: g[v]){
if(r.F == p || ded[r.F]) continue;
if(sz[r.F] > lim) return fcen(r.F,v,lim);
}
return v;
}
void fil(ll v,ll p,ll cost,ll dep,unordered_map<ll,ll> &dis){
if(cost > k) return;
ans = min(ans,dep + path[k - cost]);
dis[cost] = dis.count(cost) ? min(dis[cost],dep) : dep;
for(pii r: g[v]){
if(r.F == p || ded[r.F]) continue;
fil(r.F,v,cost + r.S,dep+1,dis);
}
return;
}
void decompose(ll v){
dfs(v,-1);
ll lim = (sz[v]/2);
ll cen = fcen(v,-1,lim);
ded[cen] = 1;
unordered_set<ll> cum;
for(pii r: g[cen]){
if(ded[r.F]) continue;
unordered_map<ll,ll> dis;
fil(r.F,cen,r.S,1,dis);
for(pii x: dis){
cum.insert(x.F);
path[x.F] = min(x.S,path[x.F]);
}
}
ans = min(ans,path[k]);
for(ll i : cum) path[i] = big;
for(pii r: g[cen]){
if(!ded[r.F]) decompose(r.F);
}
}
int best_path(int N, int K, int H[][2], int L[])
{
n = N;
k = K;
g.assign(n,{});
for(ll i = 1; i < n; i++){
ll a = H[i-1][0], b = H[i-1][1], c = L[i-1];
g[a].pb({b,c});
g[b].pb({a,c});
}
for(ll i = 0; i <= 1000010; i++) path[i] = big;
decompose(0);
return ans == big ? -1 : ans;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
9676 KB |
Output is correct |
2 |
Correct |
6 ms |
9676 KB |
Output is correct |
3 |
Correct |
6 ms |
9676 KB |
Output is correct |
4 |
Correct |
6 ms |
9676 KB |
Output is correct |
5 |
Correct |
6 ms |
9676 KB |
Output is correct |
6 |
Correct |
6 ms |
9656 KB |
Output is correct |
7 |
Correct |
6 ms |
9628 KB |
Output is correct |
8 |
Correct |
6 ms |
9676 KB |
Output is correct |
9 |
Correct |
6 ms |
9676 KB |
Output is correct |
10 |
Correct |
6 ms |
9676 KB |
Output is correct |
11 |
Correct |
6 ms |
9676 KB |
Output is correct |
12 |
Correct |
6 ms |
9676 KB |
Output is correct |
13 |
Correct |
6 ms |
9676 KB |
Output is correct |
14 |
Correct |
6 ms |
9676 KB |
Output is correct |
15 |
Correct |
6 ms |
9676 KB |
Output is correct |
16 |
Correct |
6 ms |
9676 KB |
Output is correct |
17 |
Correct |
6 ms |
9672 KB |
Output is correct |
18 |
Correct |
6 ms |
9676 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
9676 KB |
Output is correct |
2 |
Correct |
6 ms |
9676 KB |
Output is correct |
3 |
Correct |
6 ms |
9676 KB |
Output is correct |
4 |
Correct |
6 ms |
9676 KB |
Output is correct |
5 |
Correct |
6 ms |
9676 KB |
Output is correct |
6 |
Correct |
6 ms |
9656 KB |
Output is correct |
7 |
Correct |
6 ms |
9628 KB |
Output is correct |
8 |
Correct |
6 ms |
9676 KB |
Output is correct |
9 |
Correct |
6 ms |
9676 KB |
Output is correct |
10 |
Correct |
6 ms |
9676 KB |
Output is correct |
11 |
Correct |
6 ms |
9676 KB |
Output is correct |
12 |
Correct |
6 ms |
9676 KB |
Output is correct |
13 |
Correct |
6 ms |
9676 KB |
Output is correct |
14 |
Correct |
6 ms |
9676 KB |
Output is correct |
15 |
Correct |
6 ms |
9676 KB |
Output is correct |
16 |
Correct |
6 ms |
9676 KB |
Output is correct |
17 |
Correct |
6 ms |
9672 KB |
Output is correct |
18 |
Correct |
6 ms |
9676 KB |
Output is correct |
19 |
Correct |
6 ms |
9676 KB |
Output is correct |
20 |
Correct |
6 ms |
9676 KB |
Output is correct |
21 |
Correct |
7 ms |
9804 KB |
Output is correct |
22 |
Correct |
7 ms |
9804 KB |
Output is correct |
23 |
Correct |
7 ms |
9804 KB |
Output is correct |
24 |
Correct |
7 ms |
9804 KB |
Output is correct |
25 |
Correct |
10 ms |
9792 KB |
Output is correct |
26 |
Correct |
7 ms |
9804 KB |
Output is correct |
27 |
Correct |
6 ms |
9804 KB |
Output is correct |
28 |
Correct |
8 ms |
9804 KB |
Output is correct |
29 |
Correct |
8 ms |
9804 KB |
Output is correct |
30 |
Correct |
8 ms |
9812 KB |
Output is correct |
31 |
Correct |
8 ms |
9872 KB |
Output is correct |
32 |
Correct |
8 ms |
9832 KB |
Output is correct |
33 |
Correct |
8 ms |
9816 KB |
Output is correct |
34 |
Correct |
7 ms |
9804 KB |
Output is correct |
35 |
Correct |
7 ms |
9804 KB |
Output is correct |
36 |
Correct |
7 ms |
9804 KB |
Output is correct |
37 |
Correct |
10 ms |
9804 KB |
Output is correct |
38 |
Correct |
10 ms |
9804 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
9676 KB |
Output is correct |
2 |
Correct |
6 ms |
9676 KB |
Output is correct |
3 |
Correct |
6 ms |
9676 KB |
Output is correct |
4 |
Correct |
6 ms |
9676 KB |
Output is correct |
5 |
Correct |
6 ms |
9676 KB |
Output is correct |
6 |
Correct |
6 ms |
9656 KB |
Output is correct |
7 |
Correct |
6 ms |
9628 KB |
Output is correct |
8 |
Correct |
6 ms |
9676 KB |
Output is correct |
9 |
Correct |
6 ms |
9676 KB |
Output is correct |
10 |
Correct |
6 ms |
9676 KB |
Output is correct |
11 |
Correct |
6 ms |
9676 KB |
Output is correct |
12 |
Correct |
6 ms |
9676 KB |
Output is correct |
13 |
Correct |
6 ms |
9676 KB |
Output is correct |
14 |
Correct |
6 ms |
9676 KB |
Output is correct |
15 |
Correct |
6 ms |
9676 KB |
Output is correct |
16 |
Correct |
6 ms |
9676 KB |
Output is correct |
17 |
Correct |
6 ms |
9672 KB |
Output is correct |
18 |
Correct |
6 ms |
9676 KB |
Output is correct |
19 |
Correct |
223 ms |
18364 KB |
Output is correct |
20 |
Correct |
210 ms |
18372 KB |
Output is correct |
21 |
Correct |
274 ms |
18392 KB |
Output is correct |
22 |
Correct |
200 ms |
18392 KB |
Output is correct |
23 |
Correct |
124 ms |
18720 KB |
Output is correct |
24 |
Correct |
122 ms |
17852 KB |
Output is correct |
25 |
Correct |
220 ms |
21188 KB |
Output is correct |
26 |
Correct |
140 ms |
24184 KB |
Output is correct |
27 |
Correct |
273 ms |
27380 KB |
Output is correct |
28 |
Correct |
377 ms |
38712 KB |
Output is correct |
29 |
Correct |
373 ms |
37700 KB |
Output is correct |
30 |
Correct |
268 ms |
27424 KB |
Output is correct |
31 |
Correct |
280 ms |
27592 KB |
Output is correct |
32 |
Correct |
325 ms |
27476 KB |
Output is correct |
33 |
Correct |
394 ms |
26304 KB |
Output is correct |
34 |
Correct |
228 ms |
26184 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
9676 KB |
Output is correct |
2 |
Correct |
6 ms |
9676 KB |
Output is correct |
3 |
Correct |
6 ms |
9676 KB |
Output is correct |
4 |
Correct |
6 ms |
9676 KB |
Output is correct |
5 |
Correct |
6 ms |
9676 KB |
Output is correct |
6 |
Correct |
6 ms |
9656 KB |
Output is correct |
7 |
Correct |
6 ms |
9628 KB |
Output is correct |
8 |
Correct |
6 ms |
9676 KB |
Output is correct |
9 |
Correct |
6 ms |
9676 KB |
Output is correct |
10 |
Correct |
6 ms |
9676 KB |
Output is correct |
11 |
Correct |
6 ms |
9676 KB |
Output is correct |
12 |
Correct |
6 ms |
9676 KB |
Output is correct |
13 |
Correct |
6 ms |
9676 KB |
Output is correct |
14 |
Correct |
6 ms |
9676 KB |
Output is correct |
15 |
Correct |
6 ms |
9676 KB |
Output is correct |
16 |
Correct |
6 ms |
9676 KB |
Output is correct |
17 |
Correct |
6 ms |
9672 KB |
Output is correct |
18 |
Correct |
6 ms |
9676 KB |
Output is correct |
19 |
Correct |
6 ms |
9676 KB |
Output is correct |
20 |
Correct |
6 ms |
9676 KB |
Output is correct |
21 |
Correct |
7 ms |
9804 KB |
Output is correct |
22 |
Correct |
7 ms |
9804 KB |
Output is correct |
23 |
Correct |
7 ms |
9804 KB |
Output is correct |
24 |
Correct |
7 ms |
9804 KB |
Output is correct |
25 |
Correct |
10 ms |
9792 KB |
Output is correct |
26 |
Correct |
7 ms |
9804 KB |
Output is correct |
27 |
Correct |
6 ms |
9804 KB |
Output is correct |
28 |
Correct |
8 ms |
9804 KB |
Output is correct |
29 |
Correct |
8 ms |
9804 KB |
Output is correct |
30 |
Correct |
8 ms |
9812 KB |
Output is correct |
31 |
Correct |
8 ms |
9872 KB |
Output is correct |
32 |
Correct |
8 ms |
9832 KB |
Output is correct |
33 |
Correct |
8 ms |
9816 KB |
Output is correct |
34 |
Correct |
7 ms |
9804 KB |
Output is correct |
35 |
Correct |
7 ms |
9804 KB |
Output is correct |
36 |
Correct |
7 ms |
9804 KB |
Output is correct |
37 |
Correct |
10 ms |
9804 KB |
Output is correct |
38 |
Correct |
10 ms |
9804 KB |
Output is correct |
39 |
Correct |
223 ms |
18364 KB |
Output is correct |
40 |
Correct |
210 ms |
18372 KB |
Output is correct |
41 |
Correct |
274 ms |
18392 KB |
Output is correct |
42 |
Correct |
200 ms |
18392 KB |
Output is correct |
43 |
Correct |
124 ms |
18720 KB |
Output is correct |
44 |
Correct |
122 ms |
17852 KB |
Output is correct |
45 |
Correct |
220 ms |
21188 KB |
Output is correct |
46 |
Correct |
140 ms |
24184 KB |
Output is correct |
47 |
Correct |
273 ms |
27380 KB |
Output is correct |
48 |
Correct |
377 ms |
38712 KB |
Output is correct |
49 |
Correct |
373 ms |
37700 KB |
Output is correct |
50 |
Correct |
268 ms |
27424 KB |
Output is correct |
51 |
Correct |
280 ms |
27592 KB |
Output is correct |
52 |
Correct |
325 ms |
27476 KB |
Output is correct |
53 |
Correct |
394 ms |
26304 KB |
Output is correct |
54 |
Correct |
228 ms |
26184 KB |
Output is correct |
55 |
Correct |
25 ms |
10924 KB |
Output is correct |
56 |
Correct |
20 ms |
10592 KB |
Output is correct |
57 |
Correct |
154 ms |
18728 KB |
Output is correct |
58 |
Correct |
59 ms |
17940 KB |
Output is correct |
59 |
Correct |
341 ms |
30200 KB |
Output is correct |
60 |
Correct |
1233 ms |
54976 KB |
Output is correct |
61 |
Correct |
357 ms |
27568 KB |
Output is correct |
62 |
Correct |
418 ms |
27508 KB |
Output is correct |
63 |
Correct |
548 ms |
27588 KB |
Output is correct |
64 |
Correct |
1043 ms |
37020 KB |
Output is correct |
65 |
Correct |
297 ms |
27044 KB |
Output is correct |
66 |
Correct |
1062 ms |
36880 KB |
Output is correct |
67 |
Correct |
177 ms |
26292 KB |
Output is correct |
68 |
Correct |
599 ms |
33084 KB |
Output is correct |
69 |
Correct |
561 ms |
32704 KB |
Output is correct |
70 |
Correct |
512 ms |
32000 KB |
Output is correct |