#include <bits/stdc++.h>
using namespace std;
/*
John Watson
https://codeforces.com/profile/quangminh98
Mua Code nhu mua Florentino !!
*/
#define faster() ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define ll long long
const int oo = 1e9;
const int maxn = 2e5 + 9;
vector<pair<int, int>> adj[maxn];
int del[maxn], sz[maxn], h[maxn];
vector<int> cnt;
int n, k, res = oo;
int subtree(int u, int p = -1)
{
sz[u] = 1;
for (pair<int, int> nxt : adj[u])
{
int v = nxt.first;
if (v == p || del[v]) continue;
sz[u] += subtree(v, u);
}
return sz[u];
}
int find_centroid(int u, int p, int N)
{
for (pair<int, int> nxt : adj[u])
{
int v = nxt.first;
if (v == p || del[v]) continue;
if (sz[v] > N / 2)
return find_centroid(v, u, N);
}
return u;
}
void count(int u, int p, int w, int h)
{
if (w > k) return;
if (cnt[k - w] != oo)
res = min(res, cnt[k - w] + h);
for (pair<int, int> nxt : adj[u])
{
int v = nxt.first;
if (del[v] || v == p) continue;
count(v, u, w + nxt.second, h + 1);
}
}
int mx_w = 0;
void update(int u, int p, int w, int h)
{
if (w > k) return;
mx_w = max(mx_w, w);
cnt[w] = min(cnt[w], h);
for (pair<int, int> nxt : adj[u])
{
int v = nxt.first;
if (del[v] || v == p) continue;
update(v, u, w + nxt.second, h + 1);
}
}
void centroid_decomposition(int u)
{
int centroid = find_centroid(u, -1, subtree(u));
del[centroid] = 1;
mx_w = 0;
for (pair<int, int> nxt : adj[centroid])
{
int v = nxt.first;
if (del[v]) continue;
count(v, centroid, nxt.second, 1);
update(v, centroid, nxt.second, 1);
}
for (int i = 1; i <= mx_w; i++) cnt[i] = oo;
for (pair<int, int> nxt : adj[centroid])
{
int v = nxt.first;
if (del[v]) continue;
centroid_decomposition(v);
}
}
int best_path(int _n, int _k, int H[][2], int l[])
{
n = _n;
k = _k;
cnt.resize(k + 5, oo);
cnt[0] = 0;
for (int i = 0; i < n - 1; i++)
{
int w = l[i];
int u = H[i][0], v = H[i][1];
adj[u].push_back({v, w});
adj[v].push_back({u, w});
}
centroid_decomposition(1);
return (res == oo ? -1 : res);
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
4956 KB |
Output is correct |
2 |
Correct |
2 ms |
4956 KB |
Output is correct |
3 |
Correct |
2 ms |
4956 KB |
Output is correct |
4 |
Correct |
2 ms |
4956 KB |
Output is correct |
5 |
Correct |
2 ms |
4956 KB |
Output is correct |
6 |
Correct |
2 ms |
4956 KB |
Output is correct |
7 |
Correct |
2 ms |
4956 KB |
Output is correct |
8 |
Correct |
2 ms |
4956 KB |
Output is correct |
9 |
Correct |
2 ms |
4956 KB |
Output is correct |
10 |
Correct |
2 ms |
4952 KB |
Output is correct |
11 |
Correct |
3 ms |
5120 KB |
Output is correct |
12 |
Correct |
2 ms |
4956 KB |
Output is correct |
13 |
Correct |
2 ms |
4972 KB |
Output is correct |
14 |
Correct |
2 ms |
4956 KB |
Output is correct |
15 |
Correct |
2 ms |
4956 KB |
Output is correct |
16 |
Correct |
2 ms |
4956 KB |
Output is correct |
17 |
Correct |
2 ms |
4956 KB |
Output is correct |
18 |
Correct |
2 ms |
4956 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
4956 KB |
Output is correct |
2 |
Correct |
2 ms |
4956 KB |
Output is correct |
3 |
Correct |
2 ms |
4956 KB |
Output is correct |
4 |
Correct |
2 ms |
4956 KB |
Output is correct |
5 |
Correct |
2 ms |
4956 KB |
Output is correct |
6 |
Correct |
2 ms |
4956 KB |
Output is correct |
7 |
Correct |
2 ms |
4956 KB |
Output is correct |
8 |
Correct |
2 ms |
4956 KB |
Output is correct |
9 |
Correct |
2 ms |
4956 KB |
Output is correct |
10 |
Correct |
2 ms |
4952 KB |
Output is correct |
11 |
Correct |
3 ms |
5120 KB |
Output is correct |
12 |
Correct |
2 ms |
4956 KB |
Output is correct |
13 |
Correct |
2 ms |
4972 KB |
Output is correct |
14 |
Correct |
2 ms |
4956 KB |
Output is correct |
15 |
Correct |
2 ms |
4956 KB |
Output is correct |
16 |
Correct |
2 ms |
4956 KB |
Output is correct |
17 |
Correct |
2 ms |
4956 KB |
Output is correct |
18 |
Correct |
2 ms |
4956 KB |
Output is correct |
19 |
Correct |
2 ms |
4956 KB |
Output is correct |
20 |
Correct |
2 ms |
4956 KB |
Output is correct |
21 |
Correct |
2 ms |
5212 KB |
Output is correct |
22 |
Correct |
113 ms |
8840 KB |
Output is correct |
23 |
Correct |
93 ms |
7972 KB |
Output is correct |
24 |
Correct |
97 ms |
8604 KB |
Output is correct |
25 |
Correct |
17 ms |
8284 KB |
Output is correct |
26 |
Correct |
37 ms |
6492 KB |
Output is correct |
27 |
Correct |
4 ms |
8284 KB |
Output is correct |
28 |
Correct |
14 ms |
6020 KB |
Output is correct |
29 |
Correct |
15 ms |
6492 KB |
Output is correct |
30 |
Correct |
15 ms |
6488 KB |
Output is correct |
31 |
Correct |
19 ms |
7768 KB |
Output is correct |
32 |
Correct |
16 ms |
8020 KB |
Output is correct |
33 |
Correct |
19 ms |
8308 KB |
Output is correct |
34 |
Correct |
23 ms |
7560 KB |
Output is correct |
35 |
Correct |
29 ms |
8284 KB |
Output is correct |
36 |
Correct |
24 ms |
8792 KB |
Output is correct |
37 |
Correct |
7 ms |
8284 KB |
Output is correct |
38 |
Correct |
6 ms |
7004 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
4956 KB |
Output is correct |
2 |
Correct |
2 ms |
4956 KB |
Output is correct |
3 |
Correct |
2 ms |
4956 KB |
Output is correct |
4 |
Correct |
2 ms |
4956 KB |
Output is correct |
5 |
Correct |
2 ms |
4956 KB |
Output is correct |
6 |
Correct |
2 ms |
4956 KB |
Output is correct |
7 |
Correct |
2 ms |
4956 KB |
Output is correct |
8 |
Correct |
2 ms |
4956 KB |
Output is correct |
9 |
Correct |
2 ms |
4956 KB |
Output is correct |
10 |
Correct |
2 ms |
4952 KB |
Output is correct |
11 |
Correct |
3 ms |
5120 KB |
Output is correct |
12 |
Correct |
2 ms |
4956 KB |
Output is correct |
13 |
Correct |
2 ms |
4972 KB |
Output is correct |
14 |
Correct |
2 ms |
4956 KB |
Output is correct |
15 |
Correct |
2 ms |
4956 KB |
Output is correct |
16 |
Correct |
2 ms |
4956 KB |
Output is correct |
17 |
Correct |
2 ms |
4956 KB |
Output is correct |
18 |
Correct |
2 ms |
4956 KB |
Output is correct |
19 |
Correct |
72 ms |
11996 KB |
Output is correct |
20 |
Correct |
71 ms |
12112 KB |
Output is correct |
21 |
Correct |
76 ms |
11996 KB |
Output is correct |
22 |
Correct |
68 ms |
12112 KB |
Output is correct |
23 |
Correct |
79 ms |
12368 KB |
Output is correct |
24 |
Correct |
33 ms |
12112 KB |
Output is correct |
25 |
Correct |
90 ms |
17216 KB |
Output is correct |
26 |
Correct |
64 ms |
17744 KB |
Output is correct |
27 |
Correct |
118 ms |
19432 KB |
Output is correct |
28 |
Correct |
182 ms |
29960 KB |
Output is correct |
29 |
Correct |
165 ms |
25628 KB |
Output is correct |
30 |
Correct |
111 ms |
19496 KB |
Output is correct |
31 |
Correct |
111 ms |
19260 KB |
Output is correct |
32 |
Correct |
107 ms |
19512 KB |
Output is correct |
33 |
Correct |
131 ms |
18308 KB |
Output is correct |
34 |
Correct |
115 ms |
19116 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
4956 KB |
Output is correct |
2 |
Correct |
2 ms |
4956 KB |
Output is correct |
3 |
Correct |
2 ms |
4956 KB |
Output is correct |
4 |
Correct |
2 ms |
4956 KB |
Output is correct |
5 |
Correct |
2 ms |
4956 KB |
Output is correct |
6 |
Correct |
2 ms |
4956 KB |
Output is correct |
7 |
Correct |
2 ms |
4956 KB |
Output is correct |
8 |
Correct |
2 ms |
4956 KB |
Output is correct |
9 |
Correct |
2 ms |
4956 KB |
Output is correct |
10 |
Correct |
2 ms |
4952 KB |
Output is correct |
11 |
Correct |
3 ms |
5120 KB |
Output is correct |
12 |
Correct |
2 ms |
4956 KB |
Output is correct |
13 |
Correct |
2 ms |
4972 KB |
Output is correct |
14 |
Correct |
2 ms |
4956 KB |
Output is correct |
15 |
Correct |
2 ms |
4956 KB |
Output is correct |
16 |
Correct |
2 ms |
4956 KB |
Output is correct |
17 |
Correct |
2 ms |
4956 KB |
Output is correct |
18 |
Correct |
2 ms |
4956 KB |
Output is correct |
19 |
Correct |
2 ms |
4956 KB |
Output is correct |
20 |
Correct |
2 ms |
4956 KB |
Output is correct |
21 |
Correct |
2 ms |
5212 KB |
Output is correct |
22 |
Correct |
113 ms |
8840 KB |
Output is correct |
23 |
Correct |
93 ms |
7972 KB |
Output is correct |
24 |
Correct |
97 ms |
8604 KB |
Output is correct |
25 |
Correct |
17 ms |
8284 KB |
Output is correct |
26 |
Correct |
37 ms |
6492 KB |
Output is correct |
27 |
Correct |
4 ms |
8284 KB |
Output is correct |
28 |
Correct |
14 ms |
6020 KB |
Output is correct |
29 |
Correct |
15 ms |
6492 KB |
Output is correct |
30 |
Correct |
15 ms |
6488 KB |
Output is correct |
31 |
Correct |
19 ms |
7768 KB |
Output is correct |
32 |
Correct |
16 ms |
8020 KB |
Output is correct |
33 |
Correct |
19 ms |
8308 KB |
Output is correct |
34 |
Correct |
23 ms |
7560 KB |
Output is correct |
35 |
Correct |
29 ms |
8284 KB |
Output is correct |
36 |
Correct |
24 ms |
8792 KB |
Output is correct |
37 |
Correct |
7 ms |
8284 KB |
Output is correct |
38 |
Correct |
6 ms |
7004 KB |
Output is correct |
39 |
Correct |
72 ms |
11996 KB |
Output is correct |
40 |
Correct |
71 ms |
12112 KB |
Output is correct |
41 |
Correct |
76 ms |
11996 KB |
Output is correct |
42 |
Correct |
68 ms |
12112 KB |
Output is correct |
43 |
Correct |
79 ms |
12368 KB |
Output is correct |
44 |
Correct |
33 ms |
12112 KB |
Output is correct |
45 |
Correct |
90 ms |
17216 KB |
Output is correct |
46 |
Correct |
64 ms |
17744 KB |
Output is correct |
47 |
Correct |
118 ms |
19432 KB |
Output is correct |
48 |
Correct |
182 ms |
29960 KB |
Output is correct |
49 |
Correct |
165 ms |
25628 KB |
Output is correct |
50 |
Correct |
111 ms |
19496 KB |
Output is correct |
51 |
Correct |
111 ms |
19260 KB |
Output is correct |
52 |
Correct |
107 ms |
19512 KB |
Output is correct |
53 |
Correct |
131 ms |
18308 KB |
Output is correct |
54 |
Correct |
115 ms |
19116 KB |
Output is correct |
55 |
Correct |
10 ms |
5724 KB |
Output is correct |
56 |
Correct |
10 ms |
5844 KB |
Output is correct |
57 |
Correct |
45 ms |
12324 KB |
Output is correct |
58 |
Correct |
34 ms |
11976 KB |
Output is correct |
59 |
Correct |
62 ms |
18540 KB |
Output is correct |
60 |
Correct |
271 ms |
29844 KB |
Output is correct |
61 |
Correct |
152 ms |
19540 KB |
Output is correct |
62 |
Correct |
129 ms |
23484 KB |
Output is correct |
63 |
Correct |
149 ms |
23328 KB |
Output is correct |
64 |
Correct |
743 ms |
21588 KB |
Output is correct |
65 |
Correct |
144 ms |
20276 KB |
Output is correct |
66 |
Correct |
524 ms |
33000 KB |
Output is correct |
67 |
Correct |
65 ms |
23924 KB |
Output is correct |
68 |
Correct |
276 ms |
24080 KB |
Output is correct |
69 |
Correct |
294 ms |
24332 KB |
Output is correct |
70 |
Correct |
264 ms |
23380 KB |
Output is correct |