#include <bits/stdc++.h>
#include "race.h"
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
const int MAXN = 2e5 + 2;
vector<pii> adj[MAXN];
map<ll, int> m[MAXN];
map<ll, int> has[MAXN];
int n, k, ans = MAXN, sub[MAXN], inc_val[MAXN], cnt[MAXN];
ll inc_path[MAXN];
void dfs(int node, int anc){
sub[node] = 1;
for(pii x : adj[node]){
if(x.first == anc) continue;
dfs(x.first, node);
sub[node] += sub[x.first];
}
if(adj[node].size() == 1 && anc != -1){
m[node][0] = 0;
has[node][0] = 1;
}
for(pii x : adj[node]){
if(x.first == anc) continue;
++inc_val[x.first];
inc_path[x.first] += x.second;
}
m[node][0] = 0;
has[node][0] = 1;
cnt[node] = 1;
for(pii x : adj[node]){
if(x.first == anc) continue;
if(cnt[x.first] >= cnt[node]){
swap(m[node], m[x.first]);
swap(has[node], has[x.first]);
swap(inc_val[node], inc_val[x.first]);
swap(inc_path[node], inc_path[x.first]);
swap(cnt[node], cnt[x.first]);
}
for(pair<ll, int> p : m[x.first]){
ll len = p.first + inc_path[x.first];
int val = p.second + inc_val[x.first];
if(k >= len && has[node][k - len - inc_path[node]]){
ans = min(ans, val + m[node][k - len - inc_path[node]] + inc_val[node]);
}
}
for(pair<ll, int> p : m[x.first]){
ll len = p.first + inc_path[x.first] - inc_path[node];
int val = p.second + inc_val[x.first];
if(has[node][len]){
m[node][len] = min(m[node][len], val - inc_val[node]);
} else {
++cnt[node];
has[node][len] = 1;
m[node][len] = val - inc_val[node];
}
}
}
}
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]].push_back({h[i][1], l[i]});
adj[h[i][1]].push_back({h[i][0], l[i]});
}
dfs(0, -1);
if(ans == MAXN) return -1;
return ans;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
11 ms |
23764 KB |
Output is correct |
2 |
Correct |
11 ms |
23764 KB |
Output is correct |
3 |
Correct |
11 ms |
23764 KB |
Output is correct |
4 |
Correct |
12 ms |
23764 KB |
Output is correct |
5 |
Correct |
14 ms |
23764 KB |
Output is correct |
6 |
Correct |
13 ms |
23764 KB |
Output is correct |
7 |
Correct |
12 ms |
23764 KB |
Output is correct |
8 |
Correct |
10 ms |
23764 KB |
Output is correct |
9 |
Correct |
11 ms |
23764 KB |
Output is correct |
10 |
Correct |
11 ms |
23764 KB |
Output is correct |
11 |
Correct |
11 ms |
23764 KB |
Output is correct |
12 |
Correct |
11 ms |
23764 KB |
Output is correct |
13 |
Correct |
11 ms |
23764 KB |
Output is correct |
14 |
Correct |
11 ms |
23764 KB |
Output is correct |
15 |
Correct |
12 ms |
23764 KB |
Output is correct |
16 |
Correct |
12 ms |
23888 KB |
Output is correct |
17 |
Correct |
11 ms |
23764 KB |
Output is correct |
18 |
Correct |
11 ms |
23764 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
11 ms |
23764 KB |
Output is correct |
2 |
Correct |
11 ms |
23764 KB |
Output is correct |
3 |
Correct |
11 ms |
23764 KB |
Output is correct |
4 |
Correct |
12 ms |
23764 KB |
Output is correct |
5 |
Correct |
14 ms |
23764 KB |
Output is correct |
6 |
Correct |
13 ms |
23764 KB |
Output is correct |
7 |
Correct |
12 ms |
23764 KB |
Output is correct |
8 |
Correct |
10 ms |
23764 KB |
Output is correct |
9 |
Correct |
11 ms |
23764 KB |
Output is correct |
10 |
Correct |
11 ms |
23764 KB |
Output is correct |
11 |
Correct |
11 ms |
23764 KB |
Output is correct |
12 |
Correct |
11 ms |
23764 KB |
Output is correct |
13 |
Correct |
11 ms |
23764 KB |
Output is correct |
14 |
Correct |
11 ms |
23764 KB |
Output is correct |
15 |
Correct |
12 ms |
23764 KB |
Output is correct |
16 |
Correct |
12 ms |
23888 KB |
Output is correct |
17 |
Correct |
11 ms |
23764 KB |
Output is correct |
18 |
Correct |
11 ms |
23764 KB |
Output is correct |
19 |
Correct |
12 ms |
23764 KB |
Output is correct |
20 |
Correct |
11 ms |
23764 KB |
Output is correct |
21 |
Correct |
13 ms |
24276 KB |
Output is correct |
22 |
Correct |
15 ms |
24404 KB |
Output is correct |
23 |
Correct |
17 ms |
24408 KB |
Output is correct |
24 |
Correct |
13 ms |
24404 KB |
Output is correct |
25 |
Correct |
13 ms |
24460 KB |
Output is correct |
26 |
Correct |
13 ms |
24404 KB |
Output is correct |
27 |
Correct |
12 ms |
24020 KB |
Output is correct |
28 |
Correct |
13 ms |
24404 KB |
Output is correct |
29 |
Correct |
13 ms |
24404 KB |
Output is correct |
30 |
Correct |
15 ms |
24532 KB |
Output is correct |
31 |
Correct |
13 ms |
24532 KB |
Output is correct |
32 |
Correct |
15 ms |
24404 KB |
Output is correct |
33 |
Correct |
13 ms |
24532 KB |
Output is correct |
34 |
Correct |
13 ms |
24276 KB |
Output is correct |
35 |
Correct |
13 ms |
24276 KB |
Output is correct |
36 |
Correct |
12 ms |
24148 KB |
Output is correct |
37 |
Correct |
12 ms |
24276 KB |
Output is correct |
38 |
Correct |
13 ms |
24404 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
11 ms |
23764 KB |
Output is correct |
2 |
Correct |
11 ms |
23764 KB |
Output is correct |
3 |
Correct |
11 ms |
23764 KB |
Output is correct |
4 |
Correct |
12 ms |
23764 KB |
Output is correct |
5 |
Correct |
14 ms |
23764 KB |
Output is correct |
6 |
Correct |
13 ms |
23764 KB |
Output is correct |
7 |
Correct |
12 ms |
23764 KB |
Output is correct |
8 |
Correct |
10 ms |
23764 KB |
Output is correct |
9 |
Correct |
11 ms |
23764 KB |
Output is correct |
10 |
Correct |
11 ms |
23764 KB |
Output is correct |
11 |
Correct |
11 ms |
23764 KB |
Output is correct |
12 |
Correct |
11 ms |
23764 KB |
Output is correct |
13 |
Correct |
11 ms |
23764 KB |
Output is correct |
14 |
Correct |
11 ms |
23764 KB |
Output is correct |
15 |
Correct |
12 ms |
23764 KB |
Output is correct |
16 |
Correct |
12 ms |
23888 KB |
Output is correct |
17 |
Correct |
11 ms |
23764 KB |
Output is correct |
18 |
Correct |
11 ms |
23764 KB |
Output is correct |
19 |
Correct |
204 ms |
71232 KB |
Output is correct |
20 |
Correct |
186 ms |
71148 KB |
Output is correct |
21 |
Correct |
192 ms |
70780 KB |
Output is correct |
22 |
Correct |
205 ms |
69048 KB |
Output is correct |
23 |
Correct |
264 ms |
93068 KB |
Output is correct |
24 |
Correct |
200 ms |
73932 KB |
Output is correct |
25 |
Correct |
115 ms |
52940 KB |
Output is correct |
26 |
Correct |
66 ms |
59840 KB |
Output is correct |
27 |
Correct |
251 ms |
80968 KB |
Output is correct |
28 |
Correct |
348 ms |
133320 KB |
Output is correct |
29 |
Correct |
366 ms |
132400 KB |
Output is correct |
30 |
Correct |
241 ms |
81012 KB |
Output is correct |
31 |
Correct |
242 ms |
80972 KB |
Output is correct |
32 |
Correct |
366 ms |
82120 KB |
Output is correct |
33 |
Correct |
326 ms |
90572 KB |
Output is correct |
34 |
Correct |
596 ms |
162896 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
11 ms |
23764 KB |
Output is correct |
2 |
Correct |
11 ms |
23764 KB |
Output is correct |
3 |
Correct |
11 ms |
23764 KB |
Output is correct |
4 |
Correct |
12 ms |
23764 KB |
Output is correct |
5 |
Correct |
14 ms |
23764 KB |
Output is correct |
6 |
Correct |
13 ms |
23764 KB |
Output is correct |
7 |
Correct |
12 ms |
23764 KB |
Output is correct |
8 |
Correct |
10 ms |
23764 KB |
Output is correct |
9 |
Correct |
11 ms |
23764 KB |
Output is correct |
10 |
Correct |
11 ms |
23764 KB |
Output is correct |
11 |
Correct |
11 ms |
23764 KB |
Output is correct |
12 |
Correct |
11 ms |
23764 KB |
Output is correct |
13 |
Correct |
11 ms |
23764 KB |
Output is correct |
14 |
Correct |
11 ms |
23764 KB |
Output is correct |
15 |
Correct |
12 ms |
23764 KB |
Output is correct |
16 |
Correct |
12 ms |
23888 KB |
Output is correct |
17 |
Correct |
11 ms |
23764 KB |
Output is correct |
18 |
Correct |
11 ms |
23764 KB |
Output is correct |
19 |
Correct |
12 ms |
23764 KB |
Output is correct |
20 |
Correct |
11 ms |
23764 KB |
Output is correct |
21 |
Correct |
13 ms |
24276 KB |
Output is correct |
22 |
Correct |
15 ms |
24404 KB |
Output is correct |
23 |
Correct |
17 ms |
24408 KB |
Output is correct |
24 |
Correct |
13 ms |
24404 KB |
Output is correct |
25 |
Correct |
13 ms |
24460 KB |
Output is correct |
26 |
Correct |
13 ms |
24404 KB |
Output is correct |
27 |
Correct |
12 ms |
24020 KB |
Output is correct |
28 |
Correct |
13 ms |
24404 KB |
Output is correct |
29 |
Correct |
13 ms |
24404 KB |
Output is correct |
30 |
Correct |
15 ms |
24532 KB |
Output is correct |
31 |
Correct |
13 ms |
24532 KB |
Output is correct |
32 |
Correct |
15 ms |
24404 KB |
Output is correct |
33 |
Correct |
13 ms |
24532 KB |
Output is correct |
34 |
Correct |
13 ms |
24276 KB |
Output is correct |
35 |
Correct |
13 ms |
24276 KB |
Output is correct |
36 |
Correct |
12 ms |
24148 KB |
Output is correct |
37 |
Correct |
12 ms |
24276 KB |
Output is correct |
38 |
Correct |
13 ms |
24404 KB |
Output is correct |
39 |
Correct |
204 ms |
71232 KB |
Output is correct |
40 |
Correct |
186 ms |
71148 KB |
Output is correct |
41 |
Correct |
192 ms |
70780 KB |
Output is correct |
42 |
Correct |
205 ms |
69048 KB |
Output is correct |
43 |
Correct |
264 ms |
93068 KB |
Output is correct |
44 |
Correct |
200 ms |
73932 KB |
Output is correct |
45 |
Correct |
115 ms |
52940 KB |
Output is correct |
46 |
Correct |
66 ms |
59840 KB |
Output is correct |
47 |
Correct |
251 ms |
80968 KB |
Output is correct |
48 |
Correct |
348 ms |
133320 KB |
Output is correct |
49 |
Correct |
366 ms |
132400 KB |
Output is correct |
50 |
Correct |
241 ms |
81012 KB |
Output is correct |
51 |
Correct |
242 ms |
80972 KB |
Output is correct |
52 |
Correct |
366 ms |
82120 KB |
Output is correct |
53 |
Correct |
326 ms |
90572 KB |
Output is correct |
54 |
Correct |
596 ms |
162896 KB |
Output is correct |
55 |
Correct |
39 ms |
31156 KB |
Output is correct |
56 |
Correct |
25 ms |
27732 KB |
Output is correct |
57 |
Correct |
125 ms |
65440 KB |
Output is correct |
58 |
Correct |
62 ms |
43320 KB |
Output is correct |
59 |
Correct |
143 ms |
78300 KB |
Output is correct |
60 |
Correct |
379 ms |
132972 KB |
Output is correct |
61 |
Correct |
296 ms |
90424 KB |
Output is correct |
62 |
Correct |
246 ms |
82432 KB |
Output is correct |
63 |
Correct |
367 ms |
82384 KB |
Output is correct |
64 |
Correct |
871 ms |
248916 KB |
Output is correct |
65 |
Correct |
610 ms |
187796 KB |
Output is correct |
66 |
Correct |
406 ms |
131004 KB |
Output is correct |
67 |
Correct |
237 ms |
63800 KB |
Output is correct |
68 |
Correct |
629 ms |
129308 KB |
Output is correct |
69 |
Correct |
678 ms |
143096 KB |
Output is correct |
70 |
Correct |
622 ms |
125208 KB |
Output is correct |