A12094 | Shrinking Tree
时间限制1s
内存限制256MB
通过 / 提交0/0
题目描述
Consider a tree $T$ (that is, a connected graph without cycles) with $n$ vertices labelled $1$ through $n$ . We start the following process with $T$ : while $T$ has more than one vertex, do the following:
- choose a random edge of $T$ equiprobably;
- shrink the chosen edge: if the edge was connecting vertices $v$ and $u$ , erase both $v$ and $u$ and create a new vertex adjacent to all vertices previously adjacent to either $v$ or $u$ . The new vertex is labelled either $v$ or $u$ equiprobably.
At the end of the process, $T$ consists of a single vertex labelled with one of the numbers $1, \ldots, n$ . For each of the numbers, what is the probability of this number becoming the label of the final vertex?
- choose a random edge of $T$ equiprobably;
- shrink the chosen edge: if the edge was connecting vertices $v$ and $u$ , erase both $v$ and $u$ and create a new vertex adjacent to all vertices previously adjacent to either $v$ or $u$ . The new vertex is labelled either $v$ or $u$ equiprobably.
At the end of the process, $T$ consists of a single vertex labelled with one of the numbers $1, \ldots, n$ . For each of the numbers, what is the probability of this number becoming the label of the final vertex?
输入格式
The first line contains a single integer $n$ ( $1 \leq n \leq 50$ ).
The following $n - 1$ lines describe the tree edges. Each of these lines contains two integers $u_i, v_i$ — labels of vertices connected by the respective edge ( $1 \leq u_i, v_i \leq n$ , $u_i \neq v_i$ ). It is guaranteed that the given graph is a tree.
The following $n - 1$ lines describe the tree edges. Each of these lines contains two integers $u_i, v_i$ — labels of vertices connected by the respective edge ( $1 \leq u_i, v_i \leq n$ , $u_i \neq v_i$ ). It is guaranteed that the given graph is a tree.
输出格式
Print $n$ floating numbers — the desired probabilities for labels $1, \ldots, n$ respectively. All numbers should be correct up to $10^{-6}$ relative or absolute precision.
输入输出样例
输入 #1
4 1 2 1 3 1 4
输出 #1
0.1250000000 0.2916666667 0.2916666667 0.2916666667
输入 #2
7 1 2 1 3 2 4 2 5 3 6 3 7
输出 #2
0.0850694444 0.0664062500 0.0664062500 0.1955295139 0.1955295139 0.1955295139 0.1955295139
In the first sample, the resulting vertex has label 1 if and only if for all three edges the label 1 survives, hence the probability is $1/2^3 = 1/8$ . All other labels have equal probability due to symmetry, hence each of them has probability $(1 - 1/8) / 3 = 7/24$ .
C++ 编辑器
输入
输出
可保存默认模板;新题优先使用已保存模板。
当前快捷键仅展示,暂不支持修改。
- 撤销
Ctrl / ⌘ + Z - 重做
Ctrl / ⌘ + Y - 查找
Ctrl / ⌘ + F - 全选
Ctrl / ⌘ + A - 复制
Ctrl / ⌘ + C - 剪切
Ctrl / ⌘ + X - 粘贴
Ctrl / ⌘ + V - 自动排版
工具栏排版按钮 - 草稿保存
编辑时自动保存到本机
历史
提交记录
状态说明时间源码
AI
作答助手
你好,我是作答助手。可以问思路、复杂度、样例含义或代码报错原因;不会直接给出完整 AC 代码。
确定要清空代码吗?
提交通过
评测结果:Accepted