#include "race.h"
#include <bits/stdc++.h>
#define N 200000
#define MAX 1000000000
#define E 0.00000001
#define MOD 1000000007
#define INF 0x3f3f3f3f
#define INFll 0x3f3f3f3f3f3f3f3fll
#define LEFT(x) (2 * x)
#define RIGHT(x) (2 * x + 1)
#define se second
#define fi first
using namespace std;
typedef long long ll;
vector<int> g[N+1];
map<pair<int,int>, ll> e;
map<ll,int, greater<ll>> d[N+1];
ll dist[N+1];
int sz[N+1];
int ans = -1;
void prec(ll u, ll p){
if(u != 1) dist[u] = dist[p] + e[{u,p}];
for(ll v : g[u]){
if(v == p) continue;
prec(v, u);
}
}
void solve(int u, int p, int depth, ll k){
int big = -1;
sz[u] = 1;
for(int v : g[u]){
if(v == p) continue;
solve(v, u, depth+1, k);
sz[u] += sz[v];
if(big == -1 or sz[v] > sz[big]) big = v;
}
if(big == -1){
d[u][dist[u]] = depth;
return;
}
swap(d[u], d[big]);
d[u][dist[u]] = depth;
for(int v : g[u]){
if(v == p or v == big) continue;
for(auto i : d[v]){
ll want = k + 2 * dist[u] - i.fi;
if(d[u].count(want)){
if(ans == -1) ans = (i.se - depth) + (d[u][want] - depth);
else ans = min(ans, (i.se - depth) + (d[u][want] - depth));
}
}
for(auto i : d[v]){
if(d[u].count(i.fi)){
d[u][i.fi] = min(d[u][i.fi], i.se);
}
else{
d[u][i.fi] = i.se;
}
}
}
if(d[u].count(k + dist[u])){
if(ans == -1) ans = d[u][k + dist[u]] - depth;
else ans = min(ans, d[u][k + dist[u]] - depth);
}
}
int best_path(int n, int k, int (*h)[2], int* l){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
for(ll i = 0; i <= n; i++){
g[i].clear();
dist[i] = 0;
sz[i] = 0;
d[i].clear();
}
ans = -1;
e.clear();
for(ll i = 0; i < n-1; i++){
ll u, v, w;
u = h[i][0];
v = h[i][1];
w = l[i];
u++;
v++;
g[u].push_back(v);
g[v].push_back(u);
e[{u,v}] = w;
e[{v,u}] = w;
}
prec(1,0);
solve(1, 0, 0, k);
return ans;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
14420 KB |
Output is correct |
2 |
Correct |
8 ms |
14420 KB |
Output is correct |
3 |
Correct |
7 ms |
14448 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 |
14420 KB |
Output is correct |
7 |
Correct |
8 ms |
14456 KB |
Output is correct |
8 |
Correct |
7 ms |
14420 KB |
Output is correct |
9 |
Correct |
7 ms |
14444 KB |
Output is correct |
10 |
Correct |
10 ms |
14400 KB |
Output is correct |
11 |
Correct |
7 ms |
14420 KB |
Output is correct |
12 |
Correct |
9 ms |
14420 KB |
Output is correct |
13 |
Correct |
8 ms |
14456 KB |
Output is correct |
14 |
Correct |
8 ms |
14420 KB |
Output is correct |
15 |
Correct |
8 ms |
14420 KB |
Output is correct |
16 |
Correct |
11 ms |
14432 KB |
Output is correct |
17 |
Correct |
8 ms |
14420 KB |
Output is correct |
18 |
Correct |
7 ms |
14480 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
14420 KB |
Output is correct |
2 |
Correct |
8 ms |
14420 KB |
Output is correct |
3 |
Correct |
7 ms |
14448 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 |
14420 KB |
Output is correct |
7 |
Correct |
8 ms |
14456 KB |
Output is correct |
8 |
Correct |
7 ms |
14420 KB |
Output is correct |
9 |
Correct |
7 ms |
14444 KB |
Output is correct |
10 |
Correct |
10 ms |
14400 KB |
Output is correct |
11 |
Correct |
7 ms |
14420 KB |
Output is correct |
12 |
Correct |
9 ms |
14420 KB |
Output is correct |
13 |
Correct |
8 ms |
14456 KB |
Output is correct |
14 |
Correct |
8 ms |
14420 KB |
Output is correct |
15 |
Correct |
8 ms |
14420 KB |
Output is correct |
16 |
Correct |
11 ms |
14432 KB |
Output is correct |
17 |
Correct |
8 ms |
14420 KB |
Output is correct |
18 |
Correct |
7 ms |
14480 KB |
Output is correct |
19 |
Correct |
7 ms |
14420 KB |
Output is correct |
20 |
Correct |
7 ms |
14420 KB |
Output is correct |
21 |
Correct |
9 ms |
14768 KB |
Output is correct |
22 |
Correct |
9 ms |
14804 KB |
Output is correct |
23 |
Correct |
8 ms |
14800 KB |
Output is correct |
24 |
Correct |
9 ms |
14804 KB |
Output is correct |
25 |
Correct |
11 ms |
14744 KB |
Output is correct |
26 |
Correct |
9 ms |
14804 KB |
Output is correct |
27 |
Correct |
8 ms |
14676 KB |
Output is correct |
28 |
Correct |
9 ms |
14804 KB |
Output is correct |
29 |
Correct |
10 ms |
14804 KB |
Output is correct |
30 |
Correct |
9 ms |
14796 KB |
Output is correct |
31 |
Correct |
9 ms |
14796 KB |
Output is correct |
32 |
Correct |
9 ms |
14760 KB |
Output is correct |
33 |
Correct |
9 ms |
14804 KB |
Output is correct |
34 |
Correct |
9 ms |
14676 KB |
Output is correct |
35 |
Correct |
9 ms |
14664 KB |
Output is correct |
36 |
Correct |
9 ms |
14764 KB |
Output is correct |
37 |
Correct |
9 ms |
14676 KB |
Output is correct |
38 |
Correct |
9 ms |
14664 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
14420 KB |
Output is correct |
2 |
Correct |
8 ms |
14420 KB |
Output is correct |
3 |
Correct |
7 ms |
14448 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 |
14420 KB |
Output is correct |
7 |
Correct |
8 ms |
14456 KB |
Output is correct |
8 |
Correct |
7 ms |
14420 KB |
Output is correct |
9 |
Correct |
7 ms |
14444 KB |
Output is correct |
10 |
Correct |
10 ms |
14400 KB |
Output is correct |
11 |
Correct |
7 ms |
14420 KB |
Output is correct |
12 |
Correct |
9 ms |
14420 KB |
Output is correct |
13 |
Correct |
8 ms |
14456 KB |
Output is correct |
14 |
Correct |
8 ms |
14420 KB |
Output is correct |
15 |
Correct |
8 ms |
14420 KB |
Output is correct |
16 |
Correct |
11 ms |
14432 KB |
Output is correct |
17 |
Correct |
8 ms |
14420 KB |
Output is correct |
18 |
Correct |
7 ms |
14480 KB |
Output is correct |
19 |
Correct |
215 ms |
43688 KB |
Output is correct |
20 |
Correct |
233 ms |
44824 KB |
Output is correct |
21 |
Correct |
248 ms |
44892 KB |
Output is correct |
22 |
Correct |
370 ms |
45136 KB |
Output is correct |
23 |
Correct |
346 ms |
58384 KB |
Output is correct |
24 |
Correct |
326 ms |
51376 KB |
Output is correct |
25 |
Correct |
236 ms |
44348 KB |
Output is correct |
26 |
Correct |
121 ms |
52472 KB |
Output is correct |
27 |
Correct |
480 ms |
66776 KB |
Output is correct |
28 |
Correct |
623 ms |
103496 KB |
Output is correct |
29 |
Correct |
623 ms |
101652 KB |
Output is correct |
30 |
Correct |
453 ms |
66688 KB |
Output is correct |
31 |
Correct |
466 ms |
66724 KB |
Output is correct |
32 |
Correct |
803 ms |
66700 KB |
Output is correct |
33 |
Correct |
655 ms |
66276 KB |
Output is correct |
34 |
Correct |
682 ms |
98860 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
14420 KB |
Output is correct |
2 |
Correct |
8 ms |
14420 KB |
Output is correct |
3 |
Correct |
7 ms |
14448 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 |
14420 KB |
Output is correct |
7 |
Correct |
8 ms |
14456 KB |
Output is correct |
8 |
Correct |
7 ms |
14420 KB |
Output is correct |
9 |
Correct |
7 ms |
14444 KB |
Output is correct |
10 |
Correct |
10 ms |
14400 KB |
Output is correct |
11 |
Correct |
7 ms |
14420 KB |
Output is correct |
12 |
Correct |
9 ms |
14420 KB |
Output is correct |
13 |
Correct |
8 ms |
14456 KB |
Output is correct |
14 |
Correct |
8 ms |
14420 KB |
Output is correct |
15 |
Correct |
8 ms |
14420 KB |
Output is correct |
16 |
Correct |
11 ms |
14432 KB |
Output is correct |
17 |
Correct |
8 ms |
14420 KB |
Output is correct |
18 |
Correct |
7 ms |
14480 KB |
Output is correct |
19 |
Correct |
7 ms |
14420 KB |
Output is correct |
20 |
Correct |
7 ms |
14420 KB |
Output is correct |
21 |
Correct |
9 ms |
14768 KB |
Output is correct |
22 |
Correct |
9 ms |
14804 KB |
Output is correct |
23 |
Correct |
8 ms |
14800 KB |
Output is correct |
24 |
Correct |
9 ms |
14804 KB |
Output is correct |
25 |
Correct |
11 ms |
14744 KB |
Output is correct |
26 |
Correct |
9 ms |
14804 KB |
Output is correct |
27 |
Correct |
8 ms |
14676 KB |
Output is correct |
28 |
Correct |
9 ms |
14804 KB |
Output is correct |
29 |
Correct |
10 ms |
14804 KB |
Output is correct |
30 |
Correct |
9 ms |
14796 KB |
Output is correct |
31 |
Correct |
9 ms |
14796 KB |
Output is correct |
32 |
Correct |
9 ms |
14760 KB |
Output is correct |
33 |
Correct |
9 ms |
14804 KB |
Output is correct |
34 |
Correct |
9 ms |
14676 KB |
Output is correct |
35 |
Correct |
9 ms |
14664 KB |
Output is correct |
36 |
Correct |
9 ms |
14764 KB |
Output is correct |
37 |
Correct |
9 ms |
14676 KB |
Output is correct |
38 |
Correct |
9 ms |
14664 KB |
Output is correct |
39 |
Correct |
215 ms |
43688 KB |
Output is correct |
40 |
Correct |
233 ms |
44824 KB |
Output is correct |
41 |
Correct |
248 ms |
44892 KB |
Output is correct |
42 |
Correct |
370 ms |
45136 KB |
Output is correct |
43 |
Correct |
346 ms |
58384 KB |
Output is correct |
44 |
Correct |
326 ms |
51376 KB |
Output is correct |
45 |
Correct |
236 ms |
44348 KB |
Output is correct |
46 |
Correct |
121 ms |
52472 KB |
Output is correct |
47 |
Correct |
480 ms |
66776 KB |
Output is correct |
48 |
Correct |
623 ms |
103496 KB |
Output is correct |
49 |
Correct |
623 ms |
101652 KB |
Output is correct |
50 |
Correct |
453 ms |
66688 KB |
Output is correct |
51 |
Correct |
466 ms |
66724 KB |
Output is correct |
52 |
Correct |
803 ms |
66700 KB |
Output is correct |
53 |
Correct |
655 ms |
66276 KB |
Output is correct |
54 |
Correct |
682 ms |
98860 KB |
Output is correct |
55 |
Correct |
28 ms |
18468 KB |
Output is correct |
56 |
Correct |
20 ms |
17100 KB |
Output is correct |
57 |
Correct |
133 ms |
43516 KB |
Output is correct |
58 |
Correct |
129 ms |
40056 KB |
Output is correct |
59 |
Correct |
127 ms |
58812 KB |
Output is correct |
60 |
Correct |
646 ms |
102088 KB |
Output is correct |
61 |
Correct |
490 ms |
70220 KB |
Output is correct |
62 |
Correct |
439 ms |
66608 KB |
Output is correct |
63 |
Correct |
730 ms |
66928 KB |
Output is correct |
64 |
Correct |
938 ms |
117172 KB |
Output is correct |
65 |
Correct |
960 ms |
117732 KB |
Output is correct |
66 |
Correct |
693 ms |
97580 KB |
Output is correct |
67 |
Correct |
570 ms |
67244 KB |
Output is correct |
68 |
Correct |
743 ms |
85020 KB |
Output is correct |
69 |
Correct |
775 ms |
85984 KB |
Output is correct |
70 |
Correct |
731 ms |
81948 KB |
Output is correct |