#include<bits/stdc++.h>
#include "race.h"
#define ll long long
#define pb push_back
#define eb emplace_back
#define vl vector<ll>
#define fi first
#define se second
#define in insert
#define mpr make_pair
#define lg(x) __lg(x)
#define bpc(x) __builtin_popcount(x)
#define all(v) v.begin(), v.end()
#define endl "\n"
using namespace std;
const int sz = 2e5+5;
const ll inf = 1000000000000000;
bool used[sz];
vector<pair<ll,ll>> arr, adj[sz];
ll n, k, ss[sz], res = inf;
map<ll,ll> mp;
void construct(ll node, ll p){
ss[node] = 1;
for(pair<ll,ll> edge: adj[node]){
if(!used[edge.fi] && edge.fi != p){
construct(edge.fi, node);
ss[node] += ss[edge.fi];
}
}
}
ll centroid(ll v, ll p, ll cs){
for(pair<ll,ll> edge: adj[v]){
if(edge.fi == p || used[edge.fi]){continue;}
if(ss[edge.fi] > (cs>>1)){
return centroid(edge.fi, v, cs);
}
}
return v;
}
void init(ll v, ll p, ll w, ll we){
if(mp.find(k - w) != mp.end()){
res = min(res, mp[k-w] + we);
}
arr.pb({w, we});
for(pair<ll,ll> edge: adj[v]){
if(used[v] || edge.fi == p){continue;}
init(edge.fi, v, w + edge.se, we + 1);
}
}
void calc(ll node){
mp.clear();
construct(node, -1);
ll c = centroid(node, -1, ss[node]);
mp[0] = 0;
for(pair<ll,ll> edge: adj[c]){
if(used[edge.fi]){continue;}
arr.clear();
init(edge.fi, c, edge.se, 1);
for(pair<ll,ll> y: arr){
if(mp.find(y.fi) == mp.end()){
mp[y.fi] = y.se;
}
else{
mp[y.fi] = min(mp[y.fi], y.se);
}
}
}
used[c] = 1;
for(pair<ll,ll> edge: adj[c]){
if(!used[edge.fi]){
calc(edge.fi);
}
}
}
int best_path(int N, int K, int H[][2], int L[])
{
n = N;
k = K;
for(ll i=0;i<n-1;i++){
adj[H[i][0]].pb({H[i][1], L[i]});
adj[H[i][1]].pb({H[i][0], L[i]});
}
calc(0);
if(res == inf){
return -1;
}
return res;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
10336 KB |
Output is correct |
2 |
Correct |
2 ms |
10332 KB |
Output is correct |
3 |
Correct |
2 ms |
10340 KB |
Output is correct |
4 |
Correct |
2 ms |
10332 KB |
Output is correct |
5 |
Correct |
2 ms |
10332 KB |
Output is correct |
6 |
Correct |
2 ms |
10340 KB |
Output is correct |
7 |
Correct |
2 ms |
10332 KB |
Output is correct |
8 |
Correct |
2 ms |
10332 KB |
Output is correct |
9 |
Correct |
2 ms |
10340 KB |
Output is correct |
10 |
Correct |
2 ms |
10332 KB |
Output is correct |
11 |
Correct |
3 ms |
10332 KB |
Output is correct |
12 |
Correct |
3 ms |
10332 KB |
Output is correct |
13 |
Correct |
3 ms |
10164 KB |
Output is correct |
14 |
Correct |
2 ms |
10332 KB |
Output is correct |
15 |
Correct |
2 ms |
10332 KB |
Output is correct |
16 |
Correct |
2 ms |
10328 KB |
Output is correct |
17 |
Correct |
2 ms |
10328 KB |
Output is correct |
18 |
Correct |
2 ms |
10328 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
10336 KB |
Output is correct |
2 |
Correct |
2 ms |
10332 KB |
Output is correct |
3 |
Correct |
2 ms |
10340 KB |
Output is correct |
4 |
Correct |
2 ms |
10332 KB |
Output is correct |
5 |
Correct |
2 ms |
10332 KB |
Output is correct |
6 |
Correct |
2 ms |
10340 KB |
Output is correct |
7 |
Correct |
2 ms |
10332 KB |
Output is correct |
8 |
Correct |
2 ms |
10332 KB |
Output is correct |
9 |
Correct |
2 ms |
10340 KB |
Output is correct |
10 |
Correct |
2 ms |
10332 KB |
Output is correct |
11 |
Correct |
3 ms |
10332 KB |
Output is correct |
12 |
Correct |
3 ms |
10332 KB |
Output is correct |
13 |
Correct |
3 ms |
10164 KB |
Output is correct |
14 |
Correct |
2 ms |
10332 KB |
Output is correct |
15 |
Correct |
2 ms |
10332 KB |
Output is correct |
16 |
Correct |
2 ms |
10328 KB |
Output is correct |
17 |
Correct |
2 ms |
10328 KB |
Output is correct |
18 |
Correct |
2 ms |
10328 KB |
Output is correct |
19 |
Correct |
2 ms |
10332 KB |
Output is correct |
20 |
Correct |
2 ms |
10332 KB |
Output is correct |
21 |
Correct |
3 ms |
10444 KB |
Output is correct |
22 |
Correct |
4 ms |
10360 KB |
Output is correct |
23 |
Correct |
4 ms |
10332 KB |
Output is correct |
24 |
Correct |
3 ms |
10332 KB |
Output is correct |
25 |
Correct |
3 ms |
10332 KB |
Output is correct |
26 |
Correct |
3 ms |
10332 KB |
Output is correct |
27 |
Correct |
4 ms |
10340 KB |
Output is correct |
28 |
Correct |
3 ms |
10332 KB |
Output is correct |
29 |
Correct |
4 ms |
10332 KB |
Output is correct |
30 |
Correct |
3 ms |
10332 KB |
Output is correct |
31 |
Correct |
4 ms |
10356 KB |
Output is correct |
32 |
Correct |
4 ms |
10328 KB |
Output is correct |
33 |
Correct |
3 ms |
10328 KB |
Output is correct |
34 |
Correct |
3 ms |
10332 KB |
Output is correct |
35 |
Correct |
3 ms |
10332 KB |
Output is correct |
36 |
Correct |
3 ms |
10332 KB |
Output is correct |
37 |
Correct |
3 ms |
10428 KB |
Output is correct |
38 |
Correct |
3 ms |
10328 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
10336 KB |
Output is correct |
2 |
Correct |
2 ms |
10332 KB |
Output is correct |
3 |
Correct |
2 ms |
10340 KB |
Output is correct |
4 |
Correct |
2 ms |
10332 KB |
Output is correct |
5 |
Correct |
2 ms |
10332 KB |
Output is correct |
6 |
Correct |
2 ms |
10340 KB |
Output is correct |
7 |
Correct |
2 ms |
10332 KB |
Output is correct |
8 |
Correct |
2 ms |
10332 KB |
Output is correct |
9 |
Correct |
2 ms |
10340 KB |
Output is correct |
10 |
Correct |
2 ms |
10332 KB |
Output is correct |
11 |
Correct |
3 ms |
10332 KB |
Output is correct |
12 |
Correct |
3 ms |
10332 KB |
Output is correct |
13 |
Correct |
3 ms |
10164 KB |
Output is correct |
14 |
Correct |
2 ms |
10332 KB |
Output is correct |
15 |
Correct |
2 ms |
10332 KB |
Output is correct |
16 |
Correct |
2 ms |
10328 KB |
Output is correct |
17 |
Correct |
2 ms |
10328 KB |
Output is correct |
18 |
Correct |
2 ms |
10328 KB |
Output is correct |
19 |
Correct |
161 ms |
19664 KB |
Output is correct |
20 |
Correct |
163 ms |
19664 KB |
Output is correct |
21 |
Correct |
147 ms |
19772 KB |
Output is correct |
22 |
Correct |
141 ms |
19920 KB |
Output is correct |
23 |
Correct |
224 ms |
20276 KB |
Output is correct |
24 |
Correct |
102 ms |
18908 KB |
Output is correct |
25 |
Correct |
144 ms |
25036 KB |
Output is correct |
26 |
Correct |
83 ms |
25560 KB |
Output is correct |
27 |
Correct |
223 ms |
25724 KB |
Output is correct |
28 |
Correct |
627 ms |
50404 KB |
Output is correct |
29 |
Correct |
605 ms |
49520 KB |
Output is correct |
30 |
Correct |
222 ms |
25784 KB |
Output is correct |
31 |
Correct |
209 ms |
25684 KB |
Output is correct |
32 |
Correct |
255 ms |
25832 KB |
Output is correct |
33 |
Correct |
382 ms |
26192 KB |
Output is correct |
34 |
Correct |
594 ms |
38232 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
10336 KB |
Output is correct |
2 |
Correct |
2 ms |
10332 KB |
Output is correct |
3 |
Correct |
2 ms |
10340 KB |
Output is correct |
4 |
Correct |
2 ms |
10332 KB |
Output is correct |
5 |
Correct |
2 ms |
10332 KB |
Output is correct |
6 |
Correct |
2 ms |
10340 KB |
Output is correct |
7 |
Correct |
2 ms |
10332 KB |
Output is correct |
8 |
Correct |
2 ms |
10332 KB |
Output is correct |
9 |
Correct |
2 ms |
10340 KB |
Output is correct |
10 |
Correct |
2 ms |
10332 KB |
Output is correct |
11 |
Correct |
3 ms |
10332 KB |
Output is correct |
12 |
Correct |
3 ms |
10332 KB |
Output is correct |
13 |
Correct |
3 ms |
10164 KB |
Output is correct |
14 |
Correct |
2 ms |
10332 KB |
Output is correct |
15 |
Correct |
2 ms |
10332 KB |
Output is correct |
16 |
Correct |
2 ms |
10328 KB |
Output is correct |
17 |
Correct |
2 ms |
10328 KB |
Output is correct |
18 |
Correct |
2 ms |
10328 KB |
Output is correct |
19 |
Correct |
2 ms |
10332 KB |
Output is correct |
20 |
Correct |
2 ms |
10332 KB |
Output is correct |
21 |
Correct |
3 ms |
10444 KB |
Output is correct |
22 |
Correct |
4 ms |
10360 KB |
Output is correct |
23 |
Correct |
4 ms |
10332 KB |
Output is correct |
24 |
Correct |
3 ms |
10332 KB |
Output is correct |
25 |
Correct |
3 ms |
10332 KB |
Output is correct |
26 |
Correct |
3 ms |
10332 KB |
Output is correct |
27 |
Correct |
4 ms |
10340 KB |
Output is correct |
28 |
Correct |
3 ms |
10332 KB |
Output is correct |
29 |
Correct |
4 ms |
10332 KB |
Output is correct |
30 |
Correct |
3 ms |
10332 KB |
Output is correct |
31 |
Correct |
4 ms |
10356 KB |
Output is correct |
32 |
Correct |
4 ms |
10328 KB |
Output is correct |
33 |
Correct |
3 ms |
10328 KB |
Output is correct |
34 |
Correct |
3 ms |
10332 KB |
Output is correct |
35 |
Correct |
3 ms |
10332 KB |
Output is correct |
36 |
Correct |
3 ms |
10332 KB |
Output is correct |
37 |
Correct |
3 ms |
10428 KB |
Output is correct |
38 |
Correct |
3 ms |
10328 KB |
Output is correct |
39 |
Correct |
161 ms |
19664 KB |
Output is correct |
40 |
Correct |
163 ms |
19664 KB |
Output is correct |
41 |
Correct |
147 ms |
19772 KB |
Output is correct |
42 |
Correct |
141 ms |
19920 KB |
Output is correct |
43 |
Correct |
224 ms |
20276 KB |
Output is correct |
44 |
Correct |
102 ms |
18908 KB |
Output is correct |
45 |
Correct |
144 ms |
25036 KB |
Output is correct |
46 |
Correct |
83 ms |
25560 KB |
Output is correct |
47 |
Correct |
223 ms |
25724 KB |
Output is correct |
48 |
Correct |
627 ms |
50404 KB |
Output is correct |
49 |
Correct |
605 ms |
49520 KB |
Output is correct |
50 |
Correct |
222 ms |
25784 KB |
Output is correct |
51 |
Correct |
209 ms |
25684 KB |
Output is correct |
52 |
Correct |
255 ms |
25832 KB |
Output is correct |
53 |
Correct |
382 ms |
26192 KB |
Output is correct |
54 |
Correct |
594 ms |
38232 KB |
Output is correct |
55 |
Correct |
18 ms |
11376 KB |
Output is correct |
56 |
Correct |
11 ms |
11124 KB |
Output is correct |
57 |
Correct |
107 ms |
20080 KB |
Output is correct |
58 |
Correct |
43 ms |
18064 KB |
Output is correct |
59 |
Correct |
238 ms |
30156 KB |
Output is correct |
60 |
Correct |
630 ms |
49588 KB |
Output is correct |
61 |
Correct |
261 ms |
27968 KB |
Output is correct |
62 |
Correct |
209 ms |
26196 KB |
Output is correct |
63 |
Correct |
244 ms |
26256 KB |
Output is correct |
64 |
Correct |
656 ms |
33552 KB |
Output is correct |
65 |
Correct |
583 ms |
39904 KB |
Output is correct |
66 |
Correct |
643 ms |
49908 KB |
Output is correct |
67 |
Correct |
100 ms |
25272 KB |
Output is correct |
68 |
Correct |
383 ms |
38144 KB |
Output is correct |
69 |
Correct |
333 ms |
38152 KB |
Output is correct |
70 |
Correct |
327 ms |
36968 KB |
Output is correct |