// In the name of the God
#include <bits/stdc++.h>
#define ll long long
// #define int long long
#define pb push_back
#define F first
#define S second
#define mp make_pair
#define pii pair <int, int>
#define smin(x, y) (x) = min((x), (y))
#define smax(x, y) (x) = max((x), (y))
#define all(x) (x).begin(), (x).end()
using namespace std;
const int inf = 1e9+7;
const int mod = 998244353;
const int maxn = 2e5+5, maxk = 1e6+5;
int n, k, ans = inf, sz[maxn], mn[maxk];
bool mark[maxn];
vector <pii> adj[maxn], vec1;
vector <int> vec2;
void dfs1(int v, int p) {
sz[v] = 1;
for (pii e : adj[v]) {
int u = e.F;
if (u != p and !mark[u]) {
dfs1(u, v); sz[v] += sz[u];
}
}
}
int dfs2(int v, int s, int p) {
for (pii e : adj[v]) {
int u = e.F;
if (u != p and !mark[u] and sz[u] > s/2) return dfs2(u, s, v);
}
return v;
}
void dfs3(int v, int c, int h, int p) {
if (c > k) return;
vec2.pb(c); vec1.pb(mp(c, h));
smin(ans, mn[k-c]+h);
for (pii e : adj[v]) {
int u = e.F;
if (u != p and !mark[u]) dfs3(u, c+e.S, h+1, v);
}
}
void cd(int v) {
dfs1(v, v);
v = dfs2(v, sz[v], v);
mn[0] = 0; vec2.pb(0);
for (pii e : adj[v]) {
int u = e.F;
if (!mark[u]) {
dfs3(u, e.S, 1, v);
for (pii x : vec1) smin(mn[x.F], x.S);
vec1.clear();
}
}
for (int x : vec2) mn[x] = inf;
vec2.clear();
mark[v] = 1;
for (pii e : adj[v]) {
int u = e.F;
if (!mark[u]) cd(u);
}
}
int best_path(int N, int K, int H[][2], int L[]) {
n = N, k = K;
for (int i = 0; i < n-1; i++) {
adj[H[i][0]].pb(mp(H[i][1], L[i]));
adj[H[i][1]].pb(mp(H[i][0], L[i]));
}
fill(mn, mn+maxk, inf);
cd(0);
return ans == inf ? -1 : ans;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
12888 KB |
Output is correct |
2 |
Correct |
4 ms |
12888 KB |
Output is correct |
3 |
Correct |
4 ms |
12892 KB |
Output is correct |
4 |
Correct |
5 ms |
12892 KB |
Output is correct |
5 |
Correct |
3 ms |
12892 KB |
Output is correct |
6 |
Correct |
4 ms |
12892 KB |
Output is correct |
7 |
Correct |
4 ms |
12888 KB |
Output is correct |
8 |
Correct |
3 ms |
12892 KB |
Output is correct |
9 |
Correct |
3 ms |
12892 KB |
Output is correct |
10 |
Correct |
3 ms |
12892 KB |
Output is correct |
11 |
Correct |
3 ms |
12892 KB |
Output is correct |
12 |
Correct |
4 ms |
12904 KB |
Output is correct |
13 |
Correct |
3 ms |
12892 KB |
Output is correct |
14 |
Correct |
3 ms |
12892 KB |
Output is correct |
15 |
Correct |
3 ms |
12724 KB |
Output is correct |
16 |
Correct |
4 ms |
12892 KB |
Output is correct |
17 |
Correct |
3 ms |
12892 KB |
Output is correct |
18 |
Correct |
3 ms |
12888 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
12888 KB |
Output is correct |
2 |
Correct |
4 ms |
12888 KB |
Output is correct |
3 |
Correct |
4 ms |
12892 KB |
Output is correct |
4 |
Correct |
5 ms |
12892 KB |
Output is correct |
5 |
Correct |
3 ms |
12892 KB |
Output is correct |
6 |
Correct |
4 ms |
12892 KB |
Output is correct |
7 |
Correct |
4 ms |
12888 KB |
Output is correct |
8 |
Correct |
3 ms |
12892 KB |
Output is correct |
9 |
Correct |
3 ms |
12892 KB |
Output is correct |
10 |
Correct |
3 ms |
12892 KB |
Output is correct |
11 |
Correct |
3 ms |
12892 KB |
Output is correct |
12 |
Correct |
4 ms |
12904 KB |
Output is correct |
13 |
Correct |
3 ms |
12892 KB |
Output is correct |
14 |
Correct |
3 ms |
12892 KB |
Output is correct |
15 |
Correct |
3 ms |
12724 KB |
Output is correct |
16 |
Correct |
4 ms |
12892 KB |
Output is correct |
17 |
Correct |
3 ms |
12892 KB |
Output is correct |
18 |
Correct |
3 ms |
12888 KB |
Output is correct |
19 |
Correct |
4 ms |
12892 KB |
Output is correct |
20 |
Correct |
3 ms |
12888 KB |
Output is correct |
21 |
Correct |
3 ms |
12892 KB |
Output is correct |
22 |
Correct |
4 ms |
12892 KB |
Output is correct |
23 |
Correct |
4 ms |
12892 KB |
Output is correct |
24 |
Correct |
4 ms |
12892 KB |
Output is correct |
25 |
Correct |
5 ms |
12888 KB |
Output is correct |
26 |
Correct |
4 ms |
12892 KB |
Output is correct |
27 |
Correct |
4 ms |
12892 KB |
Output is correct |
28 |
Correct |
4 ms |
12892 KB |
Output is correct |
29 |
Correct |
4 ms |
12892 KB |
Output is correct |
30 |
Correct |
4 ms |
12892 KB |
Output is correct |
31 |
Correct |
4 ms |
13300 KB |
Output is correct |
32 |
Correct |
4 ms |
12892 KB |
Output is correct |
33 |
Correct |
4 ms |
12892 KB |
Output is correct |
34 |
Correct |
4 ms |
12892 KB |
Output is correct |
35 |
Correct |
4 ms |
12892 KB |
Output is correct |
36 |
Correct |
4 ms |
12888 KB |
Output is correct |
37 |
Correct |
4 ms |
12888 KB |
Output is correct |
38 |
Correct |
4 ms |
12892 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
12888 KB |
Output is correct |
2 |
Correct |
4 ms |
12888 KB |
Output is correct |
3 |
Correct |
4 ms |
12892 KB |
Output is correct |
4 |
Correct |
5 ms |
12892 KB |
Output is correct |
5 |
Correct |
3 ms |
12892 KB |
Output is correct |
6 |
Correct |
4 ms |
12892 KB |
Output is correct |
7 |
Correct |
4 ms |
12888 KB |
Output is correct |
8 |
Correct |
3 ms |
12892 KB |
Output is correct |
9 |
Correct |
3 ms |
12892 KB |
Output is correct |
10 |
Correct |
3 ms |
12892 KB |
Output is correct |
11 |
Correct |
3 ms |
12892 KB |
Output is correct |
12 |
Correct |
4 ms |
12904 KB |
Output is correct |
13 |
Correct |
3 ms |
12892 KB |
Output is correct |
14 |
Correct |
3 ms |
12892 KB |
Output is correct |
15 |
Correct |
3 ms |
12724 KB |
Output is correct |
16 |
Correct |
4 ms |
12892 KB |
Output is correct |
17 |
Correct |
3 ms |
12892 KB |
Output is correct |
18 |
Correct |
3 ms |
12888 KB |
Output is correct |
19 |
Correct |
81 ms |
19284 KB |
Output is correct |
20 |
Correct |
88 ms |
19272 KB |
Output is correct |
21 |
Correct |
80 ms |
19672 KB |
Output is correct |
22 |
Correct |
87 ms |
20096 KB |
Output is correct |
23 |
Correct |
48 ms |
19280 KB |
Output is correct |
24 |
Correct |
47 ms |
19260 KB |
Output is correct |
25 |
Correct |
83 ms |
22612 KB |
Output is correct |
26 |
Correct |
76 ms |
27456 KB |
Output is correct |
27 |
Correct |
161 ms |
23404 KB |
Output is correct |
28 |
Correct |
204 ms |
37608 KB |
Output is correct |
29 |
Correct |
264 ms |
36584 KB |
Output is correct |
30 |
Correct |
144 ms |
23496 KB |
Output is correct |
31 |
Correct |
130 ms |
23376 KB |
Output is correct |
32 |
Correct |
126 ms |
23376 KB |
Output is correct |
33 |
Correct |
139 ms |
22124 KB |
Output is correct |
34 |
Correct |
125 ms |
22276 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
12888 KB |
Output is correct |
2 |
Correct |
4 ms |
12888 KB |
Output is correct |
3 |
Correct |
4 ms |
12892 KB |
Output is correct |
4 |
Correct |
5 ms |
12892 KB |
Output is correct |
5 |
Correct |
3 ms |
12892 KB |
Output is correct |
6 |
Correct |
4 ms |
12892 KB |
Output is correct |
7 |
Correct |
4 ms |
12888 KB |
Output is correct |
8 |
Correct |
3 ms |
12892 KB |
Output is correct |
9 |
Correct |
3 ms |
12892 KB |
Output is correct |
10 |
Correct |
3 ms |
12892 KB |
Output is correct |
11 |
Correct |
3 ms |
12892 KB |
Output is correct |
12 |
Correct |
4 ms |
12904 KB |
Output is correct |
13 |
Correct |
3 ms |
12892 KB |
Output is correct |
14 |
Correct |
3 ms |
12892 KB |
Output is correct |
15 |
Correct |
3 ms |
12724 KB |
Output is correct |
16 |
Correct |
4 ms |
12892 KB |
Output is correct |
17 |
Correct |
3 ms |
12892 KB |
Output is correct |
18 |
Correct |
3 ms |
12888 KB |
Output is correct |
19 |
Correct |
4 ms |
12892 KB |
Output is correct |
20 |
Correct |
3 ms |
12888 KB |
Output is correct |
21 |
Correct |
3 ms |
12892 KB |
Output is correct |
22 |
Correct |
4 ms |
12892 KB |
Output is correct |
23 |
Correct |
4 ms |
12892 KB |
Output is correct |
24 |
Correct |
4 ms |
12892 KB |
Output is correct |
25 |
Correct |
5 ms |
12888 KB |
Output is correct |
26 |
Correct |
4 ms |
12892 KB |
Output is correct |
27 |
Correct |
4 ms |
12892 KB |
Output is correct |
28 |
Correct |
4 ms |
12892 KB |
Output is correct |
29 |
Correct |
4 ms |
12892 KB |
Output is correct |
30 |
Correct |
4 ms |
12892 KB |
Output is correct |
31 |
Correct |
4 ms |
13300 KB |
Output is correct |
32 |
Correct |
4 ms |
12892 KB |
Output is correct |
33 |
Correct |
4 ms |
12892 KB |
Output is correct |
34 |
Correct |
4 ms |
12892 KB |
Output is correct |
35 |
Correct |
4 ms |
12892 KB |
Output is correct |
36 |
Correct |
4 ms |
12888 KB |
Output is correct |
37 |
Correct |
4 ms |
12888 KB |
Output is correct |
38 |
Correct |
4 ms |
12892 KB |
Output is correct |
39 |
Correct |
81 ms |
19284 KB |
Output is correct |
40 |
Correct |
88 ms |
19272 KB |
Output is correct |
41 |
Correct |
80 ms |
19672 KB |
Output is correct |
42 |
Correct |
87 ms |
20096 KB |
Output is correct |
43 |
Correct |
48 ms |
19280 KB |
Output is correct |
44 |
Correct |
47 ms |
19260 KB |
Output is correct |
45 |
Correct |
83 ms |
22612 KB |
Output is correct |
46 |
Correct |
76 ms |
27456 KB |
Output is correct |
47 |
Correct |
161 ms |
23404 KB |
Output is correct |
48 |
Correct |
204 ms |
37608 KB |
Output is correct |
49 |
Correct |
264 ms |
36584 KB |
Output is correct |
50 |
Correct |
144 ms |
23496 KB |
Output is correct |
51 |
Correct |
130 ms |
23376 KB |
Output is correct |
52 |
Correct |
126 ms |
23376 KB |
Output is correct |
53 |
Correct |
139 ms |
22124 KB |
Output is correct |
54 |
Correct |
125 ms |
22276 KB |
Output is correct |
55 |
Correct |
9 ms |
13400 KB |
Output is correct |
56 |
Correct |
9 ms |
13404 KB |
Output is correct |
57 |
Correct |
58 ms |
20424 KB |
Output is correct |
58 |
Correct |
30 ms |
20180 KB |
Output is correct |
59 |
Correct |
71 ms |
27340 KB |
Output is correct |
60 |
Correct |
358 ms |
39144 KB |
Output is correct |
61 |
Correct |
130 ms |
23636 KB |
Output is correct |
62 |
Correct |
184 ms |
24576 KB |
Output is correct |
63 |
Correct |
210 ms |
24484 KB |
Output is correct |
64 |
Correct |
230 ms |
24992 KB |
Output is correct |
65 |
Correct |
157 ms |
23184 KB |
Output is correct |
66 |
Correct |
231 ms |
34176 KB |
Output is correct |
67 |
Correct |
81 ms |
25284 KB |
Output is correct |
68 |
Correct |
218 ms |
24268 KB |
Output is correct |
69 |
Correct |
208 ms |
24344 KB |
Output is correct |
70 |
Correct |
176 ms |
23788 KB |
Output is correct |