#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define ll long long
#define ull unsigned long long
#define ed '\n'
#define pb push_back
#define ii pair<int,int>
#define o_set tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update>
#define fi first
#define se second
using namespace __gnu_pbds;
using namespace std;
const int N = 2e5 + 10;
ii lazy[N];
vector<ii> g[N];
int n, k, ans = 2e9;
map<int, int> f[N];
void Merge(int u, int v) {
if (f[u].size() < f[v].size()) {
swap(lazy[u], lazy[v]);
f[u].swap(f[v]);
}
for(pair<int, int> tmp: f[v]) {
int num = tmp.fi + lazy[v].fi;
int Min = tmp.se + lazy[v].se;
auto t = f[u].find(k - num - lazy[u].fi);
if (t != f[u].end()) {
if (t->se + Min + lazy[u].se < ans) {
ans = t->se + Min + lazy[u].se;
}
}
// if (f[u].find(num - lazy[u].fi) == f[u].end()) {
// f[u][num - lazy[u].fi] = Min - lazy[u].se;
// }
if (f[u][num - lazy[u].fi] == 0) f[u][num - lazy[u].fi] = Min - lazy[u].se;
else {
if (Min - lazy[u].se < f[u][num - lazy[u].fi]) {
f[u][num - lazy[u].fi] = Min - lazy[u].se;
}
}
}
}
void dfs(int u, int p, int val) {
f[u][0] = 0;
for(ii &e: g[u]) if (e.fi != p) {
dfs(e.fi, u, e.se);
Merge(u, e.fi);
}
lazy[u].fi += val;
lazy[u].se ++;
}
int best_path(int N, int K, int H[][2], int L[]) {
n = N, k = K;
for(ll i=0;i<n-1;i++)
{
g[H[i][0]].pb({H[i][1],L[i]});
g[H[i][1]].pb({H[i][0],L[i]});
}
dfs(0, -1, 0);
if (ans == 2e9) ans = -1;
return ans;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
14420 KB |
Output is correct |
2 |
Correct |
7 ms |
14420 KB |
Output is correct |
3 |
Correct |
7 ms |
14420 KB |
Output is correct |
4 |
Correct |
7 ms |
14420 KB |
Output is correct |
5 |
Correct |
8 ms |
14420 KB |
Output is correct |
6 |
Correct |
8 ms |
14392 KB |
Output is correct |
7 |
Correct |
7 ms |
14308 KB |
Output is correct |
8 |
Correct |
7 ms |
14420 KB |
Output is correct |
9 |
Correct |
8 ms |
14424 KB |
Output is correct |
10 |
Correct |
8 ms |
14548 KB |
Output is correct |
11 |
Correct |
8 ms |
14420 KB |
Output is correct |
12 |
Correct |
9 ms |
14420 KB |
Output is correct |
13 |
Correct |
9 ms |
14420 KB |
Output is correct |
14 |
Correct |
7 ms |
14376 KB |
Output is correct |
15 |
Correct |
7 ms |
14420 KB |
Output is correct |
16 |
Correct |
7 ms |
14420 KB |
Output is correct |
17 |
Correct |
8 ms |
14420 KB |
Output is correct |
18 |
Correct |
8 ms |
14432 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
14420 KB |
Output is correct |
2 |
Correct |
7 ms |
14420 KB |
Output is correct |
3 |
Correct |
7 ms |
14420 KB |
Output is correct |
4 |
Correct |
7 ms |
14420 KB |
Output is correct |
5 |
Correct |
8 ms |
14420 KB |
Output is correct |
6 |
Correct |
8 ms |
14392 KB |
Output is correct |
7 |
Correct |
7 ms |
14308 KB |
Output is correct |
8 |
Correct |
7 ms |
14420 KB |
Output is correct |
9 |
Correct |
8 ms |
14424 KB |
Output is correct |
10 |
Correct |
8 ms |
14548 KB |
Output is correct |
11 |
Correct |
8 ms |
14420 KB |
Output is correct |
12 |
Correct |
9 ms |
14420 KB |
Output is correct |
13 |
Correct |
9 ms |
14420 KB |
Output is correct |
14 |
Correct |
7 ms |
14376 KB |
Output is correct |
15 |
Correct |
7 ms |
14420 KB |
Output is correct |
16 |
Correct |
7 ms |
14420 KB |
Output is correct |
17 |
Correct |
8 ms |
14420 KB |
Output is correct |
18 |
Correct |
8 ms |
14432 KB |
Output is correct |
19 |
Correct |
8 ms |
14420 KB |
Output is correct |
20 |
Correct |
8 ms |
14420 KB |
Output is correct |
21 |
Correct |
10 ms |
14496 KB |
Output is correct |
22 |
Correct |
10 ms |
14548 KB |
Output is correct |
23 |
Correct |
8 ms |
14588 KB |
Output is correct |
24 |
Correct |
8 ms |
14548 KB |
Output is correct |
25 |
Correct |
9 ms |
14548 KB |
Output is correct |
26 |
Correct |
8 ms |
14676 KB |
Output is correct |
27 |
Correct |
8 ms |
14420 KB |
Output is correct |
28 |
Correct |
8 ms |
14548 KB |
Output is correct |
29 |
Correct |
9 ms |
14628 KB |
Output is correct |
30 |
Correct |
8 ms |
14548 KB |
Output is correct |
31 |
Correct |
8 ms |
14548 KB |
Output is correct |
32 |
Correct |
9 ms |
14560 KB |
Output is correct |
33 |
Correct |
11 ms |
14548 KB |
Output is correct |
34 |
Correct |
9 ms |
14548 KB |
Output is correct |
35 |
Correct |
9 ms |
14548 KB |
Output is correct |
36 |
Correct |
9 ms |
14548 KB |
Output is correct |
37 |
Correct |
8 ms |
14548 KB |
Output is correct |
38 |
Correct |
8 ms |
14548 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
14420 KB |
Output is correct |
2 |
Correct |
7 ms |
14420 KB |
Output is correct |
3 |
Correct |
7 ms |
14420 KB |
Output is correct |
4 |
Correct |
7 ms |
14420 KB |
Output is correct |
5 |
Correct |
8 ms |
14420 KB |
Output is correct |
6 |
Correct |
8 ms |
14392 KB |
Output is correct |
7 |
Correct |
7 ms |
14308 KB |
Output is correct |
8 |
Correct |
7 ms |
14420 KB |
Output is correct |
9 |
Correct |
8 ms |
14424 KB |
Output is correct |
10 |
Correct |
8 ms |
14548 KB |
Output is correct |
11 |
Correct |
8 ms |
14420 KB |
Output is correct |
12 |
Correct |
9 ms |
14420 KB |
Output is correct |
13 |
Correct |
9 ms |
14420 KB |
Output is correct |
14 |
Correct |
7 ms |
14376 KB |
Output is correct |
15 |
Correct |
7 ms |
14420 KB |
Output is correct |
16 |
Correct |
7 ms |
14420 KB |
Output is correct |
17 |
Correct |
8 ms |
14420 KB |
Output is correct |
18 |
Correct |
8 ms |
14432 KB |
Output is correct |
19 |
Correct |
108 ms |
31680 KB |
Output is correct |
20 |
Correct |
107 ms |
31720 KB |
Output is correct |
21 |
Correct |
111 ms |
31528 KB |
Output is correct |
22 |
Correct |
109 ms |
31128 KB |
Output is correct |
23 |
Correct |
151 ms |
40780 KB |
Output is correct |
24 |
Correct |
123 ms |
33796 KB |
Output is correct |
25 |
Correct |
83 ms |
29532 KB |
Output is correct |
26 |
Correct |
57 ms |
33664 KB |
Output is correct |
27 |
Correct |
171 ms |
40116 KB |
Output is correct |
28 |
Correct |
246 ms |
62116 KB |
Output is correct |
29 |
Correct |
285 ms |
61376 KB |
Output is correct |
30 |
Correct |
176 ms |
40140 KB |
Output is correct |
31 |
Correct |
174 ms |
40148 KB |
Output is correct |
32 |
Correct |
234 ms |
40140 KB |
Output is correct |
33 |
Correct |
223 ms |
43484 KB |
Output is correct |
34 |
Correct |
321 ms |
67432 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
14420 KB |
Output is correct |
2 |
Correct |
7 ms |
14420 KB |
Output is correct |
3 |
Correct |
7 ms |
14420 KB |
Output is correct |
4 |
Correct |
7 ms |
14420 KB |
Output is correct |
5 |
Correct |
8 ms |
14420 KB |
Output is correct |
6 |
Correct |
8 ms |
14392 KB |
Output is correct |
7 |
Correct |
7 ms |
14308 KB |
Output is correct |
8 |
Correct |
7 ms |
14420 KB |
Output is correct |
9 |
Correct |
8 ms |
14424 KB |
Output is correct |
10 |
Correct |
8 ms |
14548 KB |
Output is correct |
11 |
Correct |
8 ms |
14420 KB |
Output is correct |
12 |
Correct |
9 ms |
14420 KB |
Output is correct |
13 |
Correct |
9 ms |
14420 KB |
Output is correct |
14 |
Correct |
7 ms |
14376 KB |
Output is correct |
15 |
Correct |
7 ms |
14420 KB |
Output is correct |
16 |
Correct |
7 ms |
14420 KB |
Output is correct |
17 |
Correct |
8 ms |
14420 KB |
Output is correct |
18 |
Correct |
8 ms |
14432 KB |
Output is correct |
19 |
Correct |
8 ms |
14420 KB |
Output is correct |
20 |
Correct |
8 ms |
14420 KB |
Output is correct |
21 |
Correct |
10 ms |
14496 KB |
Output is correct |
22 |
Correct |
10 ms |
14548 KB |
Output is correct |
23 |
Correct |
8 ms |
14588 KB |
Output is correct |
24 |
Correct |
8 ms |
14548 KB |
Output is correct |
25 |
Correct |
9 ms |
14548 KB |
Output is correct |
26 |
Correct |
8 ms |
14676 KB |
Output is correct |
27 |
Correct |
8 ms |
14420 KB |
Output is correct |
28 |
Correct |
8 ms |
14548 KB |
Output is correct |
29 |
Correct |
9 ms |
14628 KB |
Output is correct |
30 |
Correct |
8 ms |
14548 KB |
Output is correct |
31 |
Correct |
8 ms |
14548 KB |
Output is correct |
32 |
Correct |
9 ms |
14560 KB |
Output is correct |
33 |
Correct |
11 ms |
14548 KB |
Output is correct |
34 |
Correct |
9 ms |
14548 KB |
Output is correct |
35 |
Correct |
9 ms |
14548 KB |
Output is correct |
36 |
Correct |
9 ms |
14548 KB |
Output is correct |
37 |
Correct |
8 ms |
14548 KB |
Output is correct |
38 |
Correct |
8 ms |
14548 KB |
Output is correct |
39 |
Correct |
108 ms |
31680 KB |
Output is correct |
40 |
Correct |
107 ms |
31720 KB |
Output is correct |
41 |
Correct |
111 ms |
31528 KB |
Output is correct |
42 |
Correct |
109 ms |
31128 KB |
Output is correct |
43 |
Correct |
151 ms |
40780 KB |
Output is correct |
44 |
Correct |
123 ms |
33796 KB |
Output is correct |
45 |
Correct |
83 ms |
29532 KB |
Output is correct |
46 |
Correct |
57 ms |
33664 KB |
Output is correct |
47 |
Correct |
171 ms |
40116 KB |
Output is correct |
48 |
Correct |
246 ms |
62116 KB |
Output is correct |
49 |
Correct |
285 ms |
61376 KB |
Output is correct |
50 |
Correct |
176 ms |
40140 KB |
Output is correct |
51 |
Correct |
174 ms |
40148 KB |
Output is correct |
52 |
Correct |
234 ms |
40140 KB |
Output is correct |
53 |
Correct |
223 ms |
43484 KB |
Output is correct |
54 |
Correct |
321 ms |
67432 KB |
Output is correct |
55 |
Correct |
23 ms |
16852 KB |
Output is correct |
56 |
Correct |
17 ms |
15892 KB |
Output is correct |
57 |
Correct |
87 ms |
29904 KB |
Output is correct |
58 |
Correct |
49 ms |
24952 KB |
Output is correct |
59 |
Correct |
78 ms |
38208 KB |
Output is correct |
60 |
Correct |
243 ms |
61640 KB |
Output is correct |
61 |
Correct |
225 ms |
42656 KB |
Output is correct |
62 |
Correct |
189 ms |
40012 KB |
Output is correct |
63 |
Correct |
251 ms |
40072 KB |
Output is correct |
64 |
Incorrect |
415 ms |
81260 KB |
Output isn't correct |
65 |
Halted |
0 ms |
0 KB |
- |