#include <bits/stdc++.h>
#define ll long long
using namespace std;
const int MAXN = 2e5+5;
vector<pair<ll,ll>> adj[MAXN]; //first second node, then weight of the edge
map<ll,ll> mindist[MAXN];
ll dep[MAXN],weight[MAXN];
int K; ll ans = 1e18;
void dfs(int u, int p){
for(auto x : adj[u]){
if(p != x.first){
dep[x.first] = dep[u]+1;
weight[x.first] = weight[u]+x.second;
mindist[x.first][weight[x.first]] = dep[x.first];
//cout << x.first << " " << dep[x.first] << " " << weight[x.first] << " " << "\n";
dfs(x.first,u);
}
}
}
void smalltolarge(int u, int p){
ll F = K+2*weight[u];
for(auto x : adj[u]){
if(x.first != p){
int v = x.first;
smalltolarge(v,u);
if(mindist[v].size() > mindist[u].size()){
swap(mindist[v],mindist[u]);
}
//cout << u << " " << v << "\n";
for(auto m : mindist[v]){
if(mindist[u].find(F-m.first) != mindist[u].end()){
ans = min(ans,mindist[u][F-m.first]+m.second-2*dep[u]);
}
}
for(auto m : mindist[v]){
if(mindist[u].find(m.first) == mindist[u].end()){
mindist[u][m.first] = m.second;
}
else{
mindist[u][m.first] = min(mindist[u][m.first],m.second);
}
//cout << m.first << " " << mindist[u][m.first] << " ";
}
}
}
}
int best_path(int n, int k, int h[][2], int length[]){
K = k;
for(int i = 0;i<n-1;++i){
int u = h[i][0], v = h[i][1];
adj[u].push_back({v,length[i]});
adj[v].push_back({u,length[i]});
}
mindist[0][0] = 0;
dfs(0,-1);
smalltolarge(0,-1);
if(ans != 1e18) return ans;
else return -1;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
14400 KB |
Output is correct |
2 |
Correct |
8 ms |
14400 KB |
Output is correct |
3 |
Correct |
7 ms |
14396 KB |
Output is correct |
4 |
Correct |
6 ms |
14400 KB |
Output is correct |
5 |
Correct |
8 ms |
14420 KB |
Output is correct |
6 |
Correct |
8 ms |
14420 KB |
Output is correct |
7 |
Correct |
9 ms |
14400 KB |
Output is correct |
8 |
Correct |
8 ms |
14420 KB |
Output is correct |
9 |
Correct |
6 ms |
14356 KB |
Output is correct |
10 |
Correct |
6 ms |
14420 KB |
Output is correct |
11 |
Correct |
6 ms |
14420 KB |
Output is correct |
12 |
Correct |
6 ms |
14420 KB |
Output is correct |
13 |
Correct |
6 ms |
14360 KB |
Output is correct |
14 |
Correct |
6 ms |
14400 KB |
Output is correct |
15 |
Correct |
6 ms |
14400 KB |
Output is correct |
16 |
Correct |
9 ms |
14420 KB |
Output is correct |
17 |
Correct |
7 ms |
14420 KB |
Output is correct |
18 |
Correct |
8 ms |
14420 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
14400 KB |
Output is correct |
2 |
Correct |
8 ms |
14400 KB |
Output is correct |
3 |
Correct |
7 ms |
14396 KB |
Output is correct |
4 |
Correct |
6 ms |
14400 KB |
Output is correct |
5 |
Correct |
8 ms |
14420 KB |
Output is correct |
6 |
Correct |
8 ms |
14420 KB |
Output is correct |
7 |
Correct |
9 ms |
14400 KB |
Output is correct |
8 |
Correct |
8 ms |
14420 KB |
Output is correct |
9 |
Correct |
6 ms |
14356 KB |
Output is correct |
10 |
Correct |
6 ms |
14420 KB |
Output is correct |
11 |
Correct |
6 ms |
14420 KB |
Output is correct |
12 |
Correct |
6 ms |
14420 KB |
Output is correct |
13 |
Correct |
6 ms |
14360 KB |
Output is correct |
14 |
Correct |
6 ms |
14400 KB |
Output is correct |
15 |
Correct |
6 ms |
14400 KB |
Output is correct |
16 |
Correct |
9 ms |
14420 KB |
Output is correct |
17 |
Correct |
7 ms |
14420 KB |
Output is correct |
18 |
Correct |
8 ms |
14420 KB |
Output is correct |
19 |
Correct |
6 ms |
14420 KB |
Output is correct |
20 |
Correct |
6 ms |
14420 KB |
Output is correct |
21 |
Correct |
7 ms |
14660 KB |
Output is correct |
22 |
Correct |
7 ms |
14668 KB |
Output is correct |
23 |
Correct |
9 ms |
14736 KB |
Output is correct |
24 |
Correct |
7 ms |
14640 KB |
Output is correct |
25 |
Correct |
9 ms |
14672 KB |
Output is correct |
26 |
Correct |
8 ms |
14676 KB |
Output is correct |
27 |
Correct |
10 ms |
14560 KB |
Output is correct |
28 |
Correct |
8 ms |
14656 KB |
Output is correct |
29 |
Correct |
8 ms |
14756 KB |
Output is correct |
30 |
Correct |
7 ms |
14672 KB |
Output is correct |
31 |
Correct |
8 ms |
14760 KB |
Output is correct |
32 |
Correct |
9 ms |
14680 KB |
Output is correct |
33 |
Correct |
10 ms |
14664 KB |
Output is correct |
34 |
Correct |
7 ms |
14668 KB |
Output is correct |
35 |
Correct |
7 ms |
14660 KB |
Output is correct |
36 |
Correct |
7 ms |
14608 KB |
Output is correct |
37 |
Correct |
7 ms |
14676 KB |
Output is correct |
38 |
Correct |
7 ms |
14664 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
14400 KB |
Output is correct |
2 |
Correct |
8 ms |
14400 KB |
Output is correct |
3 |
Correct |
7 ms |
14396 KB |
Output is correct |
4 |
Correct |
6 ms |
14400 KB |
Output is correct |
5 |
Correct |
8 ms |
14420 KB |
Output is correct |
6 |
Correct |
8 ms |
14420 KB |
Output is correct |
7 |
Correct |
9 ms |
14400 KB |
Output is correct |
8 |
Correct |
8 ms |
14420 KB |
Output is correct |
9 |
Correct |
6 ms |
14356 KB |
Output is correct |
10 |
Correct |
6 ms |
14420 KB |
Output is correct |
11 |
Correct |
6 ms |
14420 KB |
Output is correct |
12 |
Correct |
6 ms |
14420 KB |
Output is correct |
13 |
Correct |
6 ms |
14360 KB |
Output is correct |
14 |
Correct |
6 ms |
14400 KB |
Output is correct |
15 |
Correct |
6 ms |
14400 KB |
Output is correct |
16 |
Correct |
9 ms |
14420 KB |
Output is correct |
17 |
Correct |
7 ms |
14420 KB |
Output is correct |
18 |
Correct |
8 ms |
14420 KB |
Output is correct |
19 |
Correct |
122 ms |
38936 KB |
Output is correct |
20 |
Correct |
150 ms |
38772 KB |
Output is correct |
21 |
Correct |
113 ms |
38428 KB |
Output is correct |
22 |
Correct |
110 ms |
38420 KB |
Output is correct |
23 |
Correct |
165 ms |
51416 KB |
Output is correct |
24 |
Correct |
125 ms |
41336 KB |
Output is correct |
25 |
Correct |
114 ms |
40028 KB |
Output is correct |
26 |
Correct |
68 ms |
48244 KB |
Output is correct |
27 |
Correct |
186 ms |
52580 KB |
Output is correct |
28 |
Correct |
291 ms |
94780 KB |
Output is correct |
29 |
Correct |
288 ms |
92928 KB |
Output is correct |
30 |
Correct |
189 ms |
52568 KB |
Output is correct |
31 |
Correct |
180 ms |
52560 KB |
Output is correct |
32 |
Correct |
268 ms |
52808 KB |
Output is correct |
33 |
Correct |
237 ms |
57536 KB |
Output is correct |
34 |
Correct |
320 ms |
90352 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
14400 KB |
Output is correct |
2 |
Correct |
8 ms |
14400 KB |
Output is correct |
3 |
Correct |
7 ms |
14396 KB |
Output is correct |
4 |
Correct |
6 ms |
14400 KB |
Output is correct |
5 |
Correct |
8 ms |
14420 KB |
Output is correct |
6 |
Correct |
8 ms |
14420 KB |
Output is correct |
7 |
Correct |
9 ms |
14400 KB |
Output is correct |
8 |
Correct |
8 ms |
14420 KB |
Output is correct |
9 |
Correct |
6 ms |
14356 KB |
Output is correct |
10 |
Correct |
6 ms |
14420 KB |
Output is correct |
11 |
Correct |
6 ms |
14420 KB |
Output is correct |
12 |
Correct |
6 ms |
14420 KB |
Output is correct |
13 |
Correct |
6 ms |
14360 KB |
Output is correct |
14 |
Correct |
6 ms |
14400 KB |
Output is correct |
15 |
Correct |
6 ms |
14400 KB |
Output is correct |
16 |
Correct |
9 ms |
14420 KB |
Output is correct |
17 |
Correct |
7 ms |
14420 KB |
Output is correct |
18 |
Correct |
8 ms |
14420 KB |
Output is correct |
19 |
Correct |
6 ms |
14420 KB |
Output is correct |
20 |
Correct |
6 ms |
14420 KB |
Output is correct |
21 |
Correct |
7 ms |
14660 KB |
Output is correct |
22 |
Correct |
7 ms |
14668 KB |
Output is correct |
23 |
Correct |
9 ms |
14736 KB |
Output is correct |
24 |
Correct |
7 ms |
14640 KB |
Output is correct |
25 |
Correct |
9 ms |
14672 KB |
Output is correct |
26 |
Correct |
8 ms |
14676 KB |
Output is correct |
27 |
Correct |
10 ms |
14560 KB |
Output is correct |
28 |
Correct |
8 ms |
14656 KB |
Output is correct |
29 |
Correct |
8 ms |
14756 KB |
Output is correct |
30 |
Correct |
7 ms |
14672 KB |
Output is correct |
31 |
Correct |
8 ms |
14760 KB |
Output is correct |
32 |
Correct |
9 ms |
14680 KB |
Output is correct |
33 |
Correct |
10 ms |
14664 KB |
Output is correct |
34 |
Correct |
7 ms |
14668 KB |
Output is correct |
35 |
Correct |
7 ms |
14660 KB |
Output is correct |
36 |
Correct |
7 ms |
14608 KB |
Output is correct |
37 |
Correct |
7 ms |
14676 KB |
Output is correct |
38 |
Correct |
7 ms |
14664 KB |
Output is correct |
39 |
Correct |
122 ms |
38936 KB |
Output is correct |
40 |
Correct |
150 ms |
38772 KB |
Output is correct |
41 |
Correct |
113 ms |
38428 KB |
Output is correct |
42 |
Correct |
110 ms |
38420 KB |
Output is correct |
43 |
Correct |
165 ms |
51416 KB |
Output is correct |
44 |
Correct |
125 ms |
41336 KB |
Output is correct |
45 |
Correct |
114 ms |
40028 KB |
Output is correct |
46 |
Correct |
68 ms |
48244 KB |
Output is correct |
47 |
Correct |
186 ms |
52580 KB |
Output is correct |
48 |
Correct |
291 ms |
94780 KB |
Output is correct |
49 |
Correct |
288 ms |
92928 KB |
Output is correct |
50 |
Correct |
189 ms |
52568 KB |
Output is correct |
51 |
Correct |
180 ms |
52560 KB |
Output is correct |
52 |
Correct |
268 ms |
52808 KB |
Output is correct |
53 |
Correct |
237 ms |
57536 KB |
Output is correct |
54 |
Correct |
320 ms |
90352 KB |
Output is correct |
55 |
Correct |
19 ms |
17876 KB |
Output is correct |
56 |
Correct |
15 ms |
16464 KB |
Output is correct |
57 |
Correct |
63 ms |
36680 KB |
Output is correct |
58 |
Correct |
48 ms |
29164 KB |
Output is correct |
59 |
Correct |
78 ms |
54468 KB |
Output is correct |
60 |
Correct |
246 ms |
93380 KB |
Output is correct |
61 |
Correct |
216 ms |
56056 KB |
Output is correct |
62 |
Correct |
189 ms |
52632 KB |
Output is correct |
63 |
Correct |
250 ms |
52744 KB |
Output is correct |
64 |
Correct |
440 ms |
107588 KB |
Output is correct |
65 |
Correct |
403 ms |
105504 KB |
Output is correct |
66 |
Correct |
282 ms |
88992 KB |
Output is correct |
67 |
Correct |
160 ms |
45520 KB |
Output is correct |
68 |
Correct |
321 ms |
68984 KB |
Output is correct |
69 |
Correct |
354 ms |
73700 KB |
Output is correct |
70 |
Correct |
305 ms |
66636 KB |
Output is correct |