#include "race.h"
#include<bits/stdc++.h>
#define ll long long
using namespace std;
struct grana{
int gde;
ll cena;
}pp;
vector<grana> stablo[200005];
int N,K,res=1e9;
const int INF=1e9;
int kol[200005],najb[1000005],p1,p2;
ll duzina[200005],puta[200005];
bool block[200005];
ll min(int a,ll b){
if(a<b)
return a;
return b;
}
int rootuj(int gde,int pret){
if(block[gde])
return 0;
kol[gde]=1;
for(int i=0;i<stablo[gde].size();i++)
if(stablo[gde][i].gde!=pret)
kol[gde]+=rootuj(stablo[gde][i].gde,gde);
return kol[gde];
}
int nadji(int gde,int pret,int pola){
int sled=-1;
for(int i=0;i<stablo[gde].size();i++){
if(block[stablo[gde][i].gde] or stablo[gde][i].gde==pret)
continue;
if(kol[stablo[gde][i].gde]>pola)
sled=stablo[gde][i].gde;
}
if(sled==-1)
return gde;
return nadji(sled,gde,pola);
}
vector<int> V,W;
void racunaj(int gde,int pret,ll dist,int put){
duzina[gde]=dist;
puta[gde]=put;
// cout<<"DFS "<<gde<<" "<<pret<<" "<<put<<" "<<dist<<endl;
if(dist<=K)
res=min(res,put+najb[K-dist]);
if(dist==K)
res=min(res,put);
V.push_back(gde);
W.push_back(gde);
for(int i=0;i<stablo[gde].size();i++){
if(block[stablo[gde][i].gde] or stablo[gde][i].gde==pret)
continue;
if(gde==pret)
W.clear();
racunaj(stablo[gde][i].gde,gde,dist+stablo[gde][i].cena,put+1);
if(gde==pret)
for(int i=0;i<W.size();i++)
if(duzina[W[i]]<=K)
najb[duzina[W[i]]]=min(najb[duzina[W[i]]],puta[W[i]]);
}
return;
}
void rek(int gde){
rootuj(gde,gde);
int c=nadji(gde,gde,kol[gde]/2+3);
V.clear();
racunaj(c,c,0,0);
/*
cout<<"CENTROID JE "<<c<<endl;
for(int i=0;i<V.size();i++)
cout<<V[i]<<": "<<puta[V[i]]<<" "<<duzina[V[i]]<<endl;
cout<<endl;
cout<<"RES= "<<res<<endl;*/
for(int i=0;i<V.size();i++){
if(duzina[V[i]]>K)
continue;
najb[duzina[V[i]]]=INF;
}
block[c]=true;
kol[c]=INF;
for(int i=0;i<stablo[c].size();i++)
if(!block[stablo[c][i].gde])
rek(stablo[c][i].gde);
return;
}
int best_path(int n,int k,int h[][2],int l[]){
N=n;
K=k;
for(int i=0;i<N-1;i++){
h[i][0]++;
h[i][1]++;
pp.cena=l[i];
pp.gde=h[i][1];
stablo[h[i][0]].push_back(pp);
//cout<<h[i][0]<<" "<<h[i][1]<<endl;
pp.gde=h[i][0];
stablo[h[i][1]].push_back(pp);
}
for(int i=0;i<=K;i++)
najb[i]=INF;
rek(1);
if(res==1e9)
return -1;
return res;
}
Compilation message
race.cpp: In function 'int rootuj(int, int)':
race.cpp:24:17: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<grana>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
24 | for(int i=0;i<stablo[gde].size();i++)
| ~^~~~~~~~~~~~~~~~~~~
race.cpp: In function 'int nadji(int, int, int)':
race.cpp:31:17: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<grana>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
31 | for(int i=0;i<stablo[gde].size();i++){
| ~^~~~~~~~~~~~~~~~~~~
race.cpp: In function 'void racunaj(int, int, long long int, int)':
race.cpp:52:17: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<grana>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
52 | for(int i=0;i<stablo[gde].size();i++){
| ~^~~~~~~~~~~~~~~~~~~
race.cpp:59:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
59 | for(int i=0;i<W.size();i++)
| ~^~~~~~~~~
race.cpp: In function 'void rek(int)':
race.cpp:77:17: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
77 | for(int i=0;i<V.size();i++){
| ~^~~~~~~~~
race.cpp:84:17: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<grana>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
84 | for(int i=0;i<stablo[c].size();i++)
| ~^~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4948 KB |
Output is correct |
2 |
Correct |
3 ms |
4948 KB |
Output is correct |
3 |
Correct |
3 ms |
4948 KB |
Output is correct |
4 |
Correct |
3 ms |
5012 KB |
Output is correct |
5 |
Correct |
3 ms |
5008 KB |
Output is correct |
6 |
Correct |
3 ms |
4948 KB |
Output is correct |
7 |
Correct |
3 ms |
5016 KB |
Output is correct |
8 |
Correct |
3 ms |
5016 KB |
Output is correct |
9 |
Correct |
3 ms |
4948 KB |
Output is correct |
10 |
Correct |
3 ms |
4948 KB |
Output is correct |
11 |
Correct |
3 ms |
4948 KB |
Output is correct |
12 |
Correct |
3 ms |
4948 KB |
Output is correct |
13 |
Correct |
3 ms |
4948 KB |
Output is correct |
14 |
Correct |
3 ms |
4948 KB |
Output is correct |
15 |
Correct |
3 ms |
4948 KB |
Output is correct |
16 |
Correct |
3 ms |
4948 KB |
Output is correct |
17 |
Correct |
3 ms |
5008 KB |
Output is correct |
18 |
Correct |
3 ms |
4948 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4948 KB |
Output is correct |
2 |
Correct |
3 ms |
4948 KB |
Output is correct |
3 |
Correct |
3 ms |
4948 KB |
Output is correct |
4 |
Correct |
3 ms |
5012 KB |
Output is correct |
5 |
Correct |
3 ms |
5008 KB |
Output is correct |
6 |
Correct |
3 ms |
4948 KB |
Output is correct |
7 |
Correct |
3 ms |
5016 KB |
Output is correct |
8 |
Correct |
3 ms |
5016 KB |
Output is correct |
9 |
Correct |
3 ms |
4948 KB |
Output is correct |
10 |
Correct |
3 ms |
4948 KB |
Output is correct |
11 |
Correct |
3 ms |
4948 KB |
Output is correct |
12 |
Correct |
3 ms |
4948 KB |
Output is correct |
13 |
Correct |
3 ms |
4948 KB |
Output is correct |
14 |
Correct |
3 ms |
4948 KB |
Output is correct |
15 |
Correct |
3 ms |
4948 KB |
Output is correct |
16 |
Correct |
3 ms |
4948 KB |
Output is correct |
17 |
Correct |
3 ms |
5008 KB |
Output is correct |
18 |
Correct |
3 ms |
4948 KB |
Output is correct |
19 |
Correct |
4 ms |
4948 KB |
Output is correct |
20 |
Correct |
3 ms |
5008 KB |
Output is correct |
21 |
Correct |
4 ms |
5076 KB |
Output is correct |
22 |
Correct |
5 ms |
8660 KB |
Output is correct |
23 |
Correct |
5 ms |
8020 KB |
Output is correct |
24 |
Correct |
6 ms |
8532 KB |
Output is correct |
25 |
Correct |
5 ms |
8352 KB |
Output is correct |
26 |
Correct |
4 ms |
6356 KB |
Output is correct |
27 |
Correct |
7 ms |
8280 KB |
Output is correct |
28 |
Correct |
4 ms |
5844 KB |
Output is correct |
29 |
Correct |
4 ms |
6356 KB |
Output is correct |
30 |
Correct |
5 ms |
6484 KB |
Output is correct |
31 |
Correct |
5 ms |
7636 KB |
Output is correct |
32 |
Correct |
5 ms |
7836 KB |
Output is correct |
33 |
Correct |
5 ms |
8148 KB |
Output is correct |
34 |
Correct |
5 ms |
7380 KB |
Output is correct |
35 |
Correct |
6 ms |
8224 KB |
Output is correct |
36 |
Correct |
5 ms |
8788 KB |
Output is correct |
37 |
Correct |
5 ms |
8224 KB |
Output is correct |
38 |
Correct |
5 ms |
7124 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4948 KB |
Output is correct |
2 |
Correct |
3 ms |
4948 KB |
Output is correct |
3 |
Correct |
3 ms |
4948 KB |
Output is correct |
4 |
Correct |
3 ms |
5012 KB |
Output is correct |
5 |
Correct |
3 ms |
5008 KB |
Output is correct |
6 |
Correct |
3 ms |
4948 KB |
Output is correct |
7 |
Correct |
3 ms |
5016 KB |
Output is correct |
8 |
Correct |
3 ms |
5016 KB |
Output is correct |
9 |
Correct |
3 ms |
4948 KB |
Output is correct |
10 |
Correct |
3 ms |
4948 KB |
Output is correct |
11 |
Correct |
3 ms |
4948 KB |
Output is correct |
12 |
Correct |
3 ms |
4948 KB |
Output is correct |
13 |
Correct |
3 ms |
4948 KB |
Output is correct |
14 |
Correct |
3 ms |
4948 KB |
Output is correct |
15 |
Correct |
3 ms |
4948 KB |
Output is correct |
16 |
Correct |
3 ms |
4948 KB |
Output is correct |
17 |
Correct |
3 ms |
5008 KB |
Output is correct |
18 |
Correct |
3 ms |
4948 KB |
Output is correct |
19 |
Correct |
168 ms |
15524 KB |
Output is correct |
20 |
Correct |
154 ms |
15376 KB |
Output is correct |
21 |
Correct |
164 ms |
15512 KB |
Output is correct |
22 |
Correct |
145 ms |
15572 KB |
Output is correct |
23 |
Correct |
116 ms |
15944 KB |
Output is correct |
24 |
Correct |
69 ms |
15016 KB |
Output is correct |
25 |
Correct |
161 ms |
19036 KB |
Output is correct |
26 |
Correct |
105 ms |
21336 KB |
Output is correct |
27 |
Correct |
195 ms |
25896 KB |
Output is correct |
28 |
Correct |
484 ms |
37540 KB |
Output is correct |
29 |
Correct |
439 ms |
36592 KB |
Output is correct |
30 |
Correct |
225 ms |
25864 KB |
Output is correct |
31 |
Correct |
192 ms |
25888 KB |
Output is correct |
32 |
Correct |
290 ms |
25948 KB |
Output is correct |
33 |
Correct |
339 ms |
25112 KB |
Output is correct |
34 |
Correct |
320 ms |
25236 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4948 KB |
Output is correct |
2 |
Correct |
3 ms |
4948 KB |
Output is correct |
3 |
Correct |
3 ms |
4948 KB |
Output is correct |
4 |
Correct |
3 ms |
5012 KB |
Output is correct |
5 |
Correct |
3 ms |
5008 KB |
Output is correct |
6 |
Correct |
3 ms |
4948 KB |
Output is correct |
7 |
Correct |
3 ms |
5016 KB |
Output is correct |
8 |
Correct |
3 ms |
5016 KB |
Output is correct |
9 |
Correct |
3 ms |
4948 KB |
Output is correct |
10 |
Correct |
3 ms |
4948 KB |
Output is correct |
11 |
Correct |
3 ms |
4948 KB |
Output is correct |
12 |
Correct |
3 ms |
4948 KB |
Output is correct |
13 |
Correct |
3 ms |
4948 KB |
Output is correct |
14 |
Correct |
3 ms |
4948 KB |
Output is correct |
15 |
Correct |
3 ms |
4948 KB |
Output is correct |
16 |
Correct |
3 ms |
4948 KB |
Output is correct |
17 |
Correct |
3 ms |
5008 KB |
Output is correct |
18 |
Correct |
3 ms |
4948 KB |
Output is correct |
19 |
Correct |
4 ms |
4948 KB |
Output is correct |
20 |
Correct |
3 ms |
5008 KB |
Output is correct |
21 |
Correct |
4 ms |
5076 KB |
Output is correct |
22 |
Correct |
5 ms |
8660 KB |
Output is correct |
23 |
Correct |
5 ms |
8020 KB |
Output is correct |
24 |
Correct |
6 ms |
8532 KB |
Output is correct |
25 |
Correct |
5 ms |
8352 KB |
Output is correct |
26 |
Correct |
4 ms |
6356 KB |
Output is correct |
27 |
Correct |
7 ms |
8280 KB |
Output is correct |
28 |
Correct |
4 ms |
5844 KB |
Output is correct |
29 |
Correct |
4 ms |
6356 KB |
Output is correct |
30 |
Correct |
5 ms |
6484 KB |
Output is correct |
31 |
Correct |
5 ms |
7636 KB |
Output is correct |
32 |
Correct |
5 ms |
7836 KB |
Output is correct |
33 |
Correct |
5 ms |
8148 KB |
Output is correct |
34 |
Correct |
5 ms |
7380 KB |
Output is correct |
35 |
Correct |
6 ms |
8224 KB |
Output is correct |
36 |
Correct |
5 ms |
8788 KB |
Output is correct |
37 |
Correct |
5 ms |
8224 KB |
Output is correct |
38 |
Correct |
5 ms |
7124 KB |
Output is correct |
39 |
Correct |
168 ms |
15524 KB |
Output is correct |
40 |
Correct |
154 ms |
15376 KB |
Output is correct |
41 |
Correct |
164 ms |
15512 KB |
Output is correct |
42 |
Correct |
145 ms |
15572 KB |
Output is correct |
43 |
Correct |
116 ms |
15944 KB |
Output is correct |
44 |
Correct |
69 ms |
15016 KB |
Output is correct |
45 |
Correct |
161 ms |
19036 KB |
Output is correct |
46 |
Correct |
105 ms |
21336 KB |
Output is correct |
47 |
Correct |
195 ms |
25896 KB |
Output is correct |
48 |
Correct |
484 ms |
37540 KB |
Output is correct |
49 |
Correct |
439 ms |
36592 KB |
Output is correct |
50 |
Correct |
225 ms |
25864 KB |
Output is correct |
51 |
Correct |
192 ms |
25888 KB |
Output is correct |
52 |
Correct |
290 ms |
25948 KB |
Output is correct |
53 |
Correct |
339 ms |
25112 KB |
Output is correct |
54 |
Correct |
320 ms |
25236 KB |
Output is correct |
55 |
Correct |
11 ms |
6040 KB |
Output is correct |
56 |
Correct |
12 ms |
6048 KB |
Output is correct |
57 |
Correct |
93 ms |
15704 KB |
Output is correct |
58 |
Correct |
43 ms |
15272 KB |
Output is correct |
59 |
Correct |
121 ms |
22088 KB |
Output is correct |
60 |
Correct |
583 ms |
40964 KB |
Output is correct |
61 |
Correct |
204 ms |
26188 KB |
Output is correct |
62 |
Correct |
209 ms |
29928 KB |
Output is correct |
63 |
Correct |
319 ms |
30032 KB |
Output is correct |
64 |
Correct |
564 ms |
28480 KB |
Output is correct |
65 |
Correct |
426 ms |
26964 KB |
Output is correct |
66 |
Correct |
537 ms |
38812 KB |
Output is correct |
67 |
Correct |
156 ms |
29956 KB |
Output is correct |
68 |
Correct |
322 ms |
30488 KB |
Output is correct |
69 |
Correct |
316 ms |
30500 KB |
Output is correct |
70 |
Correct |
309 ms |
29504 KB |
Output is correct |