#pragma GCC optimize("Ofast")
#include <bits/stdc++.h>
#include "race.h"
using namespace std;
typedef long long int ll;
typedef pair<int, int> pii;
#define SZ(x) (int) x.size()
#define F first
#define S second
const int N = 2e5 + 10;
int S[N], M[N], n, k, ret = 1e9; vector<pii> adj[N]; unordered_map<int, int> mp;
void preDFS(int v, int p = -1) {
S[v] = 1;
for (pii u : adj[v]) {
if (u.F != p && !M[u.F]) preDFS(u.F, v), S[v] += S[u.F];
}
}
int centroid(int v, int s, int p = -1) {
for (pii u : adj[v]) {
if (!M[u.F] && u.F != p && 2 * S[u.F] > s) return centroid(u.F, s, v);
}
return v;
}
void add(int v, int iw, int p, int h = 1) {
if (mp.count(iw)) mp[iw] = min(mp[iw], h);
else mp[iw] = h;
for (pii u : adj[v]) {
if (!M[u.F] && u.F != p) add(u.F, iw + u.S, v, h + 1);
}
}
void calc(int v, int iw, int p, int h = 1) {
if (iw > k) return;
if (iw == k) ret = min(ret, h);
else if (mp.count(k - iw)) ret = min(ret, h + mp[k - iw]);
for (pii u : adj[v]) {
if (!M[u.F] && u.F != p) calc(u.F, iw + u.S, v, h + 1);
}
}
void solve(int v) {
for (pii u : adj[v]) {
if (M[u.F]) continue;
calc(u.F, u.S, v);
add(u.F, u.S, v);
}
}
void decompose(int v) {
preDFS(v);
v = centroid(v, S[v]);
// solve
solve(v);
mp = {};
M[v] = 1;
for (pii u : adj[v]) if (!M[u.F]) decompose(u.F);
}
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] + 1, v = h[i][1] + 1;
adj[u].push_back({v, l[i]});
adj[v].push_back({u, l[i]});
}
decompose(1);
return ret == 1e9 ? -1 : ret;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
5100 KB |
Output is correct |
2 |
Correct |
4 ms |
5100 KB |
Output is correct |
3 |
Correct |
4 ms |
5100 KB |
Output is correct |
4 |
Correct |
4 ms |
5100 KB |
Output is correct |
5 |
Correct |
4 ms |
5100 KB |
Output is correct |
6 |
Correct |
4 ms |
5100 KB |
Output is correct |
7 |
Correct |
4 ms |
5100 KB |
Output is correct |
8 |
Correct |
4 ms |
5100 KB |
Output is correct |
9 |
Correct |
4 ms |
5100 KB |
Output is correct |
10 |
Correct |
4 ms |
5100 KB |
Output is correct |
11 |
Correct |
5 ms |
5100 KB |
Output is correct |
12 |
Correct |
4 ms |
5100 KB |
Output is correct |
13 |
Correct |
4 ms |
5100 KB |
Output is correct |
14 |
Correct |
4 ms |
5100 KB |
Output is correct |
15 |
Correct |
4 ms |
5100 KB |
Output is correct |
16 |
Correct |
4 ms |
5100 KB |
Output is correct |
17 |
Correct |
4 ms |
5100 KB |
Output is correct |
18 |
Correct |
4 ms |
5100 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
5100 KB |
Output is correct |
2 |
Correct |
4 ms |
5100 KB |
Output is correct |
3 |
Correct |
4 ms |
5100 KB |
Output is correct |
4 |
Correct |
4 ms |
5100 KB |
Output is correct |
5 |
Correct |
4 ms |
5100 KB |
Output is correct |
6 |
Correct |
4 ms |
5100 KB |
Output is correct |
7 |
Correct |
4 ms |
5100 KB |
Output is correct |
8 |
Correct |
4 ms |
5100 KB |
Output is correct |
9 |
Correct |
4 ms |
5100 KB |
Output is correct |
10 |
Correct |
4 ms |
5100 KB |
Output is correct |
11 |
Correct |
5 ms |
5100 KB |
Output is correct |
12 |
Correct |
4 ms |
5100 KB |
Output is correct |
13 |
Correct |
4 ms |
5100 KB |
Output is correct |
14 |
Correct |
4 ms |
5100 KB |
Output is correct |
15 |
Correct |
4 ms |
5100 KB |
Output is correct |
16 |
Correct |
4 ms |
5100 KB |
Output is correct |
17 |
Correct |
4 ms |
5100 KB |
Output is correct |
18 |
Correct |
4 ms |
5100 KB |
Output is correct |
19 |
Correct |
4 ms |
5100 KB |
Output is correct |
20 |
Correct |
4 ms |
5100 KB |
Output is correct |
21 |
Correct |
5 ms |
5100 KB |
Output is correct |
22 |
Correct |
5 ms |
5100 KB |
Output is correct |
23 |
Correct |
5 ms |
5100 KB |
Output is correct |
24 |
Correct |
5 ms |
5100 KB |
Output is correct |
25 |
Correct |
5 ms |
5100 KB |
Output is correct |
26 |
Correct |
6 ms |
5100 KB |
Output is correct |
27 |
Correct |
5 ms |
5100 KB |
Output is correct |
28 |
Correct |
6 ms |
5100 KB |
Output is correct |
29 |
Correct |
5 ms |
5100 KB |
Output is correct |
30 |
Correct |
5 ms |
5100 KB |
Output is correct |
31 |
Correct |
5 ms |
5100 KB |
Output is correct |
32 |
Correct |
6 ms |
5100 KB |
Output is correct |
33 |
Correct |
6 ms |
5100 KB |
Output is correct |
34 |
Correct |
6 ms |
5100 KB |
Output is correct |
35 |
Correct |
5 ms |
5100 KB |
Output is correct |
36 |
Correct |
6 ms |
5100 KB |
Output is correct |
37 |
Correct |
5 ms |
5100 KB |
Output is correct |
38 |
Correct |
5 ms |
5120 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
5100 KB |
Output is correct |
2 |
Correct |
4 ms |
5100 KB |
Output is correct |
3 |
Correct |
4 ms |
5100 KB |
Output is correct |
4 |
Correct |
4 ms |
5100 KB |
Output is correct |
5 |
Correct |
4 ms |
5100 KB |
Output is correct |
6 |
Correct |
4 ms |
5100 KB |
Output is correct |
7 |
Correct |
4 ms |
5100 KB |
Output is correct |
8 |
Correct |
4 ms |
5100 KB |
Output is correct |
9 |
Correct |
4 ms |
5100 KB |
Output is correct |
10 |
Correct |
4 ms |
5100 KB |
Output is correct |
11 |
Correct |
5 ms |
5100 KB |
Output is correct |
12 |
Correct |
4 ms |
5100 KB |
Output is correct |
13 |
Correct |
4 ms |
5100 KB |
Output is correct |
14 |
Correct |
4 ms |
5100 KB |
Output is correct |
15 |
Correct |
4 ms |
5100 KB |
Output is correct |
16 |
Correct |
4 ms |
5100 KB |
Output is correct |
17 |
Correct |
4 ms |
5100 KB |
Output is correct |
18 |
Correct |
4 ms |
5100 KB |
Output is correct |
19 |
Correct |
214 ms |
10788 KB |
Output is correct |
20 |
Correct |
225 ms |
10732 KB |
Output is correct |
21 |
Correct |
227 ms |
10732 KB |
Output is correct |
22 |
Correct |
201 ms |
10732 KB |
Output is correct |
23 |
Correct |
227 ms |
10988 KB |
Output is correct |
24 |
Correct |
113 ms |
10988 KB |
Output is correct |
25 |
Correct |
406 ms |
14572 KB |
Output is correct |
26 |
Correct |
133 ms |
17900 KB |
Output is correct |
27 |
Correct |
482 ms |
16856 KB |
Output is correct |
28 |
Execution timed out |
3036 ms |
38964 KB |
Time limit exceeded |
29 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
5100 KB |
Output is correct |
2 |
Correct |
4 ms |
5100 KB |
Output is correct |
3 |
Correct |
4 ms |
5100 KB |
Output is correct |
4 |
Correct |
4 ms |
5100 KB |
Output is correct |
5 |
Correct |
4 ms |
5100 KB |
Output is correct |
6 |
Correct |
4 ms |
5100 KB |
Output is correct |
7 |
Correct |
4 ms |
5100 KB |
Output is correct |
8 |
Correct |
4 ms |
5100 KB |
Output is correct |
9 |
Correct |
4 ms |
5100 KB |
Output is correct |
10 |
Correct |
4 ms |
5100 KB |
Output is correct |
11 |
Correct |
5 ms |
5100 KB |
Output is correct |
12 |
Correct |
4 ms |
5100 KB |
Output is correct |
13 |
Correct |
4 ms |
5100 KB |
Output is correct |
14 |
Correct |
4 ms |
5100 KB |
Output is correct |
15 |
Correct |
4 ms |
5100 KB |
Output is correct |
16 |
Correct |
4 ms |
5100 KB |
Output is correct |
17 |
Correct |
4 ms |
5100 KB |
Output is correct |
18 |
Correct |
4 ms |
5100 KB |
Output is correct |
19 |
Correct |
4 ms |
5100 KB |
Output is correct |
20 |
Correct |
4 ms |
5100 KB |
Output is correct |
21 |
Correct |
5 ms |
5100 KB |
Output is correct |
22 |
Correct |
5 ms |
5100 KB |
Output is correct |
23 |
Correct |
5 ms |
5100 KB |
Output is correct |
24 |
Correct |
5 ms |
5100 KB |
Output is correct |
25 |
Correct |
5 ms |
5100 KB |
Output is correct |
26 |
Correct |
6 ms |
5100 KB |
Output is correct |
27 |
Correct |
5 ms |
5100 KB |
Output is correct |
28 |
Correct |
6 ms |
5100 KB |
Output is correct |
29 |
Correct |
5 ms |
5100 KB |
Output is correct |
30 |
Correct |
5 ms |
5100 KB |
Output is correct |
31 |
Correct |
5 ms |
5100 KB |
Output is correct |
32 |
Correct |
6 ms |
5100 KB |
Output is correct |
33 |
Correct |
6 ms |
5100 KB |
Output is correct |
34 |
Correct |
6 ms |
5100 KB |
Output is correct |
35 |
Correct |
5 ms |
5100 KB |
Output is correct |
36 |
Correct |
6 ms |
5100 KB |
Output is correct |
37 |
Correct |
5 ms |
5100 KB |
Output is correct |
38 |
Correct |
5 ms |
5120 KB |
Output is correct |
39 |
Correct |
214 ms |
10788 KB |
Output is correct |
40 |
Correct |
225 ms |
10732 KB |
Output is correct |
41 |
Correct |
227 ms |
10732 KB |
Output is correct |
42 |
Correct |
201 ms |
10732 KB |
Output is correct |
43 |
Correct |
227 ms |
10988 KB |
Output is correct |
44 |
Correct |
113 ms |
10988 KB |
Output is correct |
45 |
Correct |
406 ms |
14572 KB |
Output is correct |
46 |
Correct |
133 ms |
17900 KB |
Output is correct |
47 |
Correct |
482 ms |
16856 KB |
Output is correct |
48 |
Execution timed out |
3036 ms |
38964 KB |
Time limit exceeded |
49 |
Halted |
0 ms |
0 KB |
- |