#include <iostream>
#include <race.h>
#include <bits/stdc++.h>
using namespace std;
const long long MAXN = 2e5+5;
long long ans =1e18;
long long sz[MAXN];
bool used[MAXN];
long long n,k;
map<long long,long long> m1;
vector<pair<long long,long long>> toadd;
long long currsz = 0;
vector<pair<long long,long long>> v1[MAXN];
void dfs(long long curr,long long par){
currsz++;
sz[curr] = 1;
for(auto x:v1[curr]){
if(!used[x.first] && x.first!=par){
dfs(x.first,curr);
sz[curr]+=sz[x.first];
}
}
}
long long centroid(long long curr,long long par){
for(auto x:v1[curr]){
if(!used[x.first] && x.first!=par && sz[x.first]>currsz/2){
return centroid(x.first,curr);
}
}
return curr;
}
void dfsans(long long curr,long long par,long long val,long long depth){
for(auto x:v1[curr]){
if(x.first!=par && !used[x.first]){
toadd.push_back(make_pair(val+x.second,depth+1));
dfsans(x.first,curr,val+x.second,depth+1);
}
}
}
void decompose(long long u,long long par){
currsz = 0;
dfs(u,u);
long long cen = centroid(u,u);
//link[cen] = par;
//find all paths through the centroid
m1.clear();
for(auto x:v1[cen]){
if(!used[x.first]){
toadd.clear();
toadd.push_back(make_pair(x.second,1));
dfsans(x.first,cen,x.second,1);
for(auto y:toadd){
if(k-y.first>=0 && m1[k-y.first]){
// cout<<x.first<<" "<<cen<<" "<<y.first<<endl;
ans = min(ans,m1[k-y.first]+y.second);
}
if(y.first == k){
ans = min(ans,y.second);
}
}
for(auto y:toadd){
// cout<<cen<<" "<<y.first<<" "<<y.second<<endl;
if(!m1[y.first]){
m1[y.first] = y.second;
}else{
m1[y.first] = min(m1[y.first],y.second);
}
}
}
}
used[cen] = true;
for(auto x:v1[cen]){
if(!used[x.first]){
decompose(x.first,cen);
}
}
used[cen] = false;
}
int best_path(int N,int K,int H[][2],int L[]){
n = N;
k = K;
for(long long i=0;i<n-1;i++){
long long u = H[i][0];
long long v = H[i][1];
long long w = L[i];
u++;v++;
v1[u].push_back(make_pair(v,w));
v1[v].push_back(make_pair(u,w));
}
decompose(1,1);
if(ans == 1e18){
return -1;
}
return (int)ans;
}
/*int main(){
cin>>n>>k;
for(long long i=0;i<n-1;i++){
long long u,v,w;
cin>>u>>v>>w;
u++;v++;
v1[u].push_back(make_pair(v,w));
v1[v].push_back(make_pair(u,w));
}
decompose(1,1);
cout<<ans<<endl;
}
*/
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
5112 KB |
Output is correct |
2 |
Correct |
7 ms |
4984 KB |
Output is correct |
3 |
Correct |
8 ms |
5112 KB |
Output is correct |
4 |
Correct |
7 ms |
4984 KB |
Output is correct |
5 |
Correct |
7 ms |
4984 KB |
Output is correct |
6 |
Correct |
7 ms |
4988 KB |
Output is correct |
7 |
Correct |
8 ms |
4984 KB |
Output is correct |
8 |
Correct |
8 ms |
4984 KB |
Output is correct |
9 |
Correct |
8 ms |
5112 KB |
Output is correct |
10 |
Correct |
7 ms |
4984 KB |
Output is correct |
11 |
Correct |
8 ms |
5112 KB |
Output is correct |
12 |
Correct |
8 ms |
4984 KB |
Output is correct |
13 |
Correct |
8 ms |
4984 KB |
Output is correct |
14 |
Correct |
8 ms |
4984 KB |
Output is correct |
15 |
Correct |
8 ms |
4984 KB |
Output is correct |
16 |
Correct |
8 ms |
5016 KB |
Output is correct |
17 |
Correct |
8 ms |
4984 KB |
Output is correct |
18 |
Correct |
8 ms |
4984 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
5112 KB |
Output is correct |
2 |
Correct |
7 ms |
4984 KB |
Output is correct |
3 |
Correct |
8 ms |
5112 KB |
Output is correct |
4 |
Correct |
7 ms |
4984 KB |
Output is correct |
5 |
Correct |
7 ms |
4984 KB |
Output is correct |
6 |
Correct |
7 ms |
4988 KB |
Output is correct |
7 |
Correct |
8 ms |
4984 KB |
Output is correct |
8 |
Correct |
8 ms |
4984 KB |
Output is correct |
9 |
Correct |
8 ms |
5112 KB |
Output is correct |
10 |
Correct |
7 ms |
4984 KB |
Output is correct |
11 |
Correct |
8 ms |
5112 KB |
Output is correct |
12 |
Correct |
8 ms |
4984 KB |
Output is correct |
13 |
Correct |
8 ms |
4984 KB |
Output is correct |
14 |
Correct |
8 ms |
4984 KB |
Output is correct |
15 |
Correct |
8 ms |
4984 KB |
Output is correct |
16 |
Correct |
8 ms |
5016 KB |
Output is correct |
17 |
Correct |
8 ms |
4984 KB |
Output is correct |
18 |
Correct |
8 ms |
4984 KB |
Output is correct |
19 |
Correct |
8 ms |
4984 KB |
Output is correct |
20 |
Correct |
8 ms |
5112 KB |
Output is correct |
21 |
Correct |
10 ms |
5112 KB |
Output is correct |
22 |
Correct |
10 ms |
5112 KB |
Output is correct |
23 |
Correct |
9 ms |
5112 KB |
Output is correct |
24 |
Correct |
9 ms |
5240 KB |
Output is correct |
25 |
Correct |
10 ms |
5244 KB |
Output is correct |
26 |
Correct |
9 ms |
5240 KB |
Output is correct |
27 |
Correct |
8 ms |
5112 KB |
Output is correct |
28 |
Correct |
10 ms |
5240 KB |
Output is correct |
29 |
Correct |
10 ms |
5240 KB |
Output is correct |
30 |
Correct |
10 ms |
5240 KB |
Output is correct |
31 |
Correct |
10 ms |
5240 KB |
Output is correct |
32 |
Correct |
10 ms |
5240 KB |
Output is correct |
33 |
Correct |
11 ms |
5244 KB |
Output is correct |
34 |
Correct |
12 ms |
5116 KB |
Output is correct |
35 |
Correct |
10 ms |
5240 KB |
Output is correct |
36 |
Correct |
9 ms |
5240 KB |
Output is correct |
37 |
Correct |
10 ms |
5240 KB |
Output is correct |
38 |
Correct |
10 ms |
5240 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
5112 KB |
Output is correct |
2 |
Correct |
7 ms |
4984 KB |
Output is correct |
3 |
Correct |
8 ms |
5112 KB |
Output is correct |
4 |
Correct |
7 ms |
4984 KB |
Output is correct |
5 |
Correct |
7 ms |
4984 KB |
Output is correct |
6 |
Correct |
7 ms |
4988 KB |
Output is correct |
7 |
Correct |
8 ms |
4984 KB |
Output is correct |
8 |
Correct |
8 ms |
4984 KB |
Output is correct |
9 |
Correct |
8 ms |
5112 KB |
Output is correct |
10 |
Correct |
7 ms |
4984 KB |
Output is correct |
11 |
Correct |
8 ms |
5112 KB |
Output is correct |
12 |
Correct |
8 ms |
4984 KB |
Output is correct |
13 |
Correct |
8 ms |
4984 KB |
Output is correct |
14 |
Correct |
8 ms |
4984 KB |
Output is correct |
15 |
Correct |
8 ms |
4984 KB |
Output is correct |
16 |
Correct |
8 ms |
5016 KB |
Output is correct |
17 |
Correct |
8 ms |
4984 KB |
Output is correct |
18 |
Correct |
8 ms |
4984 KB |
Output is correct |
19 |
Correct |
336 ms |
14648 KB |
Output is correct |
20 |
Correct |
347 ms |
14704 KB |
Output is correct |
21 |
Correct |
329 ms |
14580 KB |
Output is correct |
22 |
Correct |
312 ms |
14708 KB |
Output is correct |
23 |
Correct |
350 ms |
15088 KB |
Output is correct |
24 |
Correct |
177 ms |
13816 KB |
Output is correct |
25 |
Correct |
276 ms |
20588 KB |
Output is correct |
26 |
Correct |
152 ms |
20332 KB |
Output is correct |
27 |
Correct |
403 ms |
23032 KB |
Output is correct |
28 |
Correct |
1087 ms |
48132 KB |
Output is correct |
29 |
Correct |
1087 ms |
48100 KB |
Output is correct |
30 |
Correct |
399 ms |
23032 KB |
Output is correct |
31 |
Correct |
392 ms |
23032 KB |
Output is correct |
32 |
Correct |
465 ms |
23068 KB |
Output is correct |
33 |
Correct |
693 ms |
23664 KB |
Output is correct |
34 |
Correct |
1004 ms |
36200 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
5112 KB |
Output is correct |
2 |
Correct |
7 ms |
4984 KB |
Output is correct |
3 |
Correct |
8 ms |
5112 KB |
Output is correct |
4 |
Correct |
7 ms |
4984 KB |
Output is correct |
5 |
Correct |
7 ms |
4984 KB |
Output is correct |
6 |
Correct |
7 ms |
4988 KB |
Output is correct |
7 |
Correct |
8 ms |
4984 KB |
Output is correct |
8 |
Correct |
8 ms |
4984 KB |
Output is correct |
9 |
Correct |
8 ms |
5112 KB |
Output is correct |
10 |
Correct |
7 ms |
4984 KB |
Output is correct |
11 |
Correct |
8 ms |
5112 KB |
Output is correct |
12 |
Correct |
8 ms |
4984 KB |
Output is correct |
13 |
Correct |
8 ms |
4984 KB |
Output is correct |
14 |
Correct |
8 ms |
4984 KB |
Output is correct |
15 |
Correct |
8 ms |
4984 KB |
Output is correct |
16 |
Correct |
8 ms |
5016 KB |
Output is correct |
17 |
Correct |
8 ms |
4984 KB |
Output is correct |
18 |
Correct |
8 ms |
4984 KB |
Output is correct |
19 |
Correct |
8 ms |
4984 KB |
Output is correct |
20 |
Correct |
8 ms |
5112 KB |
Output is correct |
21 |
Correct |
10 ms |
5112 KB |
Output is correct |
22 |
Correct |
10 ms |
5112 KB |
Output is correct |
23 |
Correct |
9 ms |
5112 KB |
Output is correct |
24 |
Correct |
9 ms |
5240 KB |
Output is correct |
25 |
Correct |
10 ms |
5244 KB |
Output is correct |
26 |
Correct |
9 ms |
5240 KB |
Output is correct |
27 |
Correct |
8 ms |
5112 KB |
Output is correct |
28 |
Correct |
10 ms |
5240 KB |
Output is correct |
29 |
Correct |
10 ms |
5240 KB |
Output is correct |
30 |
Correct |
10 ms |
5240 KB |
Output is correct |
31 |
Correct |
10 ms |
5240 KB |
Output is correct |
32 |
Correct |
10 ms |
5240 KB |
Output is correct |
33 |
Correct |
11 ms |
5244 KB |
Output is correct |
34 |
Correct |
12 ms |
5116 KB |
Output is correct |
35 |
Correct |
10 ms |
5240 KB |
Output is correct |
36 |
Correct |
9 ms |
5240 KB |
Output is correct |
37 |
Correct |
10 ms |
5240 KB |
Output is correct |
38 |
Correct |
10 ms |
5240 KB |
Output is correct |
39 |
Correct |
336 ms |
14648 KB |
Output is correct |
40 |
Correct |
347 ms |
14704 KB |
Output is correct |
41 |
Correct |
329 ms |
14580 KB |
Output is correct |
42 |
Correct |
312 ms |
14708 KB |
Output is correct |
43 |
Correct |
350 ms |
15088 KB |
Output is correct |
44 |
Correct |
177 ms |
13816 KB |
Output is correct |
45 |
Correct |
276 ms |
20588 KB |
Output is correct |
46 |
Correct |
152 ms |
20332 KB |
Output is correct |
47 |
Correct |
403 ms |
23032 KB |
Output is correct |
48 |
Correct |
1087 ms |
48132 KB |
Output is correct |
49 |
Correct |
1087 ms |
48100 KB |
Output is correct |
50 |
Correct |
399 ms |
23032 KB |
Output is correct |
51 |
Correct |
392 ms |
23032 KB |
Output is correct |
52 |
Correct |
465 ms |
23068 KB |
Output is correct |
53 |
Correct |
693 ms |
23664 KB |
Output is correct |
54 |
Correct |
1004 ms |
36200 KB |
Output is correct |
55 |
Correct |
40 ms |
6392 KB |
Output is correct |
56 |
Correct |
28 ms |
6008 KB |
Output is correct |
57 |
Correct |
221 ms |
14960 KB |
Output is correct |
58 |
Correct |
62 ms |
12900 KB |
Output is correct |
59 |
Correct |
536 ms |
29040 KB |
Output is correct |
60 |
Correct |
1504 ms |
57316 KB |
Output is correct |
61 |
Correct |
507 ms |
24824 KB |
Output is correct |
62 |
Correct |
473 ms |
23288 KB |
Output is correct |
63 |
Correct |
550 ms |
23288 KB |
Output is correct |
64 |
Correct |
1747 ms |
36452 KB |
Output is correct |
65 |
Correct |
1038 ms |
37092 KB |
Output is correct |
66 |
Correct |
1424 ms |
48612 KB |
Output is correct |
67 |
Correct |
176 ms |
22112 KB |
Output is correct |
68 |
Correct |
772 ms |
39720 KB |
Output is correct |
69 |
Correct |
789 ms |
39688 KB |
Output is correct |
70 |
Correct |
730 ms |
38244 KB |
Output is correct |