#include <bits/stdc++.h>
using namespace std;
template<class A, class B> bool maximize(A& x, B y) {if (x < y) return x = y, true; else return false;}
template<class A, class B> bool minimize(A& x, B y) {if (x > y) return x = y, true; else return false;}
#define all(a) a.begin(), a.end()
#define pb push_back
#define pf push_front
#define fi first
#define se second
//#define int long long
typedef long long ll;
typedef unsigned long long ull;
typedef double db;
typedef long double ld;
typedef pair<db, db> pdb;
typedef pair<ld, ld> pld;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef pair<ll, int> plli;
typedef pair<int, ll> pill;
const int MAX_N = 2e5 + 5;
const int mod = 1e9 + 7; // 998244353
const int inf = 1e9 + 1;
int n, k;
vector<pii> adj[MAX_N];
bool processed[MAX_N];
int ans = inf;
int child[MAX_N];
int dfs(int u, int pr = 0) {
child[u] = 1;
for (auto it : adj[u]) {
int v = it.fi;
if (!processed[v] && v != pr) {
child[u] += dfs(v, u);
}
}
return child[u];
}
int find_centroid(int u, int pr, int sz) {
for (auto it : adj[u]) {
int v = it.fi;
if (!processed[v] && v != pr && child[v] > sz) {
return find_centroid(v, u, sz);
}
}
return u;
}
const int MAX_VAL = 1e6 + 10;
int dp[MAX_VAL];
vector<int> used;
void go_cal(int u, int pr, bool ok, int len, int dep) {
if (k < len) return;
if (ok) {
used.pb(len);
minimize(dp[len], dep);
}
else {
minimize(ans, dp[k - len] + dep);
}
for (auto it : adj[u]) {
int v = it.fi;
int w = it.se;
if (!processed[v] && v != pr) {
go_cal(v, u, ok, len + w, dep + 1);
}
}
}
void centroid_decomposition(int u) {
int c = find_centroid(u, 0, dfs(u) >> 1);
processed[c] = true;
used.clear();
dp[0] = 0;
for (auto it : adj[c]) {
int v = it.fi;
int w = it.se;
if (!processed[v]) {
go_cal(v, c, false, w, 1);
go_cal(v, c, true, w, 1);
}
}
for (auto it : used) {
dp[it] = inf;
}
for (auto it : adj[c]) {
int v = it.fi;
if (!processed[v]) {
centroid_decomposition(v);
}
}
}
int best_path(int _n, int _k, int h[][2], int l[]) {
n = _n;
k = _k;
for (int i = 0; i <= k; i ++) {
dp[i] = inf;
}
for (int i = 0; i < n - 1; i++) {
int u = h[i][0], v = h[i][1], w = l[i];
u++; v++;
adj[u].pb({v, w});
adj[v].pb({u, w});
}
centroid_decomposition(1);
return (ans == inf ? -1 : ans);
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4948 KB |
Output is correct |
2 |
Correct |
3 ms |
4948 KB |
Output is correct |
3 |
Correct |
3 ms |
4948 KB |
Output is correct |
4 |
Correct |
3 ms |
4948 KB |
Output is correct |
5 |
Correct |
3 ms |
4948 KB |
Output is correct |
6 |
Correct |
3 ms |
4948 KB |
Output is correct |
7 |
Correct |
3 ms |
4948 KB |
Output is correct |
8 |
Correct |
3 ms |
4948 KB |
Output is correct |
9 |
Correct |
3 ms |
4948 KB |
Output is correct |
10 |
Correct |
5 ms |
4948 KB |
Output is correct |
11 |
Correct |
4 ms |
5012 KB |
Output is correct |
12 |
Correct |
3 ms |
4948 KB |
Output is correct |
13 |
Correct |
5 ms |
5016 KB |
Output is correct |
14 |
Correct |
4 ms |
5016 KB |
Output is correct |
15 |
Correct |
3 ms |
5016 KB |
Output is correct |
16 |
Correct |
4 ms |
4948 KB |
Output is correct |
17 |
Correct |
4 ms |
4948 KB |
Output is correct |
18 |
Correct |
3 ms |
4948 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4948 KB |
Output is correct |
2 |
Correct |
3 ms |
4948 KB |
Output is correct |
3 |
Correct |
3 ms |
4948 KB |
Output is correct |
4 |
Correct |
3 ms |
4948 KB |
Output is correct |
5 |
Correct |
3 ms |
4948 KB |
Output is correct |
6 |
Correct |
3 ms |
4948 KB |
Output is correct |
7 |
Correct |
3 ms |
4948 KB |
Output is correct |
8 |
Correct |
3 ms |
4948 KB |
Output is correct |
9 |
Correct |
3 ms |
4948 KB |
Output is correct |
10 |
Correct |
5 ms |
4948 KB |
Output is correct |
11 |
Correct |
4 ms |
5012 KB |
Output is correct |
12 |
Correct |
3 ms |
4948 KB |
Output is correct |
13 |
Correct |
5 ms |
5016 KB |
Output is correct |
14 |
Correct |
4 ms |
5016 KB |
Output is correct |
15 |
Correct |
3 ms |
5016 KB |
Output is correct |
16 |
Correct |
4 ms |
4948 KB |
Output is correct |
17 |
Correct |
4 ms |
4948 KB |
Output is correct |
18 |
Correct |
3 ms |
4948 KB |
Output is correct |
19 |
Correct |
4 ms |
5076 KB |
Output is correct |
20 |
Correct |
7 ms |
5012 KB |
Output is correct |
21 |
Correct |
4 ms |
5076 KB |
Output is correct |
22 |
Correct |
6 ms |
8612 KB |
Output is correct |
23 |
Correct |
5 ms |
7984 KB |
Output is correct |
24 |
Correct |
6 ms |
8404 KB |
Output is correct |
25 |
Correct |
6 ms |
8404 KB |
Output is correct |
26 |
Correct |
5 ms |
6304 KB |
Output is correct |
27 |
Correct |
5 ms |
8148 KB |
Output is correct |
28 |
Correct |
5 ms |
5792 KB |
Output is correct |
29 |
Correct |
5 ms |
6356 KB |
Output is correct |
30 |
Correct |
5 ms |
6440 KB |
Output is correct |
31 |
Correct |
5 ms |
7632 KB |
Output is correct |
32 |
Correct |
5 ms |
7788 KB |
Output is correct |
33 |
Correct |
5 ms |
8128 KB |
Output is correct |
34 |
Correct |
5 ms |
7384 KB |
Output is correct |
35 |
Correct |
6 ms |
8276 KB |
Output is correct |
36 |
Correct |
7 ms |
8660 KB |
Output is correct |
37 |
Correct |
5 ms |
8148 KB |
Output is correct |
38 |
Correct |
5 ms |
7092 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4948 KB |
Output is correct |
2 |
Correct |
3 ms |
4948 KB |
Output is correct |
3 |
Correct |
3 ms |
4948 KB |
Output is correct |
4 |
Correct |
3 ms |
4948 KB |
Output is correct |
5 |
Correct |
3 ms |
4948 KB |
Output is correct |
6 |
Correct |
3 ms |
4948 KB |
Output is correct |
7 |
Correct |
3 ms |
4948 KB |
Output is correct |
8 |
Correct |
3 ms |
4948 KB |
Output is correct |
9 |
Correct |
3 ms |
4948 KB |
Output is correct |
10 |
Correct |
5 ms |
4948 KB |
Output is correct |
11 |
Correct |
4 ms |
5012 KB |
Output is correct |
12 |
Correct |
3 ms |
4948 KB |
Output is correct |
13 |
Correct |
5 ms |
5016 KB |
Output is correct |
14 |
Correct |
4 ms |
5016 KB |
Output is correct |
15 |
Correct |
3 ms |
5016 KB |
Output is correct |
16 |
Correct |
4 ms |
4948 KB |
Output is correct |
17 |
Correct |
4 ms |
4948 KB |
Output is correct |
18 |
Correct |
3 ms |
4948 KB |
Output is correct |
19 |
Correct |
134 ms |
11904 KB |
Output is correct |
20 |
Correct |
114 ms |
11632 KB |
Output is correct |
21 |
Correct |
151 ms |
11976 KB |
Output is correct |
22 |
Correct |
105 ms |
12312 KB |
Output is correct |
23 |
Correct |
76 ms |
12028 KB |
Output is correct |
24 |
Correct |
58 ms |
11852 KB |
Output is correct |
25 |
Correct |
137 ms |
14484 KB |
Output is correct |
26 |
Correct |
120 ms |
17868 KB |
Output is correct |
27 |
Correct |
209 ms |
18840 KB |
Output is correct |
28 |
Correct |
324 ms |
30136 KB |
Output is correct |
29 |
Correct |
281 ms |
29132 KB |
Output is correct |
30 |
Correct |
163 ms |
18840 KB |
Output is correct |
31 |
Correct |
168 ms |
18840 KB |
Output is correct |
32 |
Correct |
205 ms |
18880 KB |
Output is correct |
33 |
Correct |
185 ms |
17644 KB |
Output is correct |
34 |
Correct |
173 ms |
18648 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4948 KB |
Output is correct |
2 |
Correct |
3 ms |
4948 KB |
Output is correct |
3 |
Correct |
3 ms |
4948 KB |
Output is correct |
4 |
Correct |
3 ms |
4948 KB |
Output is correct |
5 |
Correct |
3 ms |
4948 KB |
Output is correct |
6 |
Correct |
3 ms |
4948 KB |
Output is correct |
7 |
Correct |
3 ms |
4948 KB |
Output is correct |
8 |
Correct |
3 ms |
4948 KB |
Output is correct |
9 |
Correct |
3 ms |
4948 KB |
Output is correct |
10 |
Correct |
5 ms |
4948 KB |
Output is correct |
11 |
Correct |
4 ms |
5012 KB |
Output is correct |
12 |
Correct |
3 ms |
4948 KB |
Output is correct |
13 |
Correct |
5 ms |
5016 KB |
Output is correct |
14 |
Correct |
4 ms |
5016 KB |
Output is correct |
15 |
Correct |
3 ms |
5016 KB |
Output is correct |
16 |
Correct |
4 ms |
4948 KB |
Output is correct |
17 |
Correct |
4 ms |
4948 KB |
Output is correct |
18 |
Correct |
3 ms |
4948 KB |
Output is correct |
19 |
Correct |
4 ms |
5076 KB |
Output is correct |
20 |
Correct |
7 ms |
5012 KB |
Output is correct |
21 |
Correct |
4 ms |
5076 KB |
Output is correct |
22 |
Correct |
6 ms |
8612 KB |
Output is correct |
23 |
Correct |
5 ms |
7984 KB |
Output is correct |
24 |
Correct |
6 ms |
8404 KB |
Output is correct |
25 |
Correct |
6 ms |
8404 KB |
Output is correct |
26 |
Correct |
5 ms |
6304 KB |
Output is correct |
27 |
Correct |
5 ms |
8148 KB |
Output is correct |
28 |
Correct |
5 ms |
5792 KB |
Output is correct |
29 |
Correct |
5 ms |
6356 KB |
Output is correct |
30 |
Correct |
5 ms |
6440 KB |
Output is correct |
31 |
Correct |
5 ms |
7632 KB |
Output is correct |
32 |
Correct |
5 ms |
7788 KB |
Output is correct |
33 |
Correct |
5 ms |
8128 KB |
Output is correct |
34 |
Correct |
5 ms |
7384 KB |
Output is correct |
35 |
Correct |
6 ms |
8276 KB |
Output is correct |
36 |
Correct |
7 ms |
8660 KB |
Output is correct |
37 |
Correct |
5 ms |
8148 KB |
Output is correct |
38 |
Correct |
5 ms |
7092 KB |
Output is correct |
39 |
Correct |
134 ms |
11904 KB |
Output is correct |
40 |
Correct |
114 ms |
11632 KB |
Output is correct |
41 |
Correct |
151 ms |
11976 KB |
Output is correct |
42 |
Correct |
105 ms |
12312 KB |
Output is correct |
43 |
Correct |
76 ms |
12028 KB |
Output is correct |
44 |
Correct |
58 ms |
11852 KB |
Output is correct |
45 |
Correct |
137 ms |
14484 KB |
Output is correct |
46 |
Correct |
120 ms |
17868 KB |
Output is correct |
47 |
Correct |
209 ms |
18840 KB |
Output is correct |
48 |
Correct |
324 ms |
30136 KB |
Output is correct |
49 |
Correct |
281 ms |
29132 KB |
Output is correct |
50 |
Correct |
163 ms |
18840 KB |
Output is correct |
51 |
Correct |
168 ms |
18840 KB |
Output is correct |
52 |
Correct |
205 ms |
18880 KB |
Output is correct |
53 |
Correct |
185 ms |
17644 KB |
Output is correct |
54 |
Correct |
173 ms |
18648 KB |
Output is correct |
55 |
Correct |
10 ms |
5716 KB |
Output is correct |
56 |
Correct |
12 ms |
5716 KB |
Output is correct |
57 |
Correct |
76 ms |
12480 KB |
Output is correct |
58 |
Correct |
32 ms |
12568 KB |
Output is correct |
59 |
Correct |
86 ms |
18620 KB |
Output is correct |
60 |
Correct |
435 ms |
34244 KB |
Output is correct |
61 |
Correct |
163 ms |
19244 KB |
Output is correct |
62 |
Correct |
199 ms |
23720 KB |
Output is correct |
63 |
Correct |
250 ms |
23704 KB |
Output is correct |
64 |
Correct |
532 ms |
22040 KB |
Output is correct |
65 |
Correct |
201 ms |
19684 KB |
Output is correct |
66 |
Correct |
369 ms |
31304 KB |
Output is correct |
67 |
Correct |
109 ms |
25360 KB |
Output is correct |
68 |
Correct |
231 ms |
23956 KB |
Output is correct |
69 |
Correct |
246 ms |
24044 KB |
Output is correct |
70 |
Correct |
214 ms |
23320 KB |
Output is correct |