#pragma GCC optimize("O3")
#include <bits/stdc++.h>
#include "race.h"
using namespace std;
typedef long long ll;
//#define int ll
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
set<pair<ll,ll> >v[200010];
ll mi=1e9;
ll KK;
vector<ll> dim(200010),up(200010);
ll dfs(ll pos,ll prec){
up[pos]=prec;
dim[pos]++;
for(auto x:v[pos])if(x.first!=prec)dim[pos]+=dfs(x.first,pos);
return dim[pos];
}
map<ll,ll> best;
void w(ll pos,ll prec,ll dist,ll archi){
if(best.count(dist)==0 || archi<best[dist])best[dist]=archi;
for(auto k:v[pos]){
if(k.first==prec)continue;
w(k.first,pos,dist+k.second,archi+1);
}
}
void calcola(ll pos){
map<ll,ll> q;
for(auto k:v[pos]){
best.clear();
w(k.first,pos,k.second,1);
for(auto x:best){
if(q.count(KK-x.first)){
mi=min(mi,q[KK-x.first]+x.second);
}
if(x.first==KK){
mi=min(mi,x.second);
}
}
for(auto x:best){
if(q.count(x.first)==0 || x.second<q[x.first])q[x.first]=x.second;
}
}
//cout<<"calcolato: "<<pos<<" "<<mi<<endl;
}
void centroid(ll pos);
void trova(ll pos,ll nodi){
if(nodi==1)return;
for(auto k:v[pos]){
if(k.first==up[pos])continue;
if(dim[k.first]>nodi/2){
trova(k.first,nodi);
return;
}
}
calcola(pos);
vector<ll> vicini;
for(auto k:v[pos]){
if(v[k.first].find({pos,k.second})!=v[k.first].end())v[k.first].erase({pos,k.second});
vicini.push_back(k.first);
if(k.first!=up[pos])up[k.first]=-1;
}
v[pos].clear();
ll g=up[pos];
while(g!=-1){
dim[g]-=dim[pos];
g=up[g];
}
//cout<<"pulito: "<<pos<<endl;
up[pos]=-1;
for(ll x:vicini){
centroid(x);
}
}
void centroid(ll pos){
//cout<<"centroid: "<<pos<<" ";
while(up[pos]!=-1){
pos=up[pos];
}
//cout<<pos<<endl;
trova(pos,dim[pos]);
}
int best_path(int N, int K, int H[][2], int L[]){
for(ll i=0;i<N-1;i++){
v[H[i][0]].insert({H[i][1],L[i]});
v[H[i][1]].insert({H[i][0],L[i]});
}
KK=K;
dfs(0,-1);
centroid(0);
if(mi==1e9)return -1;
else return mi;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
11 ms |
12928 KB |
Output is correct |
2 |
Correct |
11 ms |
12928 KB |
Output is correct |
3 |
Correct |
11 ms |
12928 KB |
Output is correct |
4 |
Correct |
11 ms |
12928 KB |
Output is correct |
5 |
Correct |
11 ms |
12928 KB |
Output is correct |
6 |
Correct |
12 ms |
13056 KB |
Output is correct |
7 |
Correct |
11 ms |
12928 KB |
Output is correct |
8 |
Correct |
11 ms |
12928 KB |
Output is correct |
9 |
Correct |
12 ms |
12928 KB |
Output is correct |
10 |
Correct |
11 ms |
12928 KB |
Output is correct |
11 |
Correct |
11 ms |
12920 KB |
Output is correct |
12 |
Correct |
11 ms |
12928 KB |
Output is correct |
13 |
Correct |
13 ms |
12928 KB |
Output is correct |
14 |
Correct |
11 ms |
12928 KB |
Output is correct |
15 |
Correct |
12 ms |
12928 KB |
Output is correct |
16 |
Correct |
11 ms |
12928 KB |
Output is correct |
17 |
Correct |
11 ms |
12928 KB |
Output is correct |
18 |
Correct |
11 ms |
12928 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
11 ms |
12928 KB |
Output is correct |
2 |
Correct |
11 ms |
12928 KB |
Output is correct |
3 |
Correct |
11 ms |
12928 KB |
Output is correct |
4 |
Correct |
11 ms |
12928 KB |
Output is correct |
5 |
Correct |
11 ms |
12928 KB |
Output is correct |
6 |
Correct |
12 ms |
13056 KB |
Output is correct |
7 |
Correct |
11 ms |
12928 KB |
Output is correct |
8 |
Correct |
11 ms |
12928 KB |
Output is correct |
9 |
Correct |
12 ms |
12928 KB |
Output is correct |
10 |
Correct |
11 ms |
12928 KB |
Output is correct |
11 |
Correct |
11 ms |
12920 KB |
Output is correct |
12 |
Correct |
11 ms |
12928 KB |
Output is correct |
13 |
Correct |
13 ms |
12928 KB |
Output is correct |
14 |
Correct |
11 ms |
12928 KB |
Output is correct |
15 |
Correct |
12 ms |
12928 KB |
Output is correct |
16 |
Correct |
11 ms |
12928 KB |
Output is correct |
17 |
Correct |
11 ms |
12928 KB |
Output is correct |
18 |
Correct |
11 ms |
12928 KB |
Output is correct |
19 |
Correct |
11 ms |
12928 KB |
Output is correct |
20 |
Correct |
11 ms |
12928 KB |
Output is correct |
21 |
Correct |
13 ms |
12928 KB |
Output is correct |
22 |
Correct |
15 ms |
13184 KB |
Output is correct |
23 |
Correct |
14 ms |
13056 KB |
Output is correct |
24 |
Correct |
14 ms |
13056 KB |
Output is correct |
25 |
Correct |
14 ms |
13056 KB |
Output is correct |
26 |
Correct |
15 ms |
13056 KB |
Output is correct |
27 |
Correct |
16 ms |
13048 KB |
Output is correct |
28 |
Correct |
15 ms |
13184 KB |
Output is correct |
29 |
Correct |
14 ms |
13056 KB |
Output is correct |
30 |
Correct |
14 ms |
13056 KB |
Output is correct |
31 |
Correct |
14 ms |
13184 KB |
Output is correct |
32 |
Correct |
14 ms |
13056 KB |
Output is correct |
33 |
Correct |
14 ms |
13184 KB |
Output is correct |
34 |
Correct |
14 ms |
13184 KB |
Output is correct |
35 |
Correct |
14 ms |
13184 KB |
Output is correct |
36 |
Correct |
14 ms |
13184 KB |
Output is correct |
37 |
Correct |
14 ms |
13056 KB |
Output is correct |
38 |
Correct |
14 ms |
13056 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
11 ms |
12928 KB |
Output is correct |
2 |
Correct |
11 ms |
12928 KB |
Output is correct |
3 |
Correct |
11 ms |
12928 KB |
Output is correct |
4 |
Correct |
11 ms |
12928 KB |
Output is correct |
5 |
Correct |
11 ms |
12928 KB |
Output is correct |
6 |
Correct |
12 ms |
13056 KB |
Output is correct |
7 |
Correct |
11 ms |
12928 KB |
Output is correct |
8 |
Correct |
11 ms |
12928 KB |
Output is correct |
9 |
Correct |
12 ms |
12928 KB |
Output is correct |
10 |
Correct |
11 ms |
12928 KB |
Output is correct |
11 |
Correct |
11 ms |
12920 KB |
Output is correct |
12 |
Correct |
11 ms |
12928 KB |
Output is correct |
13 |
Correct |
13 ms |
12928 KB |
Output is correct |
14 |
Correct |
11 ms |
12928 KB |
Output is correct |
15 |
Correct |
12 ms |
12928 KB |
Output is correct |
16 |
Correct |
11 ms |
12928 KB |
Output is correct |
17 |
Correct |
11 ms |
12928 KB |
Output is correct |
18 |
Correct |
11 ms |
12928 KB |
Output is correct |
19 |
Correct |
355 ms |
28024 KB |
Output is correct |
20 |
Correct |
350 ms |
28024 KB |
Output is correct |
21 |
Correct |
344 ms |
27896 KB |
Output is correct |
22 |
Correct |
333 ms |
28024 KB |
Output is correct |
23 |
Correct |
504 ms |
28280 KB |
Output is correct |
24 |
Correct |
252 ms |
28024 KB |
Output is correct |
25 |
Correct |
315 ms |
37624 KB |
Output is correct |
26 |
Correct |
140 ms |
45176 KB |
Output is correct |
27 |
Correct |
591 ms |
43384 KB |
Output is correct |
28 |
Correct |
1653 ms |
96376 KB |
Output is correct |
29 |
Correct |
1622 ms |
94200 KB |
Output is correct |
30 |
Correct |
586 ms |
43640 KB |
Output is correct |
31 |
Correct |
595 ms |
43520 KB |
Output is correct |
32 |
Correct |
692 ms |
43512 KB |
Output is correct |
33 |
Correct |
775 ms |
44152 KB |
Output is correct |
34 |
Correct |
1339 ms |
62200 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
11 ms |
12928 KB |
Output is correct |
2 |
Correct |
11 ms |
12928 KB |
Output is correct |
3 |
Correct |
11 ms |
12928 KB |
Output is correct |
4 |
Correct |
11 ms |
12928 KB |
Output is correct |
5 |
Correct |
11 ms |
12928 KB |
Output is correct |
6 |
Correct |
12 ms |
13056 KB |
Output is correct |
7 |
Correct |
11 ms |
12928 KB |
Output is correct |
8 |
Correct |
11 ms |
12928 KB |
Output is correct |
9 |
Correct |
12 ms |
12928 KB |
Output is correct |
10 |
Correct |
11 ms |
12928 KB |
Output is correct |
11 |
Correct |
11 ms |
12920 KB |
Output is correct |
12 |
Correct |
11 ms |
12928 KB |
Output is correct |
13 |
Correct |
13 ms |
12928 KB |
Output is correct |
14 |
Correct |
11 ms |
12928 KB |
Output is correct |
15 |
Correct |
12 ms |
12928 KB |
Output is correct |
16 |
Correct |
11 ms |
12928 KB |
Output is correct |
17 |
Correct |
11 ms |
12928 KB |
Output is correct |
18 |
Correct |
11 ms |
12928 KB |
Output is correct |
19 |
Correct |
11 ms |
12928 KB |
Output is correct |
20 |
Correct |
11 ms |
12928 KB |
Output is correct |
21 |
Correct |
13 ms |
12928 KB |
Output is correct |
22 |
Correct |
15 ms |
13184 KB |
Output is correct |
23 |
Correct |
14 ms |
13056 KB |
Output is correct |
24 |
Correct |
14 ms |
13056 KB |
Output is correct |
25 |
Correct |
14 ms |
13056 KB |
Output is correct |
26 |
Correct |
15 ms |
13056 KB |
Output is correct |
27 |
Correct |
16 ms |
13048 KB |
Output is correct |
28 |
Correct |
15 ms |
13184 KB |
Output is correct |
29 |
Correct |
14 ms |
13056 KB |
Output is correct |
30 |
Correct |
14 ms |
13056 KB |
Output is correct |
31 |
Correct |
14 ms |
13184 KB |
Output is correct |
32 |
Correct |
14 ms |
13056 KB |
Output is correct |
33 |
Correct |
14 ms |
13184 KB |
Output is correct |
34 |
Correct |
14 ms |
13184 KB |
Output is correct |
35 |
Correct |
14 ms |
13184 KB |
Output is correct |
36 |
Correct |
14 ms |
13184 KB |
Output is correct |
37 |
Correct |
14 ms |
13056 KB |
Output is correct |
38 |
Correct |
14 ms |
13056 KB |
Output is correct |
39 |
Correct |
355 ms |
28024 KB |
Output is correct |
40 |
Correct |
350 ms |
28024 KB |
Output is correct |
41 |
Correct |
344 ms |
27896 KB |
Output is correct |
42 |
Correct |
333 ms |
28024 KB |
Output is correct |
43 |
Correct |
504 ms |
28280 KB |
Output is correct |
44 |
Correct |
252 ms |
28024 KB |
Output is correct |
45 |
Correct |
315 ms |
37624 KB |
Output is correct |
46 |
Correct |
140 ms |
45176 KB |
Output is correct |
47 |
Correct |
591 ms |
43384 KB |
Output is correct |
48 |
Correct |
1653 ms |
96376 KB |
Output is correct |
49 |
Correct |
1622 ms |
94200 KB |
Output is correct |
50 |
Correct |
586 ms |
43640 KB |
Output is correct |
51 |
Correct |
595 ms |
43520 KB |
Output is correct |
52 |
Correct |
692 ms |
43512 KB |
Output is correct |
53 |
Correct |
775 ms |
44152 KB |
Output is correct |
54 |
Correct |
1339 ms |
62200 KB |
Output is correct |
55 |
Correct |
48 ms |
14976 KB |
Output is correct |
56 |
Correct |
30 ms |
14336 KB |
Output is correct |
57 |
Correct |
180 ms |
27896 KB |
Output is correct |
58 |
Correct |
112 ms |
28788 KB |
Output is correct |
59 |
Correct |
563 ms |
53112 KB |
Output is correct |
60 |
Correct |
1614 ms |
93704 KB |
Output is correct |
61 |
Correct |
629 ms |
45304 KB |
Output is correct |
62 |
Correct |
574 ms |
43640 KB |
Output is correct |
63 |
Correct |
691 ms |
43512 KB |
Output is correct |
64 |
Correct |
1639 ms |
52804 KB |
Output is correct |
65 |
Correct |
1409 ms |
58592 KB |
Output is correct |
66 |
Correct |
1611 ms |
88312 KB |
Output is correct |
67 |
Correct |
455 ms |
45804 KB |
Output is correct |
68 |
Correct |
907 ms |
58104 KB |
Output is correct |
69 |
Correct |
919 ms |
55164 KB |
Output is correct |
70 |
Correct |
848 ms |
52216 KB |
Output is correct |