#include <bits/stdc++.h>
using namespace std;
#define ll long long
// const ll MOD = 998244353;
const ll MOD = 1e9 + 7;
const ll NN = 2e5 + 2;
const ll KK = 1e6 + 2;
ll n, k, ans = NN;
vector<ll> is_dead(NN, 0), cnt(KK, -1), cnt1(KK, -1), sz(NN);
vector<vector<ll>> edges(NN);
map<pair<ll,ll>, ll> w;
set<ll> s, s1;
void comp_sz (ll node, ll parent) {
sz[node] = 1;
for (ll child : edges[node]) {
if (child == parent || is_dead[child]) continue;
comp_sz(child, node);
sz[node] += sz[child];
}
}
void dfs(ll node, ll parent, ll dist, ll lvl) {
if (dist < KK) s1.insert(dist), cnt1[dist] = (cnt1[dist] == -1 ? lvl : min(cnt1[dist], lvl));
for (ll child : edges[node]) {
if (child == parent || is_dead[child]) continue;
dfs(child, node, dist + w[make_pair(child, node)], lvl + 1);
}
}
void find_cent (ll node, ll parent, ll size) {
for (ll child : edges[node]) {
if (child == parent || is_dead[child]) continue;
if (sz[child] * 2 > size) {
find_cent(child, node, size);
return;
}
}
s.insert(0);
cnt[0] = 0;
for (ll child : edges[node]) {
if (is_dead[child]) continue;
dfs(child, node, w[make_pair(child, node)], 1);
for (ll x : s1) {
if (s.find(k - x) != s.end()) ans = min(ans, cnt1[x] + cnt[k - x]);
}
for (ll x : s1) {
s.insert(x);
cnt[x] = (cnt[x] == -1 ? cnt1[x] : min(cnt1[x], cnt[x]));
cnt1[x] = -1;
}
s1.clear();
}
for (ll x : s) {
cnt[x] = -1;
}
s.clear();
is_dead[node] = 1;
for (ll child : edges[node]) {
if (is_dead[child]) continue;
comp_sz(child, node);
find_cent(child, node, sz[child]);
}
}
int best_path(int N, int K, int H[][2], int L[]){
n = N, k = K;
for (int i = 0; i < n - 1; i++) {
H[i][0]++, H[i][1]++;
edges[H[i][0]].push_back(H[i][1]);
edges[H[i][1]].push_back(H[i][0]);
}
for (int i = 0; i < n - 1; i++) {
w[make_pair(H[i][0], H[i][1])] = L[i];
w[make_pair(H[i][1], H[i][0])] = L[i];
}
is_dead[0] = 1;
comp_sz(1, 0);
find_cent(1, 0, n);
if (ans == NN) ans = -1;
return ans;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
23900 KB |
Output is correct |
2 |
Correct |
6 ms |
23900 KB |
Output is correct |
3 |
Correct |
10 ms |
23896 KB |
Output is correct |
4 |
Correct |
8 ms |
23900 KB |
Output is correct |
5 |
Correct |
9 ms |
23896 KB |
Output is correct |
6 |
Correct |
6 ms |
23900 KB |
Output is correct |
7 |
Correct |
6 ms |
23900 KB |
Output is correct |
8 |
Correct |
7 ms |
23952 KB |
Output is correct |
9 |
Correct |
7 ms |
23896 KB |
Output is correct |
10 |
Correct |
7 ms |
23900 KB |
Output is correct |
11 |
Correct |
7 ms |
23876 KB |
Output is correct |
12 |
Correct |
7 ms |
23864 KB |
Output is correct |
13 |
Correct |
7 ms |
23900 KB |
Output is correct |
14 |
Correct |
7 ms |
23900 KB |
Output is correct |
15 |
Correct |
6 ms |
23900 KB |
Output is correct |
16 |
Correct |
6 ms |
23900 KB |
Output is correct |
17 |
Correct |
6 ms |
23900 KB |
Output is correct |
18 |
Correct |
10 ms |
23832 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
23900 KB |
Output is correct |
2 |
Correct |
6 ms |
23900 KB |
Output is correct |
3 |
Correct |
10 ms |
23896 KB |
Output is correct |
4 |
Correct |
8 ms |
23900 KB |
Output is correct |
5 |
Correct |
9 ms |
23896 KB |
Output is correct |
6 |
Correct |
6 ms |
23900 KB |
Output is correct |
7 |
Correct |
6 ms |
23900 KB |
Output is correct |
8 |
Correct |
7 ms |
23952 KB |
Output is correct |
9 |
Correct |
7 ms |
23896 KB |
Output is correct |
10 |
Correct |
7 ms |
23900 KB |
Output is correct |
11 |
Correct |
7 ms |
23876 KB |
Output is correct |
12 |
Correct |
7 ms |
23864 KB |
Output is correct |
13 |
Correct |
7 ms |
23900 KB |
Output is correct |
14 |
Correct |
7 ms |
23900 KB |
Output is correct |
15 |
Correct |
6 ms |
23900 KB |
Output is correct |
16 |
Correct |
6 ms |
23900 KB |
Output is correct |
17 |
Correct |
6 ms |
23900 KB |
Output is correct |
18 |
Correct |
10 ms |
23832 KB |
Output is correct |
19 |
Correct |
7 ms |
23904 KB |
Output is correct |
20 |
Correct |
7 ms |
23900 KB |
Output is correct |
21 |
Correct |
9 ms |
23900 KB |
Output is correct |
22 |
Correct |
11 ms |
23960 KB |
Output is correct |
23 |
Correct |
11 ms |
23896 KB |
Output is correct |
24 |
Correct |
8 ms |
23904 KB |
Output is correct |
25 |
Correct |
10 ms |
24152 KB |
Output is correct |
26 |
Correct |
9 ms |
24156 KB |
Output is correct |
27 |
Correct |
10 ms |
23900 KB |
Output is correct |
28 |
Correct |
10 ms |
23960 KB |
Output is correct |
29 |
Correct |
13 ms |
24156 KB |
Output is correct |
30 |
Correct |
10 ms |
24100 KB |
Output is correct |
31 |
Correct |
9 ms |
24156 KB |
Output is correct |
32 |
Correct |
9 ms |
24172 KB |
Output is correct |
33 |
Correct |
10 ms |
24152 KB |
Output is correct |
34 |
Correct |
9 ms |
23928 KB |
Output is correct |
35 |
Correct |
10 ms |
24064 KB |
Output is correct |
36 |
Correct |
10 ms |
24148 KB |
Output is correct |
37 |
Correct |
9 ms |
24156 KB |
Output is correct |
38 |
Correct |
9 ms |
24096 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
23900 KB |
Output is correct |
2 |
Correct |
6 ms |
23900 KB |
Output is correct |
3 |
Correct |
10 ms |
23896 KB |
Output is correct |
4 |
Correct |
8 ms |
23900 KB |
Output is correct |
5 |
Correct |
9 ms |
23896 KB |
Output is correct |
6 |
Correct |
6 ms |
23900 KB |
Output is correct |
7 |
Correct |
6 ms |
23900 KB |
Output is correct |
8 |
Correct |
7 ms |
23952 KB |
Output is correct |
9 |
Correct |
7 ms |
23896 KB |
Output is correct |
10 |
Correct |
7 ms |
23900 KB |
Output is correct |
11 |
Correct |
7 ms |
23876 KB |
Output is correct |
12 |
Correct |
7 ms |
23864 KB |
Output is correct |
13 |
Correct |
7 ms |
23900 KB |
Output is correct |
14 |
Correct |
7 ms |
23900 KB |
Output is correct |
15 |
Correct |
6 ms |
23900 KB |
Output is correct |
16 |
Correct |
6 ms |
23900 KB |
Output is correct |
17 |
Correct |
6 ms |
23900 KB |
Output is correct |
18 |
Correct |
10 ms |
23832 KB |
Output is correct |
19 |
Correct |
537 ms |
41448 KB |
Output is correct |
20 |
Correct |
498 ms |
42224 KB |
Output is correct |
21 |
Correct |
488 ms |
42064 KB |
Output is correct |
22 |
Correct |
442 ms |
42576 KB |
Output is correct |
23 |
Correct |
658 ms |
42868 KB |
Output is correct |
24 |
Correct |
328 ms |
42580 KB |
Output is correct |
25 |
Correct |
559 ms |
48544 KB |
Output is correct |
26 |
Correct |
239 ms |
47696 KB |
Output is correct |
27 |
Correct |
496 ms |
60500 KB |
Output is correct |
28 |
Correct |
2105 ms |
74452 KB |
Output is correct |
29 |
Correct |
2115 ms |
74904 KB |
Output is correct |
30 |
Correct |
521 ms |
60892 KB |
Output is correct |
31 |
Correct |
496 ms |
61008 KB |
Output is correct |
32 |
Correct |
897 ms |
60756 KB |
Output is correct |
33 |
Correct |
1304 ms |
60152 KB |
Output is correct |
34 |
Correct |
1032 ms |
60148 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
23900 KB |
Output is correct |
2 |
Correct |
6 ms |
23900 KB |
Output is correct |
3 |
Correct |
10 ms |
23896 KB |
Output is correct |
4 |
Correct |
8 ms |
23900 KB |
Output is correct |
5 |
Correct |
9 ms |
23896 KB |
Output is correct |
6 |
Correct |
6 ms |
23900 KB |
Output is correct |
7 |
Correct |
6 ms |
23900 KB |
Output is correct |
8 |
Correct |
7 ms |
23952 KB |
Output is correct |
9 |
Correct |
7 ms |
23896 KB |
Output is correct |
10 |
Correct |
7 ms |
23900 KB |
Output is correct |
11 |
Correct |
7 ms |
23876 KB |
Output is correct |
12 |
Correct |
7 ms |
23864 KB |
Output is correct |
13 |
Correct |
7 ms |
23900 KB |
Output is correct |
14 |
Correct |
7 ms |
23900 KB |
Output is correct |
15 |
Correct |
6 ms |
23900 KB |
Output is correct |
16 |
Correct |
6 ms |
23900 KB |
Output is correct |
17 |
Correct |
6 ms |
23900 KB |
Output is correct |
18 |
Correct |
10 ms |
23832 KB |
Output is correct |
19 |
Correct |
7 ms |
23904 KB |
Output is correct |
20 |
Correct |
7 ms |
23900 KB |
Output is correct |
21 |
Correct |
9 ms |
23900 KB |
Output is correct |
22 |
Correct |
11 ms |
23960 KB |
Output is correct |
23 |
Correct |
11 ms |
23896 KB |
Output is correct |
24 |
Correct |
8 ms |
23904 KB |
Output is correct |
25 |
Correct |
10 ms |
24152 KB |
Output is correct |
26 |
Correct |
9 ms |
24156 KB |
Output is correct |
27 |
Correct |
10 ms |
23900 KB |
Output is correct |
28 |
Correct |
10 ms |
23960 KB |
Output is correct |
29 |
Correct |
13 ms |
24156 KB |
Output is correct |
30 |
Correct |
10 ms |
24100 KB |
Output is correct |
31 |
Correct |
9 ms |
24156 KB |
Output is correct |
32 |
Correct |
9 ms |
24172 KB |
Output is correct |
33 |
Correct |
10 ms |
24152 KB |
Output is correct |
34 |
Correct |
9 ms |
23928 KB |
Output is correct |
35 |
Correct |
10 ms |
24064 KB |
Output is correct |
36 |
Correct |
10 ms |
24148 KB |
Output is correct |
37 |
Correct |
9 ms |
24156 KB |
Output is correct |
38 |
Correct |
9 ms |
24096 KB |
Output is correct |
39 |
Correct |
537 ms |
41448 KB |
Output is correct |
40 |
Correct |
498 ms |
42224 KB |
Output is correct |
41 |
Correct |
488 ms |
42064 KB |
Output is correct |
42 |
Correct |
442 ms |
42576 KB |
Output is correct |
43 |
Correct |
658 ms |
42868 KB |
Output is correct |
44 |
Correct |
328 ms |
42580 KB |
Output is correct |
45 |
Correct |
559 ms |
48544 KB |
Output is correct |
46 |
Correct |
239 ms |
47696 KB |
Output is correct |
47 |
Correct |
496 ms |
60500 KB |
Output is correct |
48 |
Correct |
2105 ms |
74452 KB |
Output is correct |
49 |
Correct |
2115 ms |
74904 KB |
Output is correct |
50 |
Correct |
521 ms |
60892 KB |
Output is correct |
51 |
Correct |
496 ms |
61008 KB |
Output is correct |
52 |
Correct |
897 ms |
60756 KB |
Output is correct |
53 |
Correct |
1304 ms |
60152 KB |
Output is correct |
54 |
Correct |
1032 ms |
60148 KB |
Output is correct |
55 |
Correct |
39 ms |
26060 KB |
Output is correct |
56 |
Correct |
28 ms |
25692 KB |
Output is correct |
57 |
Correct |
271 ms |
41556 KB |
Output is correct |
58 |
Correct |
88 ms |
42440 KB |
Output is correct |
59 |
Correct |
497 ms |
53632 KB |
Output is correct |
60 |
Correct |
2075 ms |
85204 KB |
Output is correct |
61 |
Correct |
530 ms |
61644 KB |
Output is correct |
62 |
Correct |
509 ms |
61024 KB |
Output is correct |
63 |
Correct |
897 ms |
60960 KB |
Output is correct |
64 |
Correct |
1790 ms |
66216 KB |
Output is correct |
65 |
Correct |
1028 ms |
62220 KB |
Output is correct |
66 |
Correct |
1778 ms |
70432 KB |
Output is correct |
67 |
Correct |
305 ms |
62376 KB |
Output is correct |
68 |
Correct |
903 ms |
64436 KB |
Output is correct |
69 |
Correct |
990 ms |
62648 KB |
Output is correct |
70 |
Correct |
896 ms |
63060 KB |
Output is correct |