A12181 | Minimum Diameter Tree
时间限制1s
内存限制256MB
通过 / 提交0/0
题目描述
You are given a tree (an undirected connected graph without cycles) and an integer $s$ .
Vanya wants to put weights on all edges of the tree so that all weights are non-negative real numbers and their sum is $s$ . At the same time, he wants to make the diameter of the tree as small as possible.
Let's define the diameter of a weighed tree as the maximum sum of the weights of the edges lying on the path between two some vertices of the tree. In other words, the diameter of a weighed tree is the length of the longest simple path in the tree, where length of a path is equal to the sum of weights over all edges in the path.
Find the minimum possible diameter that Vanya can get.
Vanya wants to put weights on all edges of the tree so that all weights are non-negative real numbers and their sum is $s$ . At the same time, he wants to make the diameter of the tree as small as possible.
Let's define the diameter of a weighed tree as the maximum sum of the weights of the edges lying on the path between two some vertices of the tree. In other words, the diameter of a weighed tree is the length of the longest simple path in the tree, where length of a path is equal to the sum of weights over all edges in the path.
Find the minimum possible diameter that Vanya can get.
输入格式
The first line contains two integer numbers $n$ and $s$ ( $2 \leq n \leq 10^5$ , $1 \leq s \leq 10^9$ ) — the number of vertices in the tree and the sum of edge weights.
Each of the following $n−1$ lines contains two space-separated integer numbers $a_i$ and $b_i$ ( $1 \leq a_i, b_i \leq n$ , $a_i \neq b_i$ ) — the indexes of vertices connected by an edge. The edges are undirected.
It is guaranteed that the given edges form a tree.
Each of the following $n−1$ lines contains two space-separated integer numbers $a_i$ and $b_i$ ( $1 \leq a_i, b_i \leq n$ , $a_i \neq b_i$ ) — the indexes of vertices connected by an edge. The edges are undirected.
It is guaranteed that the given edges form a tree.
输出格式
Print the minimum diameter of the tree that Vanya can get by placing some non-negative real weights on its edges with the sum equal to $s$ .
Your answer will be considered correct if its absolute or relative error does not exceed $10^{-6}$ .
Formally, let your answer be $a$ , and the jury's answer be $b$ . Your answer is considered correct if $\frac {|a-b|} {max(1, b)} \leq 10^{-6}$ .
Your answer will be considered correct if its absolute or relative error does not exceed $10^{-6}$ .
Formally, let your answer be $a$ , and the jury's answer be $b$ . Your answer is considered correct if $\frac {|a-b|} {max(1, b)} \leq 10^{-6}$ .
输入输出样例
输入 #1
4 3 1 2 1 3 1 4
输出 #1
2.000000000000000000
输入 #2
6 1 2 1 2 3 2 5 5 4 5 6
输出 #2
0.500000000000000000
输入 #3
5 5 1 2 2 3 3 4 3 5
输出 #3
3.333333333333333333
In the first example it is necessary to put weights like this:
It is easy to see that the diameter of this tree is $2$ . It can be proved that it is the minimum possible diameter.
In the second example it is necessary to put weights like this:

It is easy to see that the diameter of this tree is $2$ . It can be proved that it is the minimum possible diameter.
In the second example it is necessary to put weights like this:

C++ 编辑器
输入
输出
可保存默认模板;新题优先使用已保存模板。
当前快捷键仅展示,暂不支持修改。
- 撤销
Ctrl / ⌘ + Z - 重做
Ctrl / ⌘ + Y - 查找
Ctrl / ⌘ + F - 全选
Ctrl / ⌘ + A - 复制
Ctrl / ⌘ + C - 剪切
Ctrl / ⌘ + X - 粘贴
Ctrl / ⌘ + V - 自动排版
工具栏排版按钮 - 草稿保存
编辑时自动保存到本机
历史
提交记录
状态说明时间源码
AI
作答助手
你好,我是作答助手。可以问思路、复杂度、样例含义或代码报错原因;不会直接给出完整 AC 代码。
确定要清空代码吗?
提交通过
评测结果:Accepted