求调
查看原帖
求调
1272396
huochairenzhishang楼主2024/10/21 23:16
#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
ll max(ll a,ll b){
    return a>=b?a:b;
}
vector<ll> nn[(ll)3e5+1];
vector<ll> dp(3e5+1);
void fun(ll x,ll y,ll fa){
    for(auto z:nn[x]){
        if(z==fa) continue;
        //cout<<z<<'\n';
        fun(z,y,x);
        dp[x]+=dp[z]+1;
    }
    dp[x]=max(dp[x]-y,0);
    return;
}
void solve(){
    ll n;cin>>n;
    for(ll i=1;i<n;++i){
        ll x,y;cin>>x>>y;
        //cout<<i<<'\n';
        nn[x].push_back(y);
        nn[y].push_back(x);
    }
    ll l=0,r=n;
    //cout<<1<<'\n';
    while (l < r){
        //cout<<l<<' '<<r<<'\n';
        int mid = (l + r) >> 1;
        dp.clear();
        fun(1,mid,0);
        if (dp[1]==0)
            r = mid;
        else
            l = mid + 1;
    }
    cout<<l<<'\n';
    return;
}
int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    int t=1;
    while(t--)
    solve();
    return 0;
}
不懂啊
2024/10/21 23:16
加载中...