#include <bits/stdc++.h>
#define taskname "test"
#define fi first
#define se second
#define pb push_back
#define faster ios_base::sync_with_stdio(0); cin.tie(0);
using namespace std;
using ll = long long;
using pii = pair <int, int>;
using pil = pair <int, ll>;
using pli = pair <ll, int>;
using pll = pair <ll, ll>;
using ull = unsigned ll;
mt19937 Rand(chrono::steady_clock::now().time_since_epoch().count());
ll min(const ll &a, const ll &b){
return (a < b) ? a : b;
}
ll max(const ll &a, const ll &b){
return (a > b) ? a : b;
}
//const ll Mod = 1000000009;
//const ll Mod2 = 999999999989;
//only use when required
const int maxN = 1e6 + 1;
const int inf = 0x3f3f3f3f;
int n, k;
bool visited[maxN];
int par[maxN];
int subtree_size[maxN];
int edge[maxN];
vector <pii> adj[maxN];
int cntt = 0, cnttt = 0, total = 0;
void centroid_dfs(int u, int p){
++cntt;
subtree_size[u] = 1;
for (const auto &v: adj[u]){
int i = v.fi;
if (visited[i] || i == p) continue;
centroid_dfs(i, u);
subtree_size[u] += subtree_size[i];
}
}
int find_centroid(int u, int p, int sz){
++cntt;
for (const auto &v: adj[u]){
int i = v.fi;
if (visited[i] || i == p) continue;
if (subtree_size[i] > sz / 2){
return find_centroid(i, u, sz);
}
}
return u;
}
//check if there is any "best path" traversing through the ROOT of the subtree(this case: centroid)
int find_path(int u, int p, int d, int cnt){ ++cntt;
if (k < d) return inf;
int tem = min(edge[k - d] + cnt, inf);
for (const auto &i: adj[u]){
if (visited[i.fi] || i.fi == p || i.se + d > k) continue;
tem = min(tem, find_path(i.fi, u, i.se + d, cnt + 1));
}
return tem;
}
void add_path(int u, int p, int d, int cnt){ ++cntt;
edge[d] = min(edge[d], cnt);
for (const auto &i: adj[u]){
if (visited[i.fi] || i.fi == p || i.se + d > k) continue;
add_path(i.fi, u, i.se + d, cnt + 1);
}
}
void delete_path(int u, int p, int d){
edge[d] = inf;
for (const auto &i: adj[u]){
if (visited[i.fi] || i.fi == p || i.se + d > k) continue;
delete_path(i.fi, u, i.se + d);
}
}
int ans = inf;
int build_centroid(int u){
cntt = cnttt = 0;
centroid_dfs(u, -1);
int centroid = find_centroid(u, -1, subtree_size[u]);
//cerr << cntt << " " << total << "\n";
visited[centroid] = 1;
edge[0] = 0;
for (const auto &i: adj[centroid]){
if (visited[i.fi]) continue;
ans = min(ans, find_path(i.fi, centroid, i.se, 1));
add_path(i.fi, centroid, i.se, 1);
}
delete_path(centroid, -1, 0);
for (const auto &i: adj[centroid]){
if (visited[i.fi]) continue;
par[build_centroid(i.fi)] = centroid;
}
return centroid;
}
int best_path(int N, int K, int H[][2], int L[]){
n = N; k = K;
for (int i = 0; i < n - 1; ++i){
int u = H[i][0], v = H[i][1];
adj[u + 1].pb({v + 1, L[i]});
adj[v + 1].pb({u + 1, L[i]});
}
memset(edge, 0x3f, sizeof(edge));
par[build_centroid(1)] = 0;
if (ans == inf) ans = -1;
return ans;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
15 ms |
27732 KB |
Output is correct |
2 |
Correct |
16 ms |
27708 KB |
Output is correct |
3 |
Correct |
16 ms |
27732 KB |
Output is correct |
4 |
Correct |
13 ms |
27644 KB |
Output is correct |
5 |
Correct |
14 ms |
27652 KB |
Output is correct |
6 |
Correct |
15 ms |
27732 KB |
Output is correct |
7 |
Correct |
14 ms |
27732 KB |
Output is correct |
8 |
Correct |
14 ms |
27708 KB |
Output is correct |
9 |
Correct |
14 ms |
27652 KB |
Output is correct |
10 |
Correct |
15 ms |
27732 KB |
Output is correct |
11 |
Correct |
16 ms |
27760 KB |
Output is correct |
12 |
Correct |
15 ms |
27636 KB |
Output is correct |
13 |
Correct |
14 ms |
27708 KB |
Output is correct |
14 |
Correct |
14 ms |
27756 KB |
Output is correct |
15 |
Correct |
14 ms |
27732 KB |
Output is correct |
16 |
Correct |
14 ms |
27732 KB |
Output is correct |
17 |
Correct |
14 ms |
27720 KB |
Output is correct |
18 |
Correct |
16 ms |
27704 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
15 ms |
27732 KB |
Output is correct |
2 |
Correct |
16 ms |
27708 KB |
Output is correct |
3 |
Correct |
16 ms |
27732 KB |
Output is correct |
4 |
Correct |
13 ms |
27644 KB |
Output is correct |
5 |
Correct |
14 ms |
27652 KB |
Output is correct |
6 |
Correct |
15 ms |
27732 KB |
Output is correct |
7 |
Correct |
14 ms |
27732 KB |
Output is correct |
8 |
Correct |
14 ms |
27708 KB |
Output is correct |
9 |
Correct |
14 ms |
27652 KB |
Output is correct |
10 |
Correct |
15 ms |
27732 KB |
Output is correct |
11 |
Correct |
16 ms |
27760 KB |
Output is correct |
12 |
Correct |
15 ms |
27636 KB |
Output is correct |
13 |
Correct |
14 ms |
27708 KB |
Output is correct |
14 |
Correct |
14 ms |
27756 KB |
Output is correct |
15 |
Correct |
14 ms |
27732 KB |
Output is correct |
16 |
Correct |
14 ms |
27732 KB |
Output is correct |
17 |
Correct |
14 ms |
27720 KB |
Output is correct |
18 |
Correct |
16 ms |
27704 KB |
Output is correct |
19 |
Correct |
13 ms |
27732 KB |
Output is correct |
20 |
Correct |
13 ms |
27732 KB |
Output is correct |
21 |
Correct |
14 ms |
27736 KB |
Output is correct |
22 |
Correct |
14 ms |
27688 KB |
Output is correct |
23 |
Correct |
15 ms |
27804 KB |
Output is correct |
24 |
Correct |
14 ms |
27796 KB |
Output is correct |
25 |
Correct |
14 ms |
27712 KB |
Output is correct |
26 |
Correct |
14 ms |
27732 KB |
Output is correct |
27 |
Correct |
15 ms |
27800 KB |
Output is correct |
28 |
Correct |
15 ms |
27812 KB |
Output is correct |
29 |
Correct |
15 ms |
27732 KB |
Output is correct |
30 |
Correct |
15 ms |
27792 KB |
Output is correct |
31 |
Correct |
14 ms |
27732 KB |
Output is correct |
32 |
Correct |
16 ms |
27732 KB |
Output is correct |
33 |
Correct |
16 ms |
27700 KB |
Output is correct |
34 |
Correct |
15 ms |
27712 KB |
Output is correct |
35 |
Correct |
16 ms |
27816 KB |
Output is correct |
36 |
Correct |
15 ms |
27724 KB |
Output is correct |
37 |
Correct |
17 ms |
27804 KB |
Output is correct |
38 |
Correct |
17 ms |
27732 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
15 ms |
27732 KB |
Output is correct |
2 |
Correct |
16 ms |
27708 KB |
Output is correct |
3 |
Correct |
16 ms |
27732 KB |
Output is correct |
4 |
Correct |
13 ms |
27644 KB |
Output is correct |
5 |
Correct |
14 ms |
27652 KB |
Output is correct |
6 |
Correct |
15 ms |
27732 KB |
Output is correct |
7 |
Correct |
14 ms |
27732 KB |
Output is correct |
8 |
Correct |
14 ms |
27708 KB |
Output is correct |
9 |
Correct |
14 ms |
27652 KB |
Output is correct |
10 |
Correct |
15 ms |
27732 KB |
Output is correct |
11 |
Correct |
16 ms |
27760 KB |
Output is correct |
12 |
Correct |
15 ms |
27636 KB |
Output is correct |
13 |
Correct |
14 ms |
27708 KB |
Output is correct |
14 |
Correct |
14 ms |
27756 KB |
Output is correct |
15 |
Correct |
14 ms |
27732 KB |
Output is correct |
16 |
Correct |
14 ms |
27732 KB |
Output is correct |
17 |
Correct |
14 ms |
27720 KB |
Output is correct |
18 |
Correct |
16 ms |
27704 KB |
Output is correct |
19 |
Correct |
133 ms |
33436 KB |
Output is correct |
20 |
Correct |
135 ms |
34700 KB |
Output is correct |
21 |
Correct |
181 ms |
34816 KB |
Output is correct |
22 |
Correct |
127 ms |
34908 KB |
Output is correct |
23 |
Correct |
78 ms |
35132 KB |
Output is correct |
24 |
Correct |
63 ms |
35016 KB |
Output is correct |
25 |
Correct |
142 ms |
38384 KB |
Output is correct |
26 |
Correct |
99 ms |
41980 KB |
Output is correct |
27 |
Correct |
159 ms |
42244 KB |
Output is correct |
28 |
Correct |
243 ms |
56716 KB |
Output is correct |
29 |
Correct |
256 ms |
55660 KB |
Output is correct |
30 |
Correct |
161 ms |
42244 KB |
Output is correct |
31 |
Correct |
193 ms |
42208 KB |
Output is correct |
32 |
Correct |
208 ms |
42464 KB |
Output is correct |
33 |
Correct |
203 ms |
41124 KB |
Output is correct |
34 |
Correct |
187 ms |
42076 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
15 ms |
27732 KB |
Output is correct |
2 |
Correct |
16 ms |
27708 KB |
Output is correct |
3 |
Correct |
16 ms |
27732 KB |
Output is correct |
4 |
Correct |
13 ms |
27644 KB |
Output is correct |
5 |
Correct |
14 ms |
27652 KB |
Output is correct |
6 |
Correct |
15 ms |
27732 KB |
Output is correct |
7 |
Correct |
14 ms |
27732 KB |
Output is correct |
8 |
Correct |
14 ms |
27708 KB |
Output is correct |
9 |
Correct |
14 ms |
27652 KB |
Output is correct |
10 |
Correct |
15 ms |
27732 KB |
Output is correct |
11 |
Correct |
16 ms |
27760 KB |
Output is correct |
12 |
Correct |
15 ms |
27636 KB |
Output is correct |
13 |
Correct |
14 ms |
27708 KB |
Output is correct |
14 |
Correct |
14 ms |
27756 KB |
Output is correct |
15 |
Correct |
14 ms |
27732 KB |
Output is correct |
16 |
Correct |
14 ms |
27732 KB |
Output is correct |
17 |
Correct |
14 ms |
27720 KB |
Output is correct |
18 |
Correct |
16 ms |
27704 KB |
Output is correct |
19 |
Correct |
13 ms |
27732 KB |
Output is correct |
20 |
Correct |
13 ms |
27732 KB |
Output is correct |
21 |
Correct |
14 ms |
27736 KB |
Output is correct |
22 |
Correct |
14 ms |
27688 KB |
Output is correct |
23 |
Correct |
15 ms |
27804 KB |
Output is correct |
24 |
Correct |
14 ms |
27796 KB |
Output is correct |
25 |
Correct |
14 ms |
27712 KB |
Output is correct |
26 |
Correct |
14 ms |
27732 KB |
Output is correct |
27 |
Correct |
15 ms |
27800 KB |
Output is correct |
28 |
Correct |
15 ms |
27812 KB |
Output is correct |
29 |
Correct |
15 ms |
27732 KB |
Output is correct |
30 |
Correct |
15 ms |
27792 KB |
Output is correct |
31 |
Correct |
14 ms |
27732 KB |
Output is correct |
32 |
Correct |
16 ms |
27732 KB |
Output is correct |
33 |
Correct |
16 ms |
27700 KB |
Output is correct |
34 |
Correct |
15 ms |
27712 KB |
Output is correct |
35 |
Correct |
16 ms |
27816 KB |
Output is correct |
36 |
Correct |
15 ms |
27724 KB |
Output is correct |
37 |
Correct |
17 ms |
27804 KB |
Output is correct |
38 |
Correct |
17 ms |
27732 KB |
Output is correct |
39 |
Correct |
133 ms |
33436 KB |
Output is correct |
40 |
Correct |
135 ms |
34700 KB |
Output is correct |
41 |
Correct |
181 ms |
34816 KB |
Output is correct |
42 |
Correct |
127 ms |
34908 KB |
Output is correct |
43 |
Correct |
78 ms |
35132 KB |
Output is correct |
44 |
Correct |
63 ms |
35016 KB |
Output is correct |
45 |
Correct |
142 ms |
38384 KB |
Output is correct |
46 |
Correct |
99 ms |
41980 KB |
Output is correct |
47 |
Correct |
159 ms |
42244 KB |
Output is correct |
48 |
Correct |
243 ms |
56716 KB |
Output is correct |
49 |
Correct |
256 ms |
55660 KB |
Output is correct |
50 |
Correct |
161 ms |
42244 KB |
Output is correct |
51 |
Correct |
193 ms |
42208 KB |
Output is correct |
52 |
Correct |
208 ms |
42464 KB |
Output is correct |
53 |
Correct |
203 ms |
41124 KB |
Output is correct |
54 |
Correct |
187 ms |
42076 KB |
Output is correct |
55 |
Correct |
21 ms |
28372 KB |
Output is correct |
56 |
Correct |
23 ms |
28416 KB |
Output is correct |
57 |
Correct |
94 ms |
35072 KB |
Output is correct |
58 |
Correct |
43 ms |
34752 KB |
Output is correct |
59 |
Correct |
105 ms |
41976 KB |
Output is correct |
60 |
Correct |
476 ms |
55892 KB |
Output is correct |
61 |
Correct |
178 ms |
42356 KB |
Output is correct |
62 |
Correct |
195 ms |
42272 KB |
Output is correct |
63 |
Correct |
290 ms |
42396 KB |
Output is correct |
64 |
Correct |
523 ms |
42636 KB |
Output is correct |
65 |
Correct |
255 ms |
43304 KB |
Output is correct |
66 |
Correct |
360 ms |
53188 KB |
Output is correct |
67 |
Correct |
110 ms |
42972 KB |
Output is correct |
68 |
Correct |
254 ms |
43132 KB |
Output is correct |
69 |
Correct |
249 ms |
43212 KB |
Output is correct |
70 |
Correct |
233 ms |
42460 KB |
Output is correct |