/*
* Written by
* ______ _ _ ______ ____ ____ ____ ____
* |_ _|| |_| ||_ _||_ || __ || __ |/ _/
* | | | _ | | | / / || |||| ||| |__
* | | | | | | | | | |_ ||__||||__||\__ \
* |__| |_| |_| |__| |___||____||____|/____/
*/
#include <bits/stdc++.h>
using namespace std;
#define nmax 200020
#define ii pair<int, int>
#define ll long long
int n, k, ans, cnt[1000100], sz[nmax], r[nmax];
vector<ii> adj[nmax];
inline void minimize(int &a, int b)
{
if(a == -1 || a > b)
a = b;
}
int dfs(int u, int p = -1)
{
sz[u] = 1;
for(auto [C, v]: adj[u]) {
if(v != p && !r[v]) sz[u] += dfs(v, u);
}
return sz[u];
}
int get_centroid(int m, int u, int p = -1)
{
for(auto [C, v]: adj[u]) {
if(v != p && !r[v] && 2 * sz[v] > m) return get_centroid(m, v, u);
}
return u;
}
void get_cnt(int sum, bool filling, int u, int p = -1, int depth = 1)
{
if(sum > k) return;
if(filling)
minimize(cnt[sum], depth);
else {
if(cnt[k - sum] != -1)
minimize(ans, depth + cnt[k - sum]);
}
for(auto [C, v]: adj[u]) {
if(v != p && !r[v]) get_cnt(sum + C, filling, v, u, depth + 1);
}
}
void del(int sum, int u, int p = -1)
{
if(sum > k) return;
cnt[sum] = -1;
for(auto [C, v]: adj[u]) {
if(v != p && !r[v])
del(sum + C, v, u);
}
}
void centroid(int u = 0)
{
u = get_centroid(dfs(u), u);
r[u] = 1;
cnt[0] = 0;
for(auto [C, v]: adj[u]) {
if(!r[v]) {
get_cnt(C, false, v);
get_cnt(C, true, v);
}
}
for(auto [C, v]: adj[u]) {
if(!r[v]) {
del(C, v);
}
}
for(auto [C, v]: adj[u]) {
if(!r[v]) centroid(v);
}
}
int best_path(int N, int K, int h[][2], int l[])
{
n = N;
k = K;
for(int i = 0, u, v; i < n - 1; ++i) {
u = h[i][0];
v = h[i][1];
adj[u].push_back(ii(l[i], v));
adj[v].push_back(ii(l[i], u));
}
ans = -1;
memset(cnt, -1, sizeof(cnt));
centroid();
return ans;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
8908 KB |
Output is correct |
2 |
Correct |
7 ms |
8908 KB |
Output is correct |
3 |
Correct |
6 ms |
8848 KB |
Output is correct |
4 |
Correct |
6 ms |
8908 KB |
Output is correct |
5 |
Correct |
7 ms |
8852 KB |
Output is correct |
6 |
Correct |
8 ms |
8908 KB |
Output is correct |
7 |
Correct |
9 ms |
8908 KB |
Output is correct |
8 |
Correct |
6 ms |
8908 KB |
Output is correct |
9 |
Correct |
8 ms |
8908 KB |
Output is correct |
10 |
Correct |
8 ms |
8908 KB |
Output is correct |
11 |
Correct |
8 ms |
8908 KB |
Output is correct |
12 |
Correct |
7 ms |
8852 KB |
Output is correct |
13 |
Correct |
6 ms |
8856 KB |
Output is correct |
14 |
Correct |
6 ms |
8908 KB |
Output is correct |
15 |
Correct |
7 ms |
8908 KB |
Output is correct |
16 |
Correct |
5 ms |
8908 KB |
Output is correct |
17 |
Correct |
6 ms |
8908 KB |
Output is correct |
18 |
Correct |
6 ms |
8908 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
8908 KB |
Output is correct |
2 |
Correct |
7 ms |
8908 KB |
Output is correct |
3 |
Correct |
6 ms |
8848 KB |
Output is correct |
4 |
Correct |
6 ms |
8908 KB |
Output is correct |
5 |
Correct |
7 ms |
8852 KB |
Output is correct |
6 |
Correct |
8 ms |
8908 KB |
Output is correct |
7 |
Correct |
9 ms |
8908 KB |
Output is correct |
8 |
Correct |
6 ms |
8908 KB |
Output is correct |
9 |
Correct |
8 ms |
8908 KB |
Output is correct |
10 |
Correct |
8 ms |
8908 KB |
Output is correct |
11 |
Correct |
8 ms |
8908 KB |
Output is correct |
12 |
Correct |
7 ms |
8852 KB |
Output is correct |
13 |
Correct |
6 ms |
8856 KB |
Output is correct |
14 |
Correct |
6 ms |
8908 KB |
Output is correct |
15 |
Correct |
7 ms |
8908 KB |
Output is correct |
16 |
Correct |
5 ms |
8908 KB |
Output is correct |
17 |
Correct |
6 ms |
8908 KB |
Output is correct |
18 |
Correct |
6 ms |
8908 KB |
Output is correct |
19 |
Correct |
6 ms |
8908 KB |
Output is correct |
20 |
Correct |
6 ms |
8908 KB |
Output is correct |
21 |
Correct |
8 ms |
8908 KB |
Output is correct |
22 |
Correct |
7 ms |
8908 KB |
Output is correct |
23 |
Correct |
6 ms |
8972 KB |
Output is correct |
24 |
Correct |
7 ms |
8992 KB |
Output is correct |
25 |
Correct |
8 ms |
8908 KB |
Output is correct |
26 |
Correct |
7 ms |
8960 KB |
Output is correct |
27 |
Correct |
7 ms |
9036 KB |
Output is correct |
28 |
Correct |
7 ms |
8996 KB |
Output is correct |
29 |
Correct |
10 ms |
8988 KB |
Output is correct |
30 |
Correct |
9 ms |
8992 KB |
Output is correct |
31 |
Correct |
9 ms |
8908 KB |
Output is correct |
32 |
Correct |
8 ms |
8984 KB |
Output is correct |
33 |
Correct |
8 ms |
8908 KB |
Output is correct |
34 |
Correct |
9 ms |
8908 KB |
Output is correct |
35 |
Correct |
7 ms |
8908 KB |
Output is correct |
36 |
Correct |
8 ms |
8908 KB |
Output is correct |
37 |
Correct |
10 ms |
8984 KB |
Output is correct |
38 |
Correct |
9 ms |
8908 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
8908 KB |
Output is correct |
2 |
Correct |
7 ms |
8908 KB |
Output is correct |
3 |
Correct |
6 ms |
8848 KB |
Output is correct |
4 |
Correct |
6 ms |
8908 KB |
Output is correct |
5 |
Correct |
7 ms |
8852 KB |
Output is correct |
6 |
Correct |
8 ms |
8908 KB |
Output is correct |
7 |
Correct |
9 ms |
8908 KB |
Output is correct |
8 |
Correct |
6 ms |
8908 KB |
Output is correct |
9 |
Correct |
8 ms |
8908 KB |
Output is correct |
10 |
Correct |
8 ms |
8908 KB |
Output is correct |
11 |
Correct |
8 ms |
8908 KB |
Output is correct |
12 |
Correct |
7 ms |
8852 KB |
Output is correct |
13 |
Correct |
6 ms |
8856 KB |
Output is correct |
14 |
Correct |
6 ms |
8908 KB |
Output is correct |
15 |
Correct |
7 ms |
8908 KB |
Output is correct |
16 |
Correct |
5 ms |
8908 KB |
Output is correct |
17 |
Correct |
6 ms |
8908 KB |
Output is correct |
18 |
Correct |
6 ms |
8908 KB |
Output is correct |
19 |
Correct |
250 ms |
15820 KB |
Output is correct |
20 |
Correct |
210 ms |
15876 KB |
Output is correct |
21 |
Correct |
233 ms |
15800 KB |
Output is correct |
22 |
Correct |
253 ms |
16068 KB |
Output is correct |
23 |
Correct |
96 ms |
16104 KB |
Output is correct |
24 |
Correct |
87 ms |
16024 KB |
Output is correct |
25 |
Correct |
192 ms |
18588 KB |
Output is correct |
26 |
Correct |
121 ms |
21568 KB |
Output is correct |
27 |
Correct |
238 ms |
23268 KB |
Output is correct |
28 |
Correct |
402 ms |
34684 KB |
Output is correct |
29 |
Correct |
394 ms |
33732 KB |
Output is correct |
30 |
Correct |
240 ms |
23236 KB |
Output is correct |
31 |
Correct |
242 ms |
23188 KB |
Output is correct |
32 |
Correct |
321 ms |
23352 KB |
Output is correct |
33 |
Correct |
326 ms |
22212 KB |
Output is correct |
34 |
Correct |
255 ms |
23020 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
8908 KB |
Output is correct |
2 |
Correct |
7 ms |
8908 KB |
Output is correct |
3 |
Correct |
6 ms |
8848 KB |
Output is correct |
4 |
Correct |
6 ms |
8908 KB |
Output is correct |
5 |
Correct |
7 ms |
8852 KB |
Output is correct |
6 |
Correct |
8 ms |
8908 KB |
Output is correct |
7 |
Correct |
9 ms |
8908 KB |
Output is correct |
8 |
Correct |
6 ms |
8908 KB |
Output is correct |
9 |
Correct |
8 ms |
8908 KB |
Output is correct |
10 |
Correct |
8 ms |
8908 KB |
Output is correct |
11 |
Correct |
8 ms |
8908 KB |
Output is correct |
12 |
Correct |
7 ms |
8852 KB |
Output is correct |
13 |
Correct |
6 ms |
8856 KB |
Output is correct |
14 |
Correct |
6 ms |
8908 KB |
Output is correct |
15 |
Correct |
7 ms |
8908 KB |
Output is correct |
16 |
Correct |
5 ms |
8908 KB |
Output is correct |
17 |
Correct |
6 ms |
8908 KB |
Output is correct |
18 |
Correct |
6 ms |
8908 KB |
Output is correct |
19 |
Correct |
6 ms |
8908 KB |
Output is correct |
20 |
Correct |
6 ms |
8908 KB |
Output is correct |
21 |
Correct |
8 ms |
8908 KB |
Output is correct |
22 |
Correct |
7 ms |
8908 KB |
Output is correct |
23 |
Correct |
6 ms |
8972 KB |
Output is correct |
24 |
Correct |
7 ms |
8992 KB |
Output is correct |
25 |
Correct |
8 ms |
8908 KB |
Output is correct |
26 |
Correct |
7 ms |
8960 KB |
Output is correct |
27 |
Correct |
7 ms |
9036 KB |
Output is correct |
28 |
Correct |
7 ms |
8996 KB |
Output is correct |
29 |
Correct |
10 ms |
8988 KB |
Output is correct |
30 |
Correct |
9 ms |
8992 KB |
Output is correct |
31 |
Correct |
9 ms |
8908 KB |
Output is correct |
32 |
Correct |
8 ms |
8984 KB |
Output is correct |
33 |
Correct |
8 ms |
8908 KB |
Output is correct |
34 |
Correct |
9 ms |
8908 KB |
Output is correct |
35 |
Correct |
7 ms |
8908 KB |
Output is correct |
36 |
Correct |
8 ms |
8908 KB |
Output is correct |
37 |
Correct |
10 ms |
8984 KB |
Output is correct |
38 |
Correct |
9 ms |
8908 KB |
Output is correct |
39 |
Correct |
250 ms |
15820 KB |
Output is correct |
40 |
Correct |
210 ms |
15876 KB |
Output is correct |
41 |
Correct |
233 ms |
15800 KB |
Output is correct |
42 |
Correct |
253 ms |
16068 KB |
Output is correct |
43 |
Correct |
96 ms |
16104 KB |
Output is correct |
44 |
Correct |
87 ms |
16024 KB |
Output is correct |
45 |
Correct |
192 ms |
18588 KB |
Output is correct |
46 |
Correct |
121 ms |
21568 KB |
Output is correct |
47 |
Correct |
238 ms |
23268 KB |
Output is correct |
48 |
Correct |
402 ms |
34684 KB |
Output is correct |
49 |
Correct |
394 ms |
33732 KB |
Output is correct |
50 |
Correct |
240 ms |
23236 KB |
Output is correct |
51 |
Correct |
242 ms |
23188 KB |
Output is correct |
52 |
Correct |
321 ms |
23352 KB |
Output is correct |
53 |
Correct |
326 ms |
22212 KB |
Output is correct |
54 |
Correct |
255 ms |
23020 KB |
Output is correct |
55 |
Correct |
17 ms |
9556 KB |
Output is correct |
56 |
Correct |
17 ms |
9608 KB |
Output is correct |
57 |
Correct |
108 ms |
16044 KB |
Output is correct |
58 |
Correct |
46 ms |
15736 KB |
Output is correct |
59 |
Correct |
137 ms |
21612 KB |
Output is correct |
60 |
Correct |
824 ms |
33876 KB |
Output is correct |
61 |
Correct |
270 ms |
23436 KB |
Output is correct |
62 |
Correct |
306 ms |
23300 KB |
Output is correct |
63 |
Correct |
387 ms |
23620 KB |
Output is correct |
64 |
Correct |
907 ms |
23600 KB |
Output is correct |
65 |
Correct |
284 ms |
24088 KB |
Output is correct |
66 |
Correct |
634 ms |
31940 KB |
Output is correct |
67 |
Correct |
173 ms |
23984 KB |
Output is correct |
68 |
Correct |
391 ms |
24108 KB |
Output is correct |
69 |
Correct |
366 ms |
24168 KB |
Output is correct |
70 |
Correct |
367 ms |
23496 KB |
Output is correct |