This commit is contained in:
Zengtudor 2024-11-20 00:41:23 +08:00
parent 6edf150bd1
commit 690a8a3848
3 changed files with 1047 additions and 11 deletions

View File

@ -8,7 +8,10 @@ ll n,m,mins{inf},maxs,g[maxn],f[maxn];
std::vector<std::pair<ll,ll>> adj[maxn];
void dfs(ll fth,ll u,const ll &num){
std::vector<ll> vec;
// if(num==15){
// cout<<"is 15\n";
// }
std::multiset<ll> ms;
for(auto [v,w]:adj[u]){
if(fth==v)continue;
dfs(u,v,num);
@ -16,22 +19,49 @@ void dfs(ll fth,ll u,const ll &num){
if(ll ff=g[v]+w;ff>=num){
++f[u];
}else{
vec.emplace_back(ff);
// vec.emplace_back(ff);
ms.emplace(ff);
}
}
std::sort(vec.begin(),vec.end());
ll l{0},r{ll(vec.size())-1};
while(l<r){
if(vec[l]+vec[r]>=num){
f[u]++;
l++,r--;
if(ms.size()==0)return;
if(ms.size()==1){
g[u]=*ms.begin();
return;
}
// auto it = ms.begin();
ll ans{-1},nans;
while(ms.size()>=2){
nans=*ms.begin();
ms.erase(ms.begin());
auto fit = ms.lower_bound(num-nans);
if(fit==ms.end()){
ans=nans;
}else{
l++;
f[u]++;
ms.erase(fit);
}
}
if(l==r){
g[u]=vec[r];
if(ms.size()>0){
g[u]=*std::prev(ms.end());
}else if(ans!=-1){
g[u]=ans;
}
// std::sort(vec.begin(),vec.end());
// ll l{0},r{ll(vec.size())-1},ret{-1};
// while(l<r){
// if(vec[l]+vec[r]>=num){
// f[u]++;
// l++,r--;
// }else{
// ret=vec[l];
// l++;
// }
// }
// if(l==r){
// g[u]=vec[r];
// }else if(ret!=-1){
// g[u]=ret;
// }
}
bool check(ll num){
@ -42,6 +72,8 @@ bool check(ll num){
}
int main(){
std::iostream::sync_with_stdio(false),cin.tie(nullptr),cout.tie(nullptr);
cin>>n>>m;
for(ll i{1};i<n;i++){
ll u,v,w;
@ -55,10 +87,13 @@ int main(){
ll l{mins},r{maxs},ans{l},mid;
while(l<=r){
mid=(l+r)/2;
// cout<<"checking: "<<mid<<'\n';
if(check(mid)){
// cout<<"ok\n";
ans=mid;
l=mid+1;
}else{
// cout<<"failed\n";
r=mid-1;
}
}

1000
src/P5021/P5021_17.in Normal file

File diff suppressed because it is too large Load Diff

1
src/P5021/P5021_17.out Normal file
View File

@ -0,0 +1 @@
9353