D. Minimum Diameter Tree (greedy + evenly divided structure)

https://codeforces.com/problemset/problem/1085/D


Ideas:

It can be assigned a value of 0, and a diameter is the maximum value of the sum of the edge weights of the path from this leaf to other leaves. Once one has more diameters, the weight of the other diameter edges will become less, which is not cost-effective. To make all the maximum values ​​the smallest, it can only be evenly distributed.

#include<iostream>
#include<vector>
#include<queue>
#include<cstring>
#include<cmath>
#include<map>
#include<set>
#include<cstdio>
#include<algorithm>
#define debug(a) cout<<#a<<"="<<a<<endl;
using namespace std;
const int maxn =1e5+1000;
typedef long long LL;
LL du[maxn];
int main(void){
    LL n,s;cin>>n>>s;
    for(LL i=1;i<n;i++){
        LL x,y;cin>>x>>y;
        du[x]++;du[y]++;
    }
    LL ans=0;
    for(LL i=1;i<=n;i++){
        if(du[i]==1){
            ans++;
        }
    }

    printf("%.10f\n",1.0*s/(1.0*ans)*2.0);
}

 

Guess you like

Origin blog.csdn.net/zstuyyyyccccbbbb/article/details/115025408