#include<bits/stdc++.h>
#define f first
#define s second
#define ll long long
#define pb push_back
#define pii pair<int,int>
#define pll pair<ll,ll>
#define all(x) x.begin(),x.end()
using namespace std;
const int N=2e5+5;
ll k;
int ans=1e7;
vector<pii>g[N];
unordered_map<ll,int>d[N];
void dfs(int u=0,int p=0,int lca=0,ll cur=0){
d[u][cur]=lca;
for(auto v:g[u]){
if(v.f==p)continue;
dfs(v.f,u,lca+1,cur+v.s);
if(d[u].size()<d[v.f].size())swap(d[u],d[v.f]);
for(auto it : d[v.f]){
if(d[u].find(k-it.f+2*cur)!=d[u].end()){
ans=min(ans,it.s+d[u][k-it.f+2*cur]-2*lca);
}
}
for(auto it : d[v.f]){
if(d[u].find(it.f)==d[u].end())d[u][it.f]=it.s;
else d[u][it.f]=min(d[u][it.f],it.s);
}
}
}
int best_path(int N, int K, int H[][2], int L[])
{
k=K;
for(int 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();
return (ans==1e7?-1:ans);
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
15956 KB |
Output is correct |
2 |
Correct |
7 ms |
15956 KB |
Output is correct |
3 |
Correct |
8 ms |
15984 KB |
Output is correct |
4 |
Correct |
7 ms |
15956 KB |
Output is correct |
5 |
Correct |
8 ms |
15956 KB |
Output is correct |
6 |
Correct |
8 ms |
15956 KB |
Output is correct |
7 |
Correct |
8 ms |
15956 KB |
Output is correct |
8 |
Correct |
7 ms |
15956 KB |
Output is correct |
9 |
Correct |
8 ms |
15876 KB |
Output is correct |
10 |
Correct |
8 ms |
15956 KB |
Output is correct |
11 |
Correct |
9 ms |
15976 KB |
Output is correct |
12 |
Correct |
7 ms |
15956 KB |
Output is correct |
13 |
Correct |
8 ms |
15956 KB |
Output is correct |
14 |
Correct |
7 ms |
15956 KB |
Output is correct |
15 |
Correct |
7 ms |
15908 KB |
Output is correct |
16 |
Correct |
7 ms |
15956 KB |
Output is correct |
17 |
Correct |
8 ms |
15956 KB |
Output is correct |
18 |
Correct |
8 ms |
15956 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
15956 KB |
Output is correct |
2 |
Correct |
7 ms |
15956 KB |
Output is correct |
3 |
Correct |
8 ms |
15984 KB |
Output is correct |
4 |
Correct |
7 ms |
15956 KB |
Output is correct |
5 |
Correct |
8 ms |
15956 KB |
Output is correct |
6 |
Correct |
8 ms |
15956 KB |
Output is correct |
7 |
Correct |
8 ms |
15956 KB |
Output is correct |
8 |
Correct |
7 ms |
15956 KB |
Output is correct |
9 |
Correct |
8 ms |
15876 KB |
Output is correct |
10 |
Correct |
8 ms |
15956 KB |
Output is correct |
11 |
Correct |
9 ms |
15976 KB |
Output is correct |
12 |
Correct |
7 ms |
15956 KB |
Output is correct |
13 |
Correct |
8 ms |
15956 KB |
Output is correct |
14 |
Correct |
7 ms |
15956 KB |
Output is correct |
15 |
Correct |
7 ms |
15908 KB |
Output is correct |
16 |
Correct |
7 ms |
15956 KB |
Output is correct |
17 |
Correct |
8 ms |
15956 KB |
Output is correct |
18 |
Correct |
8 ms |
15956 KB |
Output is correct |
19 |
Correct |
7 ms |
15956 KB |
Output is correct |
20 |
Correct |
8 ms |
15956 KB |
Output is correct |
21 |
Correct |
8 ms |
16212 KB |
Output is correct |
22 |
Correct |
9 ms |
16212 KB |
Output is correct |
23 |
Correct |
9 ms |
16252 KB |
Output is correct |
24 |
Correct |
9 ms |
16212 KB |
Output is correct |
25 |
Correct |
10 ms |
16240 KB |
Output is correct |
26 |
Correct |
11 ms |
16212 KB |
Output is correct |
27 |
Correct |
8 ms |
16084 KB |
Output is correct |
28 |
Correct |
10 ms |
16212 KB |
Output is correct |
29 |
Correct |
8 ms |
16212 KB |
Output is correct |
30 |
Correct |
9 ms |
16212 KB |
Output is correct |
31 |
Correct |
9 ms |
16212 KB |
Output is correct |
32 |
Correct |
8 ms |
16212 KB |
Output is correct |
33 |
Correct |
9 ms |
16212 KB |
Output is correct |
34 |
Correct |
9 ms |
16212 KB |
Output is correct |
35 |
Correct |
8 ms |
16212 KB |
Output is correct |
36 |
Correct |
8 ms |
16212 KB |
Output is correct |
37 |
Correct |
8 ms |
16212 KB |
Output is correct |
38 |
Correct |
9 ms |
16212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
15956 KB |
Output is correct |
2 |
Correct |
7 ms |
15956 KB |
Output is correct |
3 |
Correct |
8 ms |
15984 KB |
Output is correct |
4 |
Correct |
7 ms |
15956 KB |
Output is correct |
5 |
Correct |
8 ms |
15956 KB |
Output is correct |
6 |
Correct |
8 ms |
15956 KB |
Output is correct |
7 |
Correct |
8 ms |
15956 KB |
Output is correct |
8 |
Correct |
7 ms |
15956 KB |
Output is correct |
9 |
Correct |
8 ms |
15876 KB |
Output is correct |
10 |
Correct |
8 ms |
15956 KB |
Output is correct |
11 |
Correct |
9 ms |
15976 KB |
Output is correct |
12 |
Correct |
7 ms |
15956 KB |
Output is correct |
13 |
Correct |
8 ms |
15956 KB |
Output is correct |
14 |
Correct |
7 ms |
15956 KB |
Output is correct |
15 |
Correct |
7 ms |
15908 KB |
Output is correct |
16 |
Correct |
7 ms |
15956 KB |
Output is correct |
17 |
Correct |
8 ms |
15956 KB |
Output is correct |
18 |
Correct |
8 ms |
15956 KB |
Output is correct |
19 |
Correct |
105 ms |
40304 KB |
Output is correct |
20 |
Correct |
111 ms |
40320 KB |
Output is correct |
21 |
Correct |
105 ms |
40152 KB |
Output is correct |
22 |
Correct |
105 ms |
39788 KB |
Output is correct |
23 |
Correct |
122 ms |
48312 KB |
Output is correct |
24 |
Correct |
101 ms |
42032 KB |
Output is correct |
25 |
Correct |
95 ms |
43012 KB |
Output is correct |
26 |
Correct |
60 ms |
49996 KB |
Output is correct |
27 |
Correct |
159 ms |
58412 KB |
Output is correct |
28 |
Correct |
234 ms |
96276 KB |
Output is correct |
29 |
Correct |
225 ms |
94896 KB |
Output is correct |
30 |
Correct |
164 ms |
61340 KB |
Output is correct |
31 |
Correct |
165 ms |
61296 KB |
Output is correct |
32 |
Correct |
248 ms |
61368 KB |
Output is correct |
33 |
Correct |
188 ms |
64552 KB |
Output is correct |
34 |
Correct |
318 ms |
87412 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
15956 KB |
Output is correct |
2 |
Correct |
7 ms |
15956 KB |
Output is correct |
3 |
Correct |
8 ms |
15984 KB |
Output is correct |
4 |
Correct |
7 ms |
15956 KB |
Output is correct |
5 |
Correct |
8 ms |
15956 KB |
Output is correct |
6 |
Correct |
8 ms |
15956 KB |
Output is correct |
7 |
Correct |
8 ms |
15956 KB |
Output is correct |
8 |
Correct |
7 ms |
15956 KB |
Output is correct |
9 |
Correct |
8 ms |
15876 KB |
Output is correct |
10 |
Correct |
8 ms |
15956 KB |
Output is correct |
11 |
Correct |
9 ms |
15976 KB |
Output is correct |
12 |
Correct |
7 ms |
15956 KB |
Output is correct |
13 |
Correct |
8 ms |
15956 KB |
Output is correct |
14 |
Correct |
7 ms |
15956 KB |
Output is correct |
15 |
Correct |
7 ms |
15908 KB |
Output is correct |
16 |
Correct |
7 ms |
15956 KB |
Output is correct |
17 |
Correct |
8 ms |
15956 KB |
Output is correct |
18 |
Correct |
8 ms |
15956 KB |
Output is correct |
19 |
Correct |
7 ms |
15956 KB |
Output is correct |
20 |
Correct |
8 ms |
15956 KB |
Output is correct |
21 |
Correct |
8 ms |
16212 KB |
Output is correct |
22 |
Correct |
9 ms |
16212 KB |
Output is correct |
23 |
Correct |
9 ms |
16252 KB |
Output is correct |
24 |
Correct |
9 ms |
16212 KB |
Output is correct |
25 |
Correct |
10 ms |
16240 KB |
Output is correct |
26 |
Correct |
11 ms |
16212 KB |
Output is correct |
27 |
Correct |
8 ms |
16084 KB |
Output is correct |
28 |
Correct |
10 ms |
16212 KB |
Output is correct |
29 |
Correct |
8 ms |
16212 KB |
Output is correct |
30 |
Correct |
9 ms |
16212 KB |
Output is correct |
31 |
Correct |
9 ms |
16212 KB |
Output is correct |
32 |
Correct |
8 ms |
16212 KB |
Output is correct |
33 |
Correct |
9 ms |
16212 KB |
Output is correct |
34 |
Correct |
9 ms |
16212 KB |
Output is correct |
35 |
Correct |
8 ms |
16212 KB |
Output is correct |
36 |
Correct |
8 ms |
16212 KB |
Output is correct |
37 |
Correct |
8 ms |
16212 KB |
Output is correct |
38 |
Correct |
9 ms |
16212 KB |
Output is correct |
39 |
Correct |
105 ms |
40304 KB |
Output is correct |
40 |
Correct |
111 ms |
40320 KB |
Output is correct |
41 |
Correct |
105 ms |
40152 KB |
Output is correct |
42 |
Correct |
105 ms |
39788 KB |
Output is correct |
43 |
Correct |
122 ms |
48312 KB |
Output is correct |
44 |
Correct |
101 ms |
42032 KB |
Output is correct |
45 |
Correct |
95 ms |
43012 KB |
Output is correct |
46 |
Correct |
60 ms |
49996 KB |
Output is correct |
47 |
Correct |
159 ms |
58412 KB |
Output is correct |
48 |
Correct |
234 ms |
96276 KB |
Output is correct |
49 |
Correct |
225 ms |
94896 KB |
Output is correct |
50 |
Correct |
164 ms |
61340 KB |
Output is correct |
51 |
Correct |
165 ms |
61296 KB |
Output is correct |
52 |
Correct |
248 ms |
61368 KB |
Output is correct |
53 |
Correct |
188 ms |
64552 KB |
Output is correct |
54 |
Correct |
318 ms |
87412 KB |
Output is correct |
55 |
Correct |
17 ms |
19164 KB |
Output is correct |
56 |
Correct |
18 ms |
18260 KB |
Output is correct |
57 |
Correct |
75 ms |
39832 KB |
Output is correct |
58 |
Correct |
44 ms |
36180 KB |
Output is correct |
59 |
Correct |
64 ms |
55972 KB |
Output is correct |
60 |
Correct |
230 ms |
95240 KB |
Output is correct |
61 |
Correct |
186 ms |
63804 KB |
Output is correct |
62 |
Correct |
177 ms |
61368 KB |
Output is correct |
63 |
Correct |
246 ms |
61320 KB |
Output is correct |
64 |
Correct |
386 ms |
97428 KB |
Output is correct |
65 |
Correct |
406 ms |
96612 KB |
Output is correct |
66 |
Correct |
266 ms |
91308 KB |
Output is correct |
67 |
Correct |
184 ms |
57832 KB |
Output is correct |
68 |
Correct |
339 ms |
72380 KB |
Output is correct |
69 |
Correct |
355 ms |
75832 KB |
Output is correct |
70 |
Correct |
319 ms |
70104 KB |
Output is correct |