#include <bits/stdc++.h>
#define ll long long
using namespace std;
const int MAXN = 2e5+5;
vector<pair<ll,ll>> adj[MAXN]; //first nodes, then weight
ll depth[MAXN],weightsum[MAXN],ans;
int n,k;
map<ll,ll> help[MAXN];
void dfs(int u, int p, ll d, int dep){
depth[u] = dep;
weightsum[u] = d;
help[u][d] = dep;
for(auto v : adj[u]){
if(v.first != p){
dfs(v.first,u,d+v.second,dep+1);
}
}
}
void smalltolarge(int u, int p){
ll tar = k+weightsum[u]*2;
for(auto v : adj[u]){
if(v.first != p){
smalltolarge(v.first,u);
if(help[v.first].size() > help[u].size()) swap(help[v.first],help[u]);
for(auto x : help[v.first]){
if(help[u].find(tar-x.first) != help[u].end()){
ans = min(ans,x.second+help[u][tar-x.first]-2*depth[u]);
}
}
for(auto x : help[v.first]){
if(help[u].find(x.first) == help[u].end()){
help[u].insert(x);
}
else{
help[u][x.first] = min(help[u][x.first],x.second);
}
}
}
}
}
int best_path(int N, int K, int edges[][2], int lengths[]){
if(K == 1) return 0;
n = N; k = K; ans = INT_MAX;
for(int i = 0;i<N-1;i++){
adj[edges[i][0]].push_back({edges[i][1],lengths[i]});
adj[edges[i][1]].push_back({edges[i][0],lengths[i]});
}
dfs(0,-1,0,0);
smalltolarge(0,-1);
if(ans != INT_MAX) return ans;
else return -1;
}
# |
결과 |
실행 시간 |
메모리 |
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 |
6 ms |
14420 KB |
Output is correct |
6 |
Correct |
7 ms |
14420 KB |
Output is correct |
7 |
Correct |
6 ms |
14420 KB |
Output is correct |
8 |
Correct |
7 ms |
14420 KB |
Output is correct |
9 |
Correct |
7 ms |
14428 KB |
Output is correct |
10 |
Correct |
7 ms |
14420 KB |
Output is correct |
11 |
Correct |
7 ms |
14420 KB |
Output is correct |
12 |
Correct |
7 ms |
14408 KB |
Output is correct |
13 |
Correct |
7 ms |
14420 KB |
Output is correct |
14 |
Correct |
8 ms |
14420 KB |
Output is correct |
15 |
Correct |
7 ms |
14420 KB |
Output is correct |
16 |
Correct |
6 ms |
14440 KB |
Output is correct |
17 |
Correct |
7 ms |
14444 KB |
Output is correct |
18 |
Correct |
7 ms |
14444 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 |
6 ms |
14420 KB |
Output is correct |
6 |
Correct |
7 ms |
14420 KB |
Output is correct |
7 |
Correct |
6 ms |
14420 KB |
Output is correct |
8 |
Correct |
7 ms |
14420 KB |
Output is correct |
9 |
Correct |
7 ms |
14428 KB |
Output is correct |
10 |
Correct |
7 ms |
14420 KB |
Output is correct |
11 |
Correct |
7 ms |
14420 KB |
Output is correct |
12 |
Correct |
7 ms |
14408 KB |
Output is correct |
13 |
Correct |
7 ms |
14420 KB |
Output is correct |
14 |
Correct |
8 ms |
14420 KB |
Output is correct |
15 |
Correct |
7 ms |
14420 KB |
Output is correct |
16 |
Correct |
6 ms |
14440 KB |
Output is correct |
17 |
Correct |
7 ms |
14444 KB |
Output is correct |
18 |
Correct |
7 ms |
14444 KB |
Output is correct |
19 |
Correct |
7 ms |
14408 KB |
Output is correct |
20 |
Correct |
7 ms |
14360 KB |
Output is correct |
21 |
Correct |
8 ms |
14572 KB |
Output is correct |
22 |
Correct |
8 ms |
14676 KB |
Output is correct |
23 |
Correct |
7 ms |
14680 KB |
Output is correct |
24 |
Correct |
8 ms |
14748 KB |
Output is correct |
25 |
Correct |
7 ms |
14676 KB |
Output is correct |
26 |
Correct |
8 ms |
14760 KB |
Output is correct |
27 |
Correct |
8 ms |
14548 KB |
Output is correct |
28 |
Correct |
8 ms |
14720 KB |
Output is correct |
29 |
Correct |
8 ms |
14676 KB |
Output is correct |
30 |
Correct |
8 ms |
14680 KB |
Output is correct |
31 |
Correct |
8 ms |
14676 KB |
Output is correct |
32 |
Correct |
7 ms |
14680 KB |
Output is correct |
33 |
Correct |
7 ms |
14676 KB |
Output is correct |
34 |
Correct |
7 ms |
14676 KB |
Output is correct |
35 |
Correct |
7 ms |
14676 KB |
Output is correct |
36 |
Correct |
7 ms |
14676 KB |
Output is correct |
37 |
Correct |
8 ms |
14676 KB |
Output is correct |
38 |
Correct |
7 ms |
14676 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 |
6 ms |
14420 KB |
Output is correct |
6 |
Correct |
7 ms |
14420 KB |
Output is correct |
7 |
Correct |
6 ms |
14420 KB |
Output is correct |
8 |
Correct |
7 ms |
14420 KB |
Output is correct |
9 |
Correct |
7 ms |
14428 KB |
Output is correct |
10 |
Correct |
7 ms |
14420 KB |
Output is correct |
11 |
Correct |
7 ms |
14420 KB |
Output is correct |
12 |
Correct |
7 ms |
14408 KB |
Output is correct |
13 |
Correct |
7 ms |
14420 KB |
Output is correct |
14 |
Correct |
8 ms |
14420 KB |
Output is correct |
15 |
Correct |
7 ms |
14420 KB |
Output is correct |
16 |
Correct |
6 ms |
14440 KB |
Output is correct |
17 |
Correct |
7 ms |
14444 KB |
Output is correct |
18 |
Correct |
7 ms |
14444 KB |
Output is correct |
19 |
Correct |
110 ms |
39212 KB |
Output is correct |
20 |
Correct |
135 ms |
39172 KB |
Output is correct |
21 |
Correct |
111 ms |
38924 KB |
Output is correct |
22 |
Correct |
115 ms |
38508 KB |
Output is correct |
23 |
Correct |
147 ms |
51444 KB |
Output is correct |
24 |
Correct |
107 ms |
41460 KB |
Output is correct |
25 |
Correct |
100 ms |
39960 KB |
Output is correct |
26 |
Correct |
54 ms |
48228 KB |
Output is correct |
27 |
Correct |
202 ms |
52620 KB |
Output is correct |
28 |
Correct |
262 ms |
94748 KB |
Output is correct |
29 |
Correct |
275 ms |
92932 KB |
Output is correct |
30 |
Correct |
172 ms |
52560 KB |
Output is correct |
31 |
Correct |
185 ms |
52516 KB |
Output is correct |
32 |
Correct |
255 ms |
52664 KB |
Output is correct |
33 |
Correct |
232 ms |
57532 KB |
Output is correct |
34 |
Correct |
317 ms |
90444 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 |
6 ms |
14420 KB |
Output is correct |
6 |
Correct |
7 ms |
14420 KB |
Output is correct |
7 |
Correct |
6 ms |
14420 KB |
Output is correct |
8 |
Correct |
7 ms |
14420 KB |
Output is correct |
9 |
Correct |
7 ms |
14428 KB |
Output is correct |
10 |
Correct |
7 ms |
14420 KB |
Output is correct |
11 |
Correct |
7 ms |
14420 KB |
Output is correct |
12 |
Correct |
7 ms |
14408 KB |
Output is correct |
13 |
Correct |
7 ms |
14420 KB |
Output is correct |
14 |
Correct |
8 ms |
14420 KB |
Output is correct |
15 |
Correct |
7 ms |
14420 KB |
Output is correct |
16 |
Correct |
6 ms |
14440 KB |
Output is correct |
17 |
Correct |
7 ms |
14444 KB |
Output is correct |
18 |
Correct |
7 ms |
14444 KB |
Output is correct |
19 |
Correct |
7 ms |
14408 KB |
Output is correct |
20 |
Correct |
7 ms |
14360 KB |
Output is correct |
21 |
Correct |
8 ms |
14572 KB |
Output is correct |
22 |
Correct |
8 ms |
14676 KB |
Output is correct |
23 |
Correct |
7 ms |
14680 KB |
Output is correct |
24 |
Correct |
8 ms |
14748 KB |
Output is correct |
25 |
Correct |
7 ms |
14676 KB |
Output is correct |
26 |
Correct |
8 ms |
14760 KB |
Output is correct |
27 |
Correct |
8 ms |
14548 KB |
Output is correct |
28 |
Correct |
8 ms |
14720 KB |
Output is correct |
29 |
Correct |
8 ms |
14676 KB |
Output is correct |
30 |
Correct |
8 ms |
14680 KB |
Output is correct |
31 |
Correct |
8 ms |
14676 KB |
Output is correct |
32 |
Correct |
7 ms |
14680 KB |
Output is correct |
33 |
Correct |
7 ms |
14676 KB |
Output is correct |
34 |
Correct |
7 ms |
14676 KB |
Output is correct |
35 |
Correct |
7 ms |
14676 KB |
Output is correct |
36 |
Correct |
7 ms |
14676 KB |
Output is correct |
37 |
Correct |
8 ms |
14676 KB |
Output is correct |
38 |
Correct |
7 ms |
14676 KB |
Output is correct |
39 |
Correct |
110 ms |
39212 KB |
Output is correct |
40 |
Correct |
135 ms |
39172 KB |
Output is correct |
41 |
Correct |
111 ms |
38924 KB |
Output is correct |
42 |
Correct |
115 ms |
38508 KB |
Output is correct |
43 |
Correct |
147 ms |
51444 KB |
Output is correct |
44 |
Correct |
107 ms |
41460 KB |
Output is correct |
45 |
Correct |
100 ms |
39960 KB |
Output is correct |
46 |
Correct |
54 ms |
48228 KB |
Output is correct |
47 |
Correct |
202 ms |
52620 KB |
Output is correct |
48 |
Correct |
262 ms |
94748 KB |
Output is correct |
49 |
Correct |
275 ms |
92932 KB |
Output is correct |
50 |
Correct |
172 ms |
52560 KB |
Output is correct |
51 |
Correct |
185 ms |
52516 KB |
Output is correct |
52 |
Correct |
255 ms |
52664 KB |
Output is correct |
53 |
Correct |
232 ms |
57532 KB |
Output is correct |
54 |
Correct |
317 ms |
90444 KB |
Output is correct |
55 |
Correct |
19 ms |
17824 KB |
Output is correct |
56 |
Correct |
13 ms |
16468 KB |
Output is correct |
57 |
Correct |
76 ms |
36648 KB |
Output is correct |
58 |
Correct |
47 ms |
29136 KB |
Output is correct |
59 |
Correct |
84 ms |
54484 KB |
Output is correct |
60 |
Correct |
244 ms |
93424 KB |
Output is correct |
61 |
Correct |
230 ms |
56140 KB |
Output is correct |
62 |
Correct |
172 ms |
52556 KB |
Output is correct |
63 |
Correct |
245 ms |
52688 KB |
Output is correct |
64 |
Correct |
408 ms |
107588 KB |
Output is correct |
65 |
Correct |
396 ms |
105592 KB |
Output is correct |
66 |
Correct |
275 ms |
88964 KB |
Output is correct |
67 |
Correct |
171 ms |
45648 KB |
Output is correct |
68 |
Correct |
312 ms |
69032 KB |
Output is correct |
69 |
Correct |
360 ms |
73716 KB |
Output is correct |
70 |
Correct |
311 ms |
66632 KB |
Output is correct |