#include <bits/stdc++.h>
// #include "race.h"
// #include <ext/pb_ds/assoc_container.hpp>
// using namespace __gnu_pbds;
// #pragma GCC optimize ("Ofast")
// #pragma GCC target ("avx,avx2")
using namespace std;
typedef long long ll;
typedef pair<ll, int> pp;
#define rep(i,l,r) for(int i = (l); i < r; i++)
#define per(i,r,l) for(int i = (r); i >= l; i--)
#define all(x) x.begin(), x.end()
#define sz(x) (int)(x).size()
#define pb push_back
#define ff first
#define ss second
const ll mod = 998244353, maxn = 1e6 + 5, inf = 1e9 + 5;
bool ban[maxn];
int cnt[maxn], k;
vector<pp> adj[maxn], st[maxn];
int dist[maxn];
void set_cnt(int r, int p){
cnt[r] = 1;
for(pp c: adj[r]) if(c.ff != p && !ban[c.ff]){
set_cnt(c.ff, r);
cnt[r] += cnt[c.ff];
}
}
int findCenteroid(int r, int p, int tot){
for(pp c: adj[r]) if(c.ff != p && !ban[c.ff]){
if(cnt[c.ff] >= tot - cnt[c.ff]) return findCenteroid(c.ff, r, tot);
}
return r;
}
void dfs(int r, int p, int rt, ll len, int w){
st[rt].pb({len, w});
for(pp c: adj[r]) if(c.ff != p && !ban[c.ff]) dfs(c.ff, r, rt, len + c.ss, w + 1);
}
int slv(int r){
set_cnt(r, r);
r = findCenteroid(r, r, cnt[r]);
int ans = inf;
for(pp c: adj[r]) if(!ban[c.ff]){
dfs(c.ff, r, c.ff, c.ss, 1);
for(pp t: st[c.ff]){
int v = k - t.ff;
if(v < 0 || (v > 0 && dist[v] == 0)) continue;
ans = min(ans, dist[v] + t.ss);
}
for(pp t: st[c.ff]){
if(t.ff > k) continue;
if(dist[t.ff] == 0) dist[t.ff] = t.ss;
dist[t.ff] = min(dist[t.ff], t.ss);
}
}
for(pp c: adj[r]) if(!ban[c.ff]){
for(pp t: st[c.ff]){
if(t.ff > k) continue;
dist[t.ff] = 0;
}
st[c.ff].clear();
}
ban[r] = true;
for(pp c: adj[r]) if(!ban[c.ff]){
ans = min(ans, slv(c.ff));
}
return ans;
}
int best_path(int N, int K, int H[][2], int L[]){
k = K;
rep(i,0,N-1){
adj[H[i][0]].pb({H[i][1], L[i]});
adj[H[i][1]].pb({H[i][0], L[i]});
}
int ans = slv(0);
return ans == inf? -1: ans;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
22 ms |
47308 KB |
Output is correct |
2 |
Correct |
22 ms |
47312 KB |
Output is correct |
3 |
Correct |
22 ms |
47316 KB |
Output is correct |
4 |
Correct |
22 ms |
47268 KB |
Output is correct |
5 |
Correct |
22 ms |
47248 KB |
Output is correct |
6 |
Correct |
21 ms |
47216 KB |
Output is correct |
7 |
Correct |
24 ms |
47212 KB |
Output is correct |
8 |
Correct |
23 ms |
47268 KB |
Output is correct |
9 |
Correct |
22 ms |
47196 KB |
Output is correct |
10 |
Correct |
24 ms |
47300 KB |
Output is correct |
11 |
Correct |
23 ms |
47208 KB |
Output is correct |
12 |
Correct |
22 ms |
47220 KB |
Output is correct |
13 |
Correct |
23 ms |
47308 KB |
Output is correct |
14 |
Correct |
23 ms |
47316 KB |
Output is correct |
15 |
Correct |
22 ms |
47320 KB |
Output is correct |
16 |
Correct |
23 ms |
47296 KB |
Output is correct |
17 |
Correct |
22 ms |
47292 KB |
Output is correct |
18 |
Correct |
23 ms |
47208 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
22 ms |
47308 KB |
Output is correct |
2 |
Correct |
22 ms |
47312 KB |
Output is correct |
3 |
Correct |
22 ms |
47316 KB |
Output is correct |
4 |
Correct |
22 ms |
47268 KB |
Output is correct |
5 |
Correct |
22 ms |
47248 KB |
Output is correct |
6 |
Correct |
21 ms |
47216 KB |
Output is correct |
7 |
Correct |
24 ms |
47212 KB |
Output is correct |
8 |
Correct |
23 ms |
47268 KB |
Output is correct |
9 |
Correct |
22 ms |
47196 KB |
Output is correct |
10 |
Correct |
24 ms |
47300 KB |
Output is correct |
11 |
Correct |
23 ms |
47208 KB |
Output is correct |
12 |
Correct |
22 ms |
47220 KB |
Output is correct |
13 |
Correct |
23 ms |
47308 KB |
Output is correct |
14 |
Correct |
23 ms |
47316 KB |
Output is correct |
15 |
Correct |
22 ms |
47320 KB |
Output is correct |
16 |
Correct |
23 ms |
47296 KB |
Output is correct |
17 |
Correct |
22 ms |
47292 KB |
Output is correct |
18 |
Correct |
23 ms |
47208 KB |
Output is correct |
19 |
Correct |
21 ms |
47180 KB |
Output is correct |
20 |
Correct |
23 ms |
47304 KB |
Output is correct |
21 |
Correct |
24 ms |
47436 KB |
Output is correct |
22 |
Correct |
29 ms |
49876 KB |
Output is correct |
23 |
Correct |
29 ms |
49612 KB |
Output is correct |
24 |
Correct |
25 ms |
50248 KB |
Output is correct |
25 |
Correct |
25 ms |
49140 KB |
Output is correct |
26 |
Correct |
25 ms |
48768 KB |
Output is correct |
27 |
Correct |
24 ms |
47424 KB |
Output is correct |
28 |
Correct |
25 ms |
48204 KB |
Output is correct |
29 |
Correct |
25 ms |
48728 KB |
Output is correct |
30 |
Correct |
24 ms |
48856 KB |
Output is correct |
31 |
Correct |
25 ms |
49220 KB |
Output is correct |
32 |
Correct |
25 ms |
49204 KB |
Output is correct |
33 |
Correct |
25 ms |
50044 KB |
Output is correct |
34 |
Correct |
24 ms |
49700 KB |
Output is correct |
35 |
Correct |
26 ms |
50384 KB |
Output is correct |
36 |
Correct |
25 ms |
50672 KB |
Output is correct |
37 |
Correct |
25 ms |
49376 KB |
Output is correct |
38 |
Correct |
26 ms |
48564 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
22 ms |
47308 KB |
Output is correct |
2 |
Correct |
22 ms |
47312 KB |
Output is correct |
3 |
Correct |
22 ms |
47316 KB |
Output is correct |
4 |
Correct |
22 ms |
47268 KB |
Output is correct |
5 |
Correct |
22 ms |
47248 KB |
Output is correct |
6 |
Correct |
21 ms |
47216 KB |
Output is correct |
7 |
Correct |
24 ms |
47212 KB |
Output is correct |
8 |
Correct |
23 ms |
47268 KB |
Output is correct |
9 |
Correct |
22 ms |
47196 KB |
Output is correct |
10 |
Correct |
24 ms |
47300 KB |
Output is correct |
11 |
Correct |
23 ms |
47208 KB |
Output is correct |
12 |
Correct |
22 ms |
47220 KB |
Output is correct |
13 |
Correct |
23 ms |
47308 KB |
Output is correct |
14 |
Correct |
23 ms |
47316 KB |
Output is correct |
15 |
Correct |
22 ms |
47320 KB |
Output is correct |
16 |
Correct |
23 ms |
47296 KB |
Output is correct |
17 |
Correct |
22 ms |
47292 KB |
Output is correct |
18 |
Correct |
23 ms |
47208 KB |
Output is correct |
19 |
Correct |
192 ms |
73644 KB |
Output is correct |
20 |
Correct |
169 ms |
73440 KB |
Output is correct |
21 |
Correct |
177 ms |
72940 KB |
Output is correct |
22 |
Correct |
161 ms |
69344 KB |
Output is correct |
23 |
Correct |
151 ms |
79668 KB |
Output is correct |
24 |
Correct |
117 ms |
66456 KB |
Output is correct |
25 |
Correct |
199 ms |
89280 KB |
Output is correct |
26 |
Correct |
136 ms |
90004 KB |
Output is correct |
27 |
Correct |
214 ms |
87372 KB |
Output is correct |
28 |
Correct |
479 ms |
136404 KB |
Output is correct |
29 |
Correct |
446 ms |
135524 KB |
Output is correct |
30 |
Correct |
236 ms |
87336 KB |
Output is correct |
31 |
Correct |
259 ms |
87344 KB |
Output is correct |
32 |
Correct |
309 ms |
87324 KB |
Output is correct |
33 |
Correct |
320 ms |
114688 KB |
Output is correct |
34 |
Correct |
343 ms |
112924 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
22 ms |
47308 KB |
Output is correct |
2 |
Correct |
22 ms |
47312 KB |
Output is correct |
3 |
Correct |
22 ms |
47316 KB |
Output is correct |
4 |
Correct |
22 ms |
47268 KB |
Output is correct |
5 |
Correct |
22 ms |
47248 KB |
Output is correct |
6 |
Correct |
21 ms |
47216 KB |
Output is correct |
7 |
Correct |
24 ms |
47212 KB |
Output is correct |
8 |
Correct |
23 ms |
47268 KB |
Output is correct |
9 |
Correct |
22 ms |
47196 KB |
Output is correct |
10 |
Correct |
24 ms |
47300 KB |
Output is correct |
11 |
Correct |
23 ms |
47208 KB |
Output is correct |
12 |
Correct |
22 ms |
47220 KB |
Output is correct |
13 |
Correct |
23 ms |
47308 KB |
Output is correct |
14 |
Correct |
23 ms |
47316 KB |
Output is correct |
15 |
Correct |
22 ms |
47320 KB |
Output is correct |
16 |
Correct |
23 ms |
47296 KB |
Output is correct |
17 |
Correct |
22 ms |
47292 KB |
Output is correct |
18 |
Correct |
23 ms |
47208 KB |
Output is correct |
19 |
Correct |
21 ms |
47180 KB |
Output is correct |
20 |
Correct |
23 ms |
47304 KB |
Output is correct |
21 |
Correct |
24 ms |
47436 KB |
Output is correct |
22 |
Correct |
29 ms |
49876 KB |
Output is correct |
23 |
Correct |
29 ms |
49612 KB |
Output is correct |
24 |
Correct |
25 ms |
50248 KB |
Output is correct |
25 |
Correct |
25 ms |
49140 KB |
Output is correct |
26 |
Correct |
25 ms |
48768 KB |
Output is correct |
27 |
Correct |
24 ms |
47424 KB |
Output is correct |
28 |
Correct |
25 ms |
48204 KB |
Output is correct |
29 |
Correct |
25 ms |
48728 KB |
Output is correct |
30 |
Correct |
24 ms |
48856 KB |
Output is correct |
31 |
Correct |
25 ms |
49220 KB |
Output is correct |
32 |
Correct |
25 ms |
49204 KB |
Output is correct |
33 |
Correct |
25 ms |
50044 KB |
Output is correct |
34 |
Correct |
24 ms |
49700 KB |
Output is correct |
35 |
Correct |
26 ms |
50384 KB |
Output is correct |
36 |
Correct |
25 ms |
50672 KB |
Output is correct |
37 |
Correct |
25 ms |
49376 KB |
Output is correct |
38 |
Correct |
26 ms |
48564 KB |
Output is correct |
39 |
Correct |
192 ms |
73644 KB |
Output is correct |
40 |
Correct |
169 ms |
73440 KB |
Output is correct |
41 |
Correct |
177 ms |
72940 KB |
Output is correct |
42 |
Correct |
161 ms |
69344 KB |
Output is correct |
43 |
Correct |
151 ms |
79668 KB |
Output is correct |
44 |
Correct |
117 ms |
66456 KB |
Output is correct |
45 |
Correct |
199 ms |
89280 KB |
Output is correct |
46 |
Correct |
136 ms |
90004 KB |
Output is correct |
47 |
Correct |
214 ms |
87372 KB |
Output is correct |
48 |
Correct |
479 ms |
136404 KB |
Output is correct |
49 |
Correct |
446 ms |
135524 KB |
Output is correct |
50 |
Correct |
236 ms |
87336 KB |
Output is correct |
51 |
Correct |
259 ms |
87344 KB |
Output is correct |
52 |
Correct |
309 ms |
87324 KB |
Output is correct |
53 |
Correct |
320 ms |
114688 KB |
Output is correct |
54 |
Correct |
343 ms |
112924 KB |
Output is correct |
55 |
Correct |
32 ms |
49160 KB |
Output is correct |
56 |
Correct |
33 ms |
49484 KB |
Output is correct |
57 |
Correct |
121 ms |
79576 KB |
Output is correct |
58 |
Correct |
59 ms |
57816 KB |
Output is correct |
59 |
Correct |
133 ms |
90344 KB |
Output is correct |
60 |
Correct |
483 ms |
138256 KB |
Output is correct |
61 |
Correct |
224 ms |
87500 KB |
Output is correct |
62 |
Correct |
211 ms |
87216 KB |
Output is correct |
63 |
Correct |
307 ms |
87200 KB |
Output is correct |
64 |
Correct |
442 ms |
103960 KB |
Output is correct |
65 |
Correct |
385 ms |
93564 KB |
Output is correct |
66 |
Correct |
466 ms |
139072 KB |
Output is correct |
67 |
Correct |
163 ms |
69544 KB |
Output is correct |
68 |
Correct |
295 ms |
87632 KB |
Output is correct |
69 |
Correct |
299 ms |
87424 KB |
Output is correct |
70 |
Correct |
280 ms |
85456 KB |
Output is correct |