#include "race.h"
#include <vector>
#include <set>
#include <algorithm>
#include <queue>
#include <iostream>
using namespace std;
typedef long long ll;
ll k;
set<pair<ll,ll>> nodes[200000];
ll parents[200000];
ll sub_size[200000];
ll depth[200000];
ll abs_depth[200000];
void init_tree(ll node,ll par,ll d=0,ll abs_d=1){
//cout<<node+1<<" "<<par+1<<"\n";
parents[node]=par;
depth[node]=d;
abs_depth[node]=abs_d;
sub_size[node]=0;
for(pair<ll,ll> ne:nodes[node]){
if(ne.first!=par){
init_tree(ne.first,node,d+ne.second,abs_d+1);
sub_size[node]+=sub_size[ne.first];
}
}
sub_size[node]++;
//cout<<"done "<<node+1<<"\n";
}
ll get_centroid(ll node){
ll n=sub_size[node];
ll centroid=node;
while(sub_size[centroid]>n/2){
ll biggest_size=-1;
ll biggest=-1;
for(pair<ll,ll> ne:nodes[centroid]){
//cout<<ne<<" "<<parents[centroid]<<" "<<sub_size[ne]<<"\n";
if(ne.first!=parents[centroid]&&sub_size[ne.first]>=biggest_size){
biggest_size=sub_size[ne.first];
biggest=ne.first;
}
}
centroid=biggest;
}
return parents[centroid];
}
vector<pair<ll,ll>> get_depths(ll node, ll add){
vector<pair<ll,ll>> depths;
queue<ll> q;
q.push(node);
while(!q.empty()){
ll c=q.front();
q.pop();
depths.push_back({depth[c]+add,abs_depth[c]});
for(pair<ll,ll> ne:nodes[c])
if(ne.first!=parents[c])
q.push(ne.first);
}
return depths;
}
ll get_pairs2(vector<pair<ll,ll>>&a,vector<pair<ll,ll>>&b,ll s){
reverse(a.begin(),a.end());
ll l=0;
ll res=1000000;
for(pair<ll,ll> i:a){
ll rec=s-i.first;
while(l<b.size()&&b[l].first<rec)
l++;
if(l<b.size()&&b[l].first+i.first==s)
res=min(res,b[l].second+i.second);
}
//cout<<"pairs2: "<<res<<"\n";
return res;
}
ll get_pairs(vector<vector<pair<ll,ll>>>vec,ll s){
/*cout<<"-------\n";
for(vector<int> i:vec){
for(int j:i)
cout<<j<<" ";
cout<<"\n";
}*/
ll n=vec.size();
if(n==1){
auto iter=lower_bound(vec[0].begin(),vec[0].end(),pair<ll,ll>(s,0));
if(iter==vec[0].end()||iter<vec[0].begin()||iter->first!=s)
return 1000000;
return iter->second;
}
ll res=1000000;
ll m=n/2;
vector<pair<ll,ll>>a,b;
for(ll i=0;i<n;i++){
if(i<m)
a.insert(a.end(),vec[i].begin(),vec[i].end());
else
b.insert(b.end(),vec[i].begin(),vec[i].end());
}
sort(a.begin(),a.end());
sort(b.begin(),b.end());
res=min(res,get_pairs2(a,b,s));
res=min(res,get_pairs({vec.begin(),vec.begin()+m},s));
res=min(res,get_pairs({vec.begin()+m,vec.end()},s));
//cout<<"pairs1: "<<res<<"\n";
return res;
}
ll get(ll node){
if(nodes[node].empty())
return 1000000;
int centroid=get_centroid(node);
init_tree(centroid,centroid);
vector<pair<ll,ll>> children(nodes[centroid].begin(),nodes[centroid].end());
vector<vector<pair<ll,ll>>>vec;
for(pair<ll,ll> child:children){
nodes[centroid].erase(child);
nodes[child.first].erase({centroid,child.second});
init_tree(child.first,child.first);
vec.push_back(get_depths(child.first,child.second));
}
ll res=get_pairs(vec,k);
for(pair<ll,ll> child:children){
init_tree(child.first,child.first);
res=min(res,get(child.first));
}
return res;
}
int best_path(int N, int K, int H[][2], int L[]) {
//if(N<=1000){
ll n=N;
k=K;
for(ll i=0;i<n-1;i++){
ll a=H[i][0],b=H[i][1];
nodes[a].insert({b,L[i]});
nodes[b].insert({a,L[i]});
}
init_tree(0,0);
ll res=get(0);
if(res>=1000000)
return -1;
return res;
/*}else{
ll n=N;
k=K;
for(ll i=0;i<n-1;i++){
ll a=H[i][0],b=H[i][1];
nodes[a].insert({b,L[i]});
//nodes[b].insert({a,L[i]});
}
//init_tree(0,0);
//ll res=get(0);
ll res=-1;
if(res>=1000000)
return -1;
return res;
}*/
}
Compilation message
race.cpp: In function 'll get_pairs2(std::vector<std::pair<long long int, long long int> >&, std::vector<std::pair<long long int, long long int> >&, ll)':
race.cpp:74:16: warning: comparison of integer expressions of different signedness: 'll' {aka 'long long int'} and 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
74 | while(l<b.size()&&b[l].first<rec)
| ~^~~~~~~~~
race.cpp:76:13: warning: comparison of integer expressions of different signedness: 'll' {aka 'long long int'} and 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
76 | if(l<b.size()&&b[l].first+i.first==s)
| ~^~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
9684 KB |
Output is correct |
2 |
Correct |
4 ms |
9684 KB |
Output is correct |
3 |
Correct |
4 ms |
9684 KB |
Output is correct |
4 |
Correct |
4 ms |
9684 KB |
Output is correct |
5 |
Correct |
5 ms |
9684 KB |
Output is correct |
6 |
Correct |
4 ms |
9684 KB |
Output is correct |
7 |
Correct |
4 ms |
9684 KB |
Output is correct |
8 |
Correct |
4 ms |
9684 KB |
Output is correct |
9 |
Correct |
4 ms |
9684 KB |
Output is correct |
10 |
Correct |
4 ms |
9684 KB |
Output is correct |
11 |
Correct |
4 ms |
9684 KB |
Output is correct |
12 |
Correct |
4 ms |
9684 KB |
Output is correct |
13 |
Correct |
4 ms |
9684 KB |
Output is correct |
14 |
Correct |
5 ms |
9772 KB |
Output is correct |
15 |
Correct |
4 ms |
9684 KB |
Output is correct |
16 |
Correct |
4 ms |
9684 KB |
Output is correct |
17 |
Correct |
4 ms |
9684 KB |
Output is correct |
18 |
Correct |
4 ms |
9684 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
9684 KB |
Output is correct |
2 |
Correct |
4 ms |
9684 KB |
Output is correct |
3 |
Correct |
4 ms |
9684 KB |
Output is correct |
4 |
Correct |
4 ms |
9684 KB |
Output is correct |
5 |
Correct |
5 ms |
9684 KB |
Output is correct |
6 |
Correct |
4 ms |
9684 KB |
Output is correct |
7 |
Correct |
4 ms |
9684 KB |
Output is correct |
8 |
Correct |
4 ms |
9684 KB |
Output is correct |
9 |
Correct |
4 ms |
9684 KB |
Output is correct |
10 |
Correct |
4 ms |
9684 KB |
Output is correct |
11 |
Correct |
4 ms |
9684 KB |
Output is correct |
12 |
Correct |
4 ms |
9684 KB |
Output is correct |
13 |
Correct |
4 ms |
9684 KB |
Output is correct |
14 |
Correct |
5 ms |
9772 KB |
Output is correct |
15 |
Correct |
4 ms |
9684 KB |
Output is correct |
16 |
Correct |
4 ms |
9684 KB |
Output is correct |
17 |
Correct |
4 ms |
9684 KB |
Output is correct |
18 |
Correct |
4 ms |
9684 KB |
Output is correct |
19 |
Correct |
4 ms |
9684 KB |
Output is correct |
20 |
Correct |
4 ms |
9684 KB |
Output is correct |
21 |
Correct |
6 ms |
9940 KB |
Output is correct |
22 |
Correct |
6 ms |
9940 KB |
Output is correct |
23 |
Correct |
5 ms |
9940 KB |
Output is correct |
24 |
Correct |
7 ms |
9940 KB |
Output is correct |
25 |
Correct |
6 ms |
9940 KB |
Output is correct |
26 |
Correct |
5 ms |
9940 KB |
Output is correct |
27 |
Correct |
6 ms |
9908 KB |
Output is correct |
28 |
Correct |
6 ms |
9940 KB |
Output is correct |
29 |
Correct |
6 ms |
9940 KB |
Output is correct |
30 |
Correct |
6 ms |
9980 KB |
Output is correct |
31 |
Correct |
6 ms |
10028 KB |
Output is correct |
32 |
Correct |
6 ms |
9940 KB |
Output is correct |
33 |
Correct |
6 ms |
9940 KB |
Output is correct |
34 |
Correct |
5 ms |
9940 KB |
Output is correct |
35 |
Correct |
5 ms |
9940 KB |
Output is correct |
36 |
Correct |
5 ms |
9940 KB |
Output is correct |
37 |
Correct |
5 ms |
9940 KB |
Output is correct |
38 |
Correct |
5 ms |
9980 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
9684 KB |
Output is correct |
2 |
Correct |
4 ms |
9684 KB |
Output is correct |
3 |
Correct |
4 ms |
9684 KB |
Output is correct |
4 |
Correct |
4 ms |
9684 KB |
Output is correct |
5 |
Correct |
5 ms |
9684 KB |
Output is correct |
6 |
Correct |
4 ms |
9684 KB |
Output is correct |
7 |
Correct |
4 ms |
9684 KB |
Output is correct |
8 |
Correct |
4 ms |
9684 KB |
Output is correct |
9 |
Correct |
4 ms |
9684 KB |
Output is correct |
10 |
Correct |
4 ms |
9684 KB |
Output is correct |
11 |
Correct |
4 ms |
9684 KB |
Output is correct |
12 |
Correct |
4 ms |
9684 KB |
Output is correct |
13 |
Correct |
4 ms |
9684 KB |
Output is correct |
14 |
Correct |
5 ms |
9772 KB |
Output is correct |
15 |
Correct |
4 ms |
9684 KB |
Output is correct |
16 |
Correct |
4 ms |
9684 KB |
Output is correct |
17 |
Correct |
4 ms |
9684 KB |
Output is correct |
18 |
Correct |
4 ms |
9684 KB |
Output is correct |
19 |
Correct |
369 ms |
33608 KB |
Output is correct |
20 |
Correct |
383 ms |
36240 KB |
Output is correct |
21 |
Correct |
379 ms |
36736 KB |
Output is correct |
22 |
Correct |
352 ms |
38124 KB |
Output is correct |
23 |
Correct |
280 ms |
36004 KB |
Output is correct |
24 |
Correct |
243 ms |
36072 KB |
Output is correct |
25 |
Correct |
326 ms |
37644 KB |
Output is correct |
26 |
Correct |
171 ms |
41304 KB |
Output is correct |
27 |
Correct |
555 ms |
66400 KB |
Output is correct |
28 |
Correct |
1022 ms |
73004 KB |
Output is correct |
29 |
Correct |
987 ms |
71944 KB |
Output is correct |
30 |
Correct |
521 ms |
66464 KB |
Output is correct |
31 |
Correct |
513 ms |
66400 KB |
Output is correct |
32 |
Correct |
683 ms |
64240 KB |
Output is correct |
33 |
Correct |
709 ms |
62448 KB |
Output is correct |
34 |
Correct |
749 ms |
63288 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
9684 KB |
Output is correct |
2 |
Correct |
4 ms |
9684 KB |
Output is correct |
3 |
Correct |
4 ms |
9684 KB |
Output is correct |
4 |
Correct |
4 ms |
9684 KB |
Output is correct |
5 |
Correct |
5 ms |
9684 KB |
Output is correct |
6 |
Correct |
4 ms |
9684 KB |
Output is correct |
7 |
Correct |
4 ms |
9684 KB |
Output is correct |
8 |
Correct |
4 ms |
9684 KB |
Output is correct |
9 |
Correct |
4 ms |
9684 KB |
Output is correct |
10 |
Correct |
4 ms |
9684 KB |
Output is correct |
11 |
Correct |
4 ms |
9684 KB |
Output is correct |
12 |
Correct |
4 ms |
9684 KB |
Output is correct |
13 |
Correct |
4 ms |
9684 KB |
Output is correct |
14 |
Correct |
5 ms |
9772 KB |
Output is correct |
15 |
Correct |
4 ms |
9684 KB |
Output is correct |
16 |
Correct |
4 ms |
9684 KB |
Output is correct |
17 |
Correct |
4 ms |
9684 KB |
Output is correct |
18 |
Correct |
4 ms |
9684 KB |
Output is correct |
19 |
Correct |
4 ms |
9684 KB |
Output is correct |
20 |
Correct |
4 ms |
9684 KB |
Output is correct |
21 |
Correct |
6 ms |
9940 KB |
Output is correct |
22 |
Correct |
6 ms |
9940 KB |
Output is correct |
23 |
Correct |
5 ms |
9940 KB |
Output is correct |
24 |
Correct |
7 ms |
9940 KB |
Output is correct |
25 |
Correct |
6 ms |
9940 KB |
Output is correct |
26 |
Correct |
5 ms |
9940 KB |
Output is correct |
27 |
Correct |
6 ms |
9908 KB |
Output is correct |
28 |
Correct |
6 ms |
9940 KB |
Output is correct |
29 |
Correct |
6 ms |
9940 KB |
Output is correct |
30 |
Correct |
6 ms |
9980 KB |
Output is correct |
31 |
Correct |
6 ms |
10028 KB |
Output is correct |
32 |
Correct |
6 ms |
9940 KB |
Output is correct |
33 |
Correct |
6 ms |
9940 KB |
Output is correct |
34 |
Correct |
5 ms |
9940 KB |
Output is correct |
35 |
Correct |
5 ms |
9940 KB |
Output is correct |
36 |
Correct |
5 ms |
9940 KB |
Output is correct |
37 |
Correct |
5 ms |
9940 KB |
Output is correct |
38 |
Correct |
5 ms |
9980 KB |
Output is correct |
39 |
Correct |
369 ms |
33608 KB |
Output is correct |
40 |
Correct |
383 ms |
36240 KB |
Output is correct |
41 |
Correct |
379 ms |
36736 KB |
Output is correct |
42 |
Correct |
352 ms |
38124 KB |
Output is correct |
43 |
Correct |
280 ms |
36004 KB |
Output is correct |
44 |
Correct |
243 ms |
36072 KB |
Output is correct |
45 |
Correct |
326 ms |
37644 KB |
Output is correct |
46 |
Correct |
171 ms |
41304 KB |
Output is correct |
47 |
Correct |
555 ms |
66400 KB |
Output is correct |
48 |
Correct |
1022 ms |
73004 KB |
Output is correct |
49 |
Correct |
987 ms |
71944 KB |
Output is correct |
50 |
Correct |
521 ms |
66464 KB |
Output is correct |
51 |
Correct |
513 ms |
66400 KB |
Output is correct |
52 |
Correct |
683 ms |
64240 KB |
Output is correct |
53 |
Correct |
709 ms |
62448 KB |
Output is correct |
54 |
Correct |
749 ms |
63288 KB |
Output is correct |
55 |
Correct |
27 ms |
12764 KB |
Output is correct |
56 |
Correct |
23 ms |
12276 KB |
Output is correct |
57 |
Correct |
184 ms |
35588 KB |
Output is correct |
58 |
Correct |
215 ms |
37348 KB |
Output is correct |
59 |
Correct |
162 ms |
41292 KB |
Output is correct |
60 |
Correct |
931 ms |
72124 KB |
Output is correct |
61 |
Correct |
522 ms |
66576 KB |
Output is correct |
62 |
Correct |
533 ms |
65320 KB |
Output is correct |
63 |
Correct |
670 ms |
63620 KB |
Output is correct |
64 |
Correct |
937 ms |
62852 KB |
Output is correct |
65 |
Correct |
878 ms |
67060 KB |
Output is correct |
66 |
Correct |
1004 ms |
69484 KB |
Output is correct |
67 |
Correct |
708 ms |
71092 KB |
Output is correct |
68 |
Correct |
788 ms |
77848 KB |
Output is correct |
69 |
Correct |
796 ms |
79496 KB |
Output is correct |
70 |
Correct |
752 ms |
71900 KB |
Output is correct |