D. Minimum Diameter Tree(贪心+均分构造)

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


思路:

可以赋值0,而且一条直径的是从这个叶子到其他叶子的上的路径边权总和中的最大值。一旦一条直径多了,就会让其他的直径边权重变少了,不划算。要让所有的最大值都最小,只能是平均分配。

#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);
}

猜你喜欢

转载自blog.csdn.net/zstuyyyyccccbbbb/article/details/115025408