#include <bits/stdc++.h>
#define P pair<int, int>
#define x first
#define y second
using namespace std;
const int MAXN = 2e5 + 50, MAXK = 1e6 + 50;
int n, k, snode, curmx, ans, cnt, size[MAXN], dp[MAXK], mpath[MAXK];
bool chk[MAXN];
vector<P> g[MAXN];
int dfs(int u, int p) {
size[u] = 1;
for(auto v : g[u]) if(!chk[v.x] && v.x != p) {
dfs(v.x, u);
size[u] += size[v.x];
}
return size[u];
}
void findsen(int u, int p, int d) {
int mx = (d - size[u]);
for(auto v : g[u]) if(!chk[v.x] && v.x != p) {
findsen(v.x, u, d);
mx = max(mx, size[v.x]);
}
if(mx < curmx) snode = u, curmx = mx;
}
void dfs1(int u, int p, int cost, int len, bool fill) {
if(cost > k) return;
if(!fill) {
if(dp[k-cost] == cnt && (len + mpath[k-cost] < ans || ans == -1)) ans = len + mpath[k-cost];
if(cost == k && (len < ans || ans == -1)) ans = len;
} else if(dp[cost] < cnt || len < mpath[cost]) dp[cost] = cnt, mpath[cost] = len;
for(auto v : g[u]) if(!chk[v.x] && v.x != p) dfs1(v.x, u, cost + v.y, len + 1, fill);
}
void process(int u) {
++cnt;
if(dfs(u, -1) <= 1) return;
curmx = size[u] + 1;
findsen(u, -1, size[u]);
u = snode;
for(auto v : g[u]) if(!chk[v.x]) dfs1(v.x, snode, v.y, 1, 0), dfs1(v.x, snode, v.y, 1, 1);
chk[u] = true;
for(auto v : g[u]) if(!chk[v.x]) process(v.x);
}
int best_path(int _n, int _k, int h[][2], int l[]) {
n = _n, k = _k;
for(int i = 0; i < n-1; ++i) {
g[h[i][0]].emplace_back(h[i][1], l[i]);
g[h[i][1]].emplace_back(h[i][0], l[i]);
}
cnt = 0;
ans = -1;
process(0);
return ans;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
4984 KB |
Output is correct |
2 |
Correct |
6 ms |
5092 KB |
Output is correct |
3 |
Correct |
7 ms |
5212 KB |
Output is correct |
4 |
Correct |
6 ms |
5236 KB |
Output is correct |
5 |
Correct |
7 ms |
5236 KB |
Output is correct |
6 |
Correct |
6 ms |
5236 KB |
Output is correct |
7 |
Correct |
6 ms |
5236 KB |
Output is correct |
8 |
Correct |
6 ms |
5280 KB |
Output is correct |
9 |
Correct |
5 ms |
5280 KB |
Output is correct |
10 |
Correct |
6 ms |
5360 KB |
Output is correct |
11 |
Correct |
6 ms |
5372 KB |
Output is correct |
12 |
Correct |
7 ms |
5372 KB |
Output is correct |
13 |
Correct |
6 ms |
5372 KB |
Output is correct |
14 |
Correct |
6 ms |
5372 KB |
Output is correct |
15 |
Correct |
7 ms |
5372 KB |
Output is correct |
16 |
Correct |
5 ms |
5372 KB |
Output is correct |
17 |
Correct |
7 ms |
5372 KB |
Output is correct |
18 |
Correct |
6 ms |
5372 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
4984 KB |
Output is correct |
2 |
Correct |
6 ms |
5092 KB |
Output is correct |
3 |
Correct |
7 ms |
5212 KB |
Output is correct |
4 |
Correct |
6 ms |
5236 KB |
Output is correct |
5 |
Correct |
7 ms |
5236 KB |
Output is correct |
6 |
Correct |
6 ms |
5236 KB |
Output is correct |
7 |
Correct |
6 ms |
5236 KB |
Output is correct |
8 |
Correct |
6 ms |
5280 KB |
Output is correct |
9 |
Correct |
5 ms |
5280 KB |
Output is correct |
10 |
Correct |
6 ms |
5360 KB |
Output is correct |
11 |
Correct |
6 ms |
5372 KB |
Output is correct |
12 |
Correct |
7 ms |
5372 KB |
Output is correct |
13 |
Correct |
6 ms |
5372 KB |
Output is correct |
14 |
Correct |
6 ms |
5372 KB |
Output is correct |
15 |
Correct |
7 ms |
5372 KB |
Output is correct |
16 |
Correct |
5 ms |
5372 KB |
Output is correct |
17 |
Correct |
7 ms |
5372 KB |
Output is correct |
18 |
Correct |
6 ms |
5372 KB |
Output is correct |
19 |
Correct |
5 ms |
5372 KB |
Output is correct |
20 |
Correct |
6 ms |
5372 KB |
Output is correct |
21 |
Correct |
7 ms |
5372 KB |
Output is correct |
22 |
Correct |
10 ms |
10368 KB |
Output is correct |
23 |
Correct |
10 ms |
10368 KB |
Output is correct |
24 |
Correct |
12 ms |
11132 KB |
Output is correct |
25 |
Correct |
10 ms |
11132 KB |
Output is correct |
26 |
Correct |
9 ms |
11132 KB |
Output is correct |
27 |
Correct |
6 ms |
11132 KB |
Output is correct |
28 |
Correct |
8 ms |
11132 KB |
Output is correct |
29 |
Correct |
8 ms |
11132 KB |
Output is correct |
30 |
Correct |
10 ms |
11132 KB |
Output is correct |
31 |
Correct |
10 ms |
11132 KB |
Output is correct |
32 |
Correct |
12 ms |
11132 KB |
Output is correct |
33 |
Correct |
11 ms |
11132 KB |
Output is correct |
34 |
Correct |
11 ms |
11132 KB |
Output is correct |
35 |
Correct |
13 ms |
11260 KB |
Output is correct |
36 |
Correct |
12 ms |
11772 KB |
Output is correct |
37 |
Correct |
11 ms |
11772 KB |
Output is correct |
38 |
Correct |
11 ms |
11772 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
4984 KB |
Output is correct |
2 |
Correct |
6 ms |
5092 KB |
Output is correct |
3 |
Correct |
7 ms |
5212 KB |
Output is correct |
4 |
Correct |
6 ms |
5236 KB |
Output is correct |
5 |
Correct |
7 ms |
5236 KB |
Output is correct |
6 |
Correct |
6 ms |
5236 KB |
Output is correct |
7 |
Correct |
6 ms |
5236 KB |
Output is correct |
8 |
Correct |
6 ms |
5280 KB |
Output is correct |
9 |
Correct |
5 ms |
5280 KB |
Output is correct |
10 |
Correct |
6 ms |
5360 KB |
Output is correct |
11 |
Correct |
6 ms |
5372 KB |
Output is correct |
12 |
Correct |
7 ms |
5372 KB |
Output is correct |
13 |
Correct |
6 ms |
5372 KB |
Output is correct |
14 |
Correct |
6 ms |
5372 KB |
Output is correct |
15 |
Correct |
7 ms |
5372 KB |
Output is correct |
16 |
Correct |
5 ms |
5372 KB |
Output is correct |
17 |
Correct |
7 ms |
5372 KB |
Output is correct |
18 |
Correct |
6 ms |
5372 KB |
Output is correct |
19 |
Correct |
274 ms |
11772 KB |
Output is correct |
20 |
Correct |
179 ms |
11772 KB |
Output is correct |
21 |
Correct |
224 ms |
11772 KB |
Output is correct |
22 |
Correct |
202 ms |
11772 KB |
Output is correct |
23 |
Correct |
135 ms |
11772 KB |
Output is correct |
24 |
Correct |
113 ms |
11772 KB |
Output is correct |
25 |
Correct |
286 ms |
14324 KB |
Output is correct |
26 |
Correct |
122 ms |
18044 KB |
Output is correct |
27 |
Correct |
286 ms |
18044 KB |
Output is correct |
28 |
Correct |
658 ms |
30588 KB |
Output is correct |
29 |
Correct |
530 ms |
30588 KB |
Output is correct |
30 |
Correct |
335 ms |
30588 KB |
Output is correct |
31 |
Correct |
274 ms |
30588 KB |
Output is correct |
32 |
Correct |
326 ms |
30588 KB |
Output is correct |
33 |
Correct |
537 ms |
30588 KB |
Output is correct |
34 |
Correct |
522 ms |
30588 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
4984 KB |
Output is correct |
2 |
Correct |
6 ms |
5092 KB |
Output is correct |
3 |
Correct |
7 ms |
5212 KB |
Output is correct |
4 |
Correct |
6 ms |
5236 KB |
Output is correct |
5 |
Correct |
7 ms |
5236 KB |
Output is correct |
6 |
Correct |
6 ms |
5236 KB |
Output is correct |
7 |
Correct |
6 ms |
5236 KB |
Output is correct |
8 |
Correct |
6 ms |
5280 KB |
Output is correct |
9 |
Correct |
5 ms |
5280 KB |
Output is correct |
10 |
Correct |
6 ms |
5360 KB |
Output is correct |
11 |
Correct |
6 ms |
5372 KB |
Output is correct |
12 |
Correct |
7 ms |
5372 KB |
Output is correct |
13 |
Correct |
6 ms |
5372 KB |
Output is correct |
14 |
Correct |
6 ms |
5372 KB |
Output is correct |
15 |
Correct |
7 ms |
5372 KB |
Output is correct |
16 |
Correct |
5 ms |
5372 KB |
Output is correct |
17 |
Correct |
7 ms |
5372 KB |
Output is correct |
18 |
Correct |
6 ms |
5372 KB |
Output is correct |
19 |
Correct |
5 ms |
5372 KB |
Output is correct |
20 |
Correct |
6 ms |
5372 KB |
Output is correct |
21 |
Correct |
7 ms |
5372 KB |
Output is correct |
22 |
Correct |
10 ms |
10368 KB |
Output is correct |
23 |
Correct |
10 ms |
10368 KB |
Output is correct |
24 |
Correct |
12 ms |
11132 KB |
Output is correct |
25 |
Correct |
10 ms |
11132 KB |
Output is correct |
26 |
Correct |
9 ms |
11132 KB |
Output is correct |
27 |
Correct |
6 ms |
11132 KB |
Output is correct |
28 |
Correct |
8 ms |
11132 KB |
Output is correct |
29 |
Correct |
8 ms |
11132 KB |
Output is correct |
30 |
Correct |
10 ms |
11132 KB |
Output is correct |
31 |
Correct |
10 ms |
11132 KB |
Output is correct |
32 |
Correct |
12 ms |
11132 KB |
Output is correct |
33 |
Correct |
11 ms |
11132 KB |
Output is correct |
34 |
Correct |
11 ms |
11132 KB |
Output is correct |
35 |
Correct |
13 ms |
11260 KB |
Output is correct |
36 |
Correct |
12 ms |
11772 KB |
Output is correct |
37 |
Correct |
11 ms |
11772 KB |
Output is correct |
38 |
Correct |
11 ms |
11772 KB |
Output is correct |
39 |
Correct |
274 ms |
11772 KB |
Output is correct |
40 |
Correct |
179 ms |
11772 KB |
Output is correct |
41 |
Correct |
224 ms |
11772 KB |
Output is correct |
42 |
Correct |
202 ms |
11772 KB |
Output is correct |
43 |
Correct |
135 ms |
11772 KB |
Output is correct |
44 |
Correct |
113 ms |
11772 KB |
Output is correct |
45 |
Correct |
286 ms |
14324 KB |
Output is correct |
46 |
Correct |
122 ms |
18044 KB |
Output is correct |
47 |
Correct |
286 ms |
18044 KB |
Output is correct |
48 |
Correct |
658 ms |
30588 KB |
Output is correct |
49 |
Correct |
530 ms |
30588 KB |
Output is correct |
50 |
Correct |
335 ms |
30588 KB |
Output is correct |
51 |
Correct |
274 ms |
30588 KB |
Output is correct |
52 |
Correct |
326 ms |
30588 KB |
Output is correct |
53 |
Correct |
537 ms |
30588 KB |
Output is correct |
54 |
Correct |
522 ms |
30588 KB |
Output is correct |
55 |
Correct |
19 ms |
30588 KB |
Output is correct |
56 |
Correct |
20 ms |
30588 KB |
Output is correct |
57 |
Correct |
122 ms |
30588 KB |
Output is correct |
58 |
Correct |
52 ms |
30588 KB |
Output is correct |
59 |
Correct |
145 ms |
30588 KB |
Output is correct |
60 |
Correct |
823 ms |
34556 KB |
Output is correct |
61 |
Correct |
287 ms |
34556 KB |
Output is correct |
62 |
Correct |
293 ms |
34556 KB |
Output is correct |
63 |
Correct |
424 ms |
34556 KB |
Output is correct |
64 |
Correct |
938 ms |
34556 KB |
Output is correct |
65 |
Correct |
466 ms |
34556 KB |
Output is correct |
66 |
Correct |
898 ms |
34684 KB |
Output is correct |
67 |
Correct |
185 ms |
34684 KB |
Output is correct |
68 |
Correct |
473 ms |
34684 KB |
Output is correct |
69 |
Correct |
468 ms |
34684 KB |
Output is correct |
70 |
Correct |
428 ms |
34684 KB |
Output is correct |