#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define pii pair<int,int>
#define pll pair<ll,ll>
#define fi first
#define se second
#define mp make_pair
#define pb push_back
#define eb emplace_back
#define all(vr) vr.begin(),vr.end()
#define vi vector<int>
#define vll vector<ll>
const int N = 2e5 + 10;
vector<pii> G[N];
int res, mark[N], sz[N], p[N];
void dfs_cen(int u, int r)
{
sz[u] = 1;
for (pii e : G[u])
if (e.fi != r && !mark[e.fi]) p[e.fi] = u, dfs_cen(e.fi, u), sz[u] += sz[e.fi];
}
int h[N];
ll d[N];
void dfs(int u, int r, vi &vt)
{
vt.eb(u);
for (pii e : G[u])
if (e.fi != r && !mark[e.fi])
{
h[e.fi] = h[u] + 1;
d[e.fi] = d[u] + e.se;
dfs(e.fi, u, vt);
}
}
void build_centroid(vi &vt, int K)
{
int len = vt.size();
dfs_cen(vt[0], 0);
int root = vt[0];
for (int x : vt)
{
int mx = len - sz[x];
for (pii e : G[x])
if (!mark[e.fi] && e.fi != p[x]) mx = max(mx, sz[e.fi]);
if (mx <= len / 2) root = x;
}
mark[root] = 1;
vector<vi> vt_sub;
map<ll,int> best;
best[0] = 0;
h[root] = 0;
for (pii e : G[root])
if (!mark[e.fi])
{
h[e.fi] = 1;
d[e.fi] = e.se;
vi s;
dfs(e.fi, root, s);
for (int x : s)
if (d[x] <= K && best.count(K - d[x])) res = min(res, h[x] + best[K - d[x]]);
for (int x : s)
if (!best.count(d[x])) best[d[x]] = h[x];
else best[d[x]] = min(best[d[x]], h[x]);
vt_sub.eb(s);
}
for (vi s : vt_sub) build_centroid(s, K);
}
int best_path(int n, int K, int H[][2], int L[])
{
for (int i = 1; i <= n; ++i) G[i].clear(), mark[i] = 0;
for (int i = 0; i < n - 1; ++i)
{
int u = H[i][0] + 1;
int v = H[i][1] + 1;
int w = L[i];
G[u].eb(v,w);
G[v].eb(u,w);
}
res = 1e9;
vi vt;
for (int i = 1; i <= n; ++i) vt.eb(i);
build_centroid(vt, K);
if (res == 1e9) res = -1;
return res;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4940 KB |
Output is correct |
2 |
Correct |
3 ms |
4940 KB |
Output is correct |
3 |
Correct |
3 ms |
4940 KB |
Output is correct |
4 |
Correct |
3 ms |
5000 KB |
Output is correct |
5 |
Correct |
4 ms |
4940 KB |
Output is correct |
6 |
Correct |
4 ms |
4940 KB |
Output is correct |
7 |
Correct |
3 ms |
5000 KB |
Output is correct |
8 |
Correct |
3 ms |
5068 KB |
Output is correct |
9 |
Correct |
3 ms |
4988 KB |
Output is correct |
10 |
Correct |
3 ms |
5068 KB |
Output is correct |
11 |
Correct |
3 ms |
5004 KB |
Output is correct |
12 |
Correct |
4 ms |
5068 KB |
Output is correct |
13 |
Correct |
4 ms |
4940 KB |
Output is correct |
14 |
Correct |
3 ms |
5068 KB |
Output is correct |
15 |
Correct |
3 ms |
4940 KB |
Output is correct |
16 |
Correct |
3 ms |
5000 KB |
Output is correct |
17 |
Correct |
3 ms |
4992 KB |
Output is correct |
18 |
Correct |
3 ms |
4940 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4940 KB |
Output is correct |
2 |
Correct |
3 ms |
4940 KB |
Output is correct |
3 |
Correct |
3 ms |
4940 KB |
Output is correct |
4 |
Correct |
3 ms |
5000 KB |
Output is correct |
5 |
Correct |
4 ms |
4940 KB |
Output is correct |
6 |
Correct |
4 ms |
4940 KB |
Output is correct |
7 |
Correct |
3 ms |
5000 KB |
Output is correct |
8 |
Correct |
3 ms |
5068 KB |
Output is correct |
9 |
Correct |
3 ms |
4988 KB |
Output is correct |
10 |
Correct |
3 ms |
5068 KB |
Output is correct |
11 |
Correct |
3 ms |
5004 KB |
Output is correct |
12 |
Correct |
4 ms |
5068 KB |
Output is correct |
13 |
Correct |
4 ms |
4940 KB |
Output is correct |
14 |
Correct |
3 ms |
5068 KB |
Output is correct |
15 |
Correct |
3 ms |
4940 KB |
Output is correct |
16 |
Correct |
3 ms |
5000 KB |
Output is correct |
17 |
Correct |
3 ms |
4992 KB |
Output is correct |
18 |
Correct |
3 ms |
4940 KB |
Output is correct |
19 |
Correct |
3 ms |
4940 KB |
Output is correct |
20 |
Correct |
3 ms |
4940 KB |
Output is correct |
21 |
Correct |
5 ms |
5068 KB |
Output is correct |
22 |
Correct |
6 ms |
5196 KB |
Output is correct |
23 |
Correct |
6 ms |
5272 KB |
Output is correct |
24 |
Correct |
5 ms |
5208 KB |
Output is correct |
25 |
Correct |
5 ms |
5196 KB |
Output is correct |
26 |
Correct |
5 ms |
5196 KB |
Output is correct |
27 |
Correct |
5 ms |
5068 KB |
Output is correct |
28 |
Correct |
5 ms |
5196 KB |
Output is correct |
29 |
Correct |
5 ms |
5196 KB |
Output is correct |
30 |
Correct |
5 ms |
5144 KB |
Output is correct |
31 |
Correct |
5 ms |
5196 KB |
Output is correct |
32 |
Correct |
5 ms |
5196 KB |
Output is correct |
33 |
Correct |
6 ms |
5196 KB |
Output is correct |
34 |
Correct |
5 ms |
5196 KB |
Output is correct |
35 |
Correct |
6 ms |
5260 KB |
Output is correct |
36 |
Correct |
5 ms |
5132 KB |
Output is correct |
37 |
Correct |
6 ms |
5196 KB |
Output is correct |
38 |
Correct |
5 ms |
5196 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4940 KB |
Output is correct |
2 |
Correct |
3 ms |
4940 KB |
Output is correct |
3 |
Correct |
3 ms |
4940 KB |
Output is correct |
4 |
Correct |
3 ms |
5000 KB |
Output is correct |
5 |
Correct |
4 ms |
4940 KB |
Output is correct |
6 |
Correct |
4 ms |
4940 KB |
Output is correct |
7 |
Correct |
3 ms |
5000 KB |
Output is correct |
8 |
Correct |
3 ms |
5068 KB |
Output is correct |
9 |
Correct |
3 ms |
4988 KB |
Output is correct |
10 |
Correct |
3 ms |
5068 KB |
Output is correct |
11 |
Correct |
3 ms |
5004 KB |
Output is correct |
12 |
Correct |
4 ms |
5068 KB |
Output is correct |
13 |
Correct |
4 ms |
4940 KB |
Output is correct |
14 |
Correct |
3 ms |
5068 KB |
Output is correct |
15 |
Correct |
3 ms |
4940 KB |
Output is correct |
16 |
Correct |
3 ms |
5000 KB |
Output is correct |
17 |
Correct |
3 ms |
4992 KB |
Output is correct |
18 |
Correct |
3 ms |
4940 KB |
Output is correct |
19 |
Correct |
395 ms |
14332 KB |
Output is correct |
20 |
Correct |
370 ms |
14528 KB |
Output is correct |
21 |
Correct |
366 ms |
14488 KB |
Output is correct |
22 |
Correct |
363 ms |
14472 KB |
Output is correct |
23 |
Correct |
466 ms |
14664 KB |
Output is correct |
24 |
Correct |
198 ms |
14028 KB |
Output is correct |
25 |
Correct |
315 ms |
18768 KB |
Output is correct |
26 |
Correct |
155 ms |
21820 KB |
Output is correct |
27 |
Correct |
418 ms |
22232 KB |
Output is correct |
28 |
Correct |
1183 ms |
62520 KB |
Output is correct |
29 |
Correct |
1153 ms |
61360 KB |
Output is correct |
30 |
Correct |
410 ms |
22220 KB |
Output is correct |
31 |
Correct |
464 ms |
22204 KB |
Output is correct |
32 |
Correct |
494 ms |
22156 KB |
Output is correct |
33 |
Correct |
734 ms |
22816 KB |
Output is correct |
34 |
Correct |
1054 ms |
43968 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4940 KB |
Output is correct |
2 |
Correct |
3 ms |
4940 KB |
Output is correct |
3 |
Correct |
3 ms |
4940 KB |
Output is correct |
4 |
Correct |
3 ms |
5000 KB |
Output is correct |
5 |
Correct |
4 ms |
4940 KB |
Output is correct |
6 |
Correct |
4 ms |
4940 KB |
Output is correct |
7 |
Correct |
3 ms |
5000 KB |
Output is correct |
8 |
Correct |
3 ms |
5068 KB |
Output is correct |
9 |
Correct |
3 ms |
4988 KB |
Output is correct |
10 |
Correct |
3 ms |
5068 KB |
Output is correct |
11 |
Correct |
3 ms |
5004 KB |
Output is correct |
12 |
Correct |
4 ms |
5068 KB |
Output is correct |
13 |
Correct |
4 ms |
4940 KB |
Output is correct |
14 |
Correct |
3 ms |
5068 KB |
Output is correct |
15 |
Correct |
3 ms |
4940 KB |
Output is correct |
16 |
Correct |
3 ms |
5000 KB |
Output is correct |
17 |
Correct |
3 ms |
4992 KB |
Output is correct |
18 |
Correct |
3 ms |
4940 KB |
Output is correct |
19 |
Correct |
3 ms |
4940 KB |
Output is correct |
20 |
Correct |
3 ms |
4940 KB |
Output is correct |
21 |
Correct |
5 ms |
5068 KB |
Output is correct |
22 |
Correct |
6 ms |
5196 KB |
Output is correct |
23 |
Correct |
6 ms |
5272 KB |
Output is correct |
24 |
Correct |
5 ms |
5208 KB |
Output is correct |
25 |
Correct |
5 ms |
5196 KB |
Output is correct |
26 |
Correct |
5 ms |
5196 KB |
Output is correct |
27 |
Correct |
5 ms |
5068 KB |
Output is correct |
28 |
Correct |
5 ms |
5196 KB |
Output is correct |
29 |
Correct |
5 ms |
5196 KB |
Output is correct |
30 |
Correct |
5 ms |
5144 KB |
Output is correct |
31 |
Correct |
5 ms |
5196 KB |
Output is correct |
32 |
Correct |
5 ms |
5196 KB |
Output is correct |
33 |
Correct |
6 ms |
5196 KB |
Output is correct |
34 |
Correct |
5 ms |
5196 KB |
Output is correct |
35 |
Correct |
6 ms |
5260 KB |
Output is correct |
36 |
Correct |
5 ms |
5132 KB |
Output is correct |
37 |
Correct |
6 ms |
5196 KB |
Output is correct |
38 |
Correct |
5 ms |
5196 KB |
Output is correct |
39 |
Correct |
395 ms |
14332 KB |
Output is correct |
40 |
Correct |
370 ms |
14528 KB |
Output is correct |
41 |
Correct |
366 ms |
14488 KB |
Output is correct |
42 |
Correct |
363 ms |
14472 KB |
Output is correct |
43 |
Correct |
466 ms |
14664 KB |
Output is correct |
44 |
Correct |
198 ms |
14028 KB |
Output is correct |
45 |
Correct |
315 ms |
18768 KB |
Output is correct |
46 |
Correct |
155 ms |
21820 KB |
Output is correct |
47 |
Correct |
418 ms |
22232 KB |
Output is correct |
48 |
Correct |
1183 ms |
62520 KB |
Output is correct |
49 |
Correct |
1153 ms |
61360 KB |
Output is correct |
50 |
Correct |
410 ms |
22220 KB |
Output is correct |
51 |
Correct |
464 ms |
22204 KB |
Output is correct |
52 |
Correct |
494 ms |
22156 KB |
Output is correct |
53 |
Correct |
734 ms |
22816 KB |
Output is correct |
54 |
Correct |
1054 ms |
43968 KB |
Output is correct |
55 |
Correct |
33 ms |
6788 KB |
Output is correct |
56 |
Correct |
25 ms |
5964 KB |
Output is correct |
57 |
Correct |
203 ms |
14400 KB |
Output is correct |
58 |
Correct |
74 ms |
18988 KB |
Output is correct |
59 |
Correct |
412 ms |
31000 KB |
Output is correct |
60 |
Correct |
1261 ms |
62096 KB |
Output is correct |
61 |
Correct |
469 ms |
26404 KB |
Output is correct |
62 |
Correct |
401 ms |
24592 KB |
Output is correct |
63 |
Correct |
523 ms |
24640 KB |
Output is correct |
64 |
Correct |
1236 ms |
40552 KB |
Output is correct |
65 |
Correct |
1032 ms |
47964 KB |
Output is correct |
66 |
Correct |
1213 ms |
61544 KB |
Output is correct |
67 |
Correct |
235 ms |
34776 KB |
Output is correct |
68 |
Correct |
655 ms |
41376 KB |
Output is correct |
69 |
Correct |
656 ms |
42108 KB |
Output is correct |
70 |
Correct |
612 ms |
39988 KB |
Output is correct |