【链接】 我是链接,点我呀:)

【题意】

题意

【题解】

统计叶子节点个数m
把每条和叶子节点相邻的边权设置成s/cnt就可以了
这样答案就是2*s/m(直径最后肯定是从一个叶子节点开始,到另外一个叶子节点结束)

证明:

设dis(i,j)表示节点i和节点j之间的权值和

设a[1],a[2]..a[m]是m个叶子节点



\(max(dis(a[i],a[j])) >= ∑\frac{dis(a[i],a[j]) }{ \frac{m*(m-1)}{2|} }\)

即\(\frac{m*(m-1)}{2}*max(dis(a[i],a[j]))>=∑dis(a[i],a[j])\)

而会发现每一条边对不等式右边的贡献至少是为\(1*(m-1)\)的也就是m-1

最坏的情况就是,每条边左边只有一个叶子节点,然后右边有m-1个叶子节点。贡献就是最小的m-1

所以\(\frac{m*(m-1)}{2}*max(dis(a[i],a[j]))>=∑dis(a[i],a[j])>=(m-1)*∑e[i]=(m-1)*s\)

然后就能得到max(dis(a[i],a[j]))>=2*s/m

【代码】

#include <bits/stdc++.h>
#define rep1(i,a,b) for (int i = a;i <= b;i++)
#define rep2(i,a,b) for (int i = a;i>= b;i--)
#define ll long long
using namespace std; const int N = 1e5; int n,s;
int du[N+10]; int main(){
ios::sync_with_stdio(0),cin.tie(0);
cin >> n >> s;
rep1(i,1,n-1){
int x,y;
cin >> x >> y;
du[x]++;du[y]++;
}
int m = 0;
double ans = 2*s;
rep1(i,1,n){
if (du[i]==1){
m++;
}
}
ans = ans/m;
cout<<fixed<<setprecision(15)<<ans<<endl;
return 0;
}

【Codeforces 1086B】Minimum Diameter Tree的更多相关文章

  1. 【27.91%】【codeforces 734E】Anton and Tree

    time limit per test3 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...

  2. 【codeforces 805D】Minimum number of steps

    [题目链接]:http://codeforces.com/contest/805/problem/D [题意] 给你一个字符串; 里面只包括a和b; 让你把里面的"ab"子串全都去 ...

  3. 【codeforces 791D】 Bear and Tree Jumps

    [题目链接]:http://codeforces.com/contest/791/problem/D [题意] 你可以从树上的节点一次最多走k条边. (称为跳一次); 树为无权树; 然后问你任意两点之 ...

  4. 【HDU 4408】Minimum Spanning Tree(最小生成树计数)

    Problem Description XXX is very interested in algorithm. After learning the Prim algorithm and Krusk ...

  5. 【19.27%】【codeforces 618D】Hamiltonian Spanning Tree

    time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...

  6. 【27.48%】【codeforces 699D】 Fix a Tree

    time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...

  7. 【Codeforces 161D】Distance in Tree

    [链接] 我是链接,点我呀:) [题意] 问你一棵树上有多少条长度为k的路径 [题解] 树形dp 设 size[i]表示以节点i为根节点的子树的节点个数 dp[i][k]表示以i为根节点的子树里面距离 ...

  8. 【codeforces 698B】 Fix a Tree

    题目链接: http://codeforces.com/problemset/problem/698/B 题解: 还是比较简单的.因为每个节点只有一个父亲,可以直接建反图,保证出现的环中只有一条路径. ...

  9. 【CodeForces 699D】Fix a Tree

    dfs找出联通块个数cnt,当形成环时,令指向已访问过节点的节点变成指向-1,即做一个标记.把它作为该联通图的根. 把所有联通的图变成一颗树,如果存在指向自己的点,那么它所在的联通块就是一个树(n-1 ...

随机推荐

  1. poj 1511 Invitation Cards spfa 邻接矩阵

    题目链接: http://poj.org/problem?id=1511 题目大意: 这道题目比较难理解,我读了好长时间,最后还是在队友的帮助下理解了题意,大意就是,以一为起点,求从一到其他各点的最短 ...

  2. 对数组随机赋值,并输出(Arrays.toString(arr))

    import java.util.Arrays; public class Demo { public static void main(String[] args) { int[] arr = ne ...

  3. [转]无废话SharePoint入门教程二[SharePoint发展、工具及术语]

    本文转自:http://www.cnblogs.com/iamlilinfeng/p/3186919.html 一.前言 1.由于上一篇文章的标题命名失误,此篇标题写给百度搜索”什么是SharePoi ...

  4. [转]在 Azure 云服务上设计大规模服务的最佳实践

    本文转自:http://technet.microsoft.com/zh-cn/magazine/jj717232.aspx 英文版:http://msdn.microsoft.com/library ...

  5. LN : leetcode 118 Pascal's Triangle

    lc 118 Pascal's Triangle 118 Pascal's Triangle Given numRows, generate the first numRows of Pascal's ...

  6. javax.xml.bind.UnmarshalException: 意外的元素 (uri:"", local:"xml")。所需元素为(none)

    将xml转换为object时候报错:javax.xml.bind.UnmarshalException: 意外的元素 (uri:"", local:"xml") ...

  7. 微信小程序组件解读和分析:十二、picker滚动选择器

    picker滚动选择器组件说明: picker: 滚动选择器,现支持三种选择器,通过mode属性来区分, 分别是普通选择器(mode = selector),时间选择器(mode = time),日期 ...

  8. 轻松搞定Spring+quartz的定时任务

    1.spring 的定时任务写法有两种:一种是继承工作类,一种是普通的Bean,定时写法有两种写法:一种是以时间间隔启动任务SimpleTriggerBean,一种是以时刻启动任务CronTrigge ...

  9. apache设置无缓存

    打开httpd.conf 开启扩展 确保开启 LoadModule headers_module modules/mod_headers.so 添加配置项 并添加以下配置,跟据文件类型来让浏览器每次都 ...

  10. 【My First Blog】评近期国产烂片-《何以笙箫默》

    昨晚,我脑抽去看了[何以笙箫默],看完后我的感觉就是被这部片子恶心到了,现在想起来我还是想说:何以笙箫默是烂片烂片烂片!重要的是说三遍.以前,我问电视剧版<何以笙箫默>何以这么LOW,看了 ...