A14271 | Tree Array
时间限制1s
内存限制256MB
通过 / 提交0/0
题目描述
You are given a tree consisting of $n$ nodes. You generate an array from the tree by marking nodes one by one.
Initially, when no nodes are marked, a node is equiprobably chosen and marked from the entire tree.
After that, until all nodes are marked, a node is equiprobably chosen and marked from the set of unmarked nodes with at least one edge to a marked node.
It can be shown that the process marks all nodes in the tree.
The final array $a$ is the list of the nodes' labels in order of the time each node was marked.
Find the expected number of inversions in the array that is generated by the tree and the aforementioned process.
The number of inversions in an array $a$ is the number of pairs of indices $(i, j)$ such that $i < j$ and $a_i > a_j$ . For example, the array $[4, 1, 3, 2]$ contains $4$ inversions: $(1, 2)$ , $(1, 3)$ , $(1, 4)$ , $(3, 4)$ .
Initially, when no nodes are marked, a node is equiprobably chosen and marked from the entire tree.
After that, until all nodes are marked, a node is equiprobably chosen and marked from the set of unmarked nodes with at least one edge to a marked node.
It can be shown that the process marks all nodes in the tree.
The final array $a$ is the list of the nodes' labels in order of the time each node was marked.
Find the expected number of inversions in the array that is generated by the tree and the aforementioned process.
The number of inversions in an array $a$ is the number of pairs of indices $(i, j)$ such that $i < j$ and $a_i > a_j$ . For example, the array $[4, 1, 3, 2]$ contains $4$ inversions: $(1, 2)$ , $(1, 3)$ , $(1, 4)$ , $(3, 4)$ .
输入格式
The first line contains a single integer $n$ ( $2 \le n \le 200$ ) — the number of nodes in the tree.
The next $n - 1$ lines each contains two integers $x$ and $y$ ( $1 \le x, y \le n$ ; $x \neq y$ ), denoting an edge between node $x$ and $y$ .
It's guaranteed that the given edges form a tree.
The next $n - 1$ lines each contains two integers $x$ and $y$ ( $1 \le x, y \le n$ ; $x \neq y$ ), denoting an edge between node $x$ and $y$ .
It's guaranteed that the given edges form a tree.
输出格式
Output the expected number of inversions in the generated array modulo $10^9+7$ .
Formally, let $M = 10^9+7$ . It can be shown that the answer can be expressed as an irreducible fraction $\frac{p}{q}$ , where $p$ and $q$ are integers and $q \not \equiv 0 \pmod{M}$ . Output the integer equal to $p \cdot q^{-1} \bmod M$ . In other words, output such an integer $x$ that $0 \le x < M$ and $x \cdot q \equiv p \pmod{M}$ .
Formally, let $M = 10^9+7$ . It can be shown that the answer can be expressed as an irreducible fraction $\frac{p}{q}$ , where $p$ and $q$ are integers and $q \not \equiv 0 \pmod{M}$ . Output the integer equal to $p \cdot q^{-1} \bmod M$ . In other words, output such an integer $x$ that $0 \le x < M$ and $x \cdot q \equiv p \pmod{M}$ .
输入输出样例
输入 #1
3 1 2 1 3
输出 #1
166666669
输入 #2
6 2 1 2 3 6 1 1 4 2 5
输出 #2
500000009
输入 #3
5 1 2 1 3 1 4 2 5
输出 #3
500000007
This is the tree from the first sample:
For the first sample, the arrays are almost fixed. If node $2$ is chosen initially, then the only possible array is $[2, 1, 3]$ ( $1$ inversion). If node $3$ is chosen initially, then the only possible array is $[3, 1, 2]$ ( $2$ inversions). If node $1$ is chosen initially, the arrays $[1, 2, 3]$ ( $0$ inversions) and $[1, 3, 2]$ ( $1$ inversion) are the only possibilities and equiprobable. In total, the expected number of inversions is $\frac{1}{3}\cdot 1 + \frac{1}{3} \cdot 2 + \frac{1}{3} \cdot (\frac{1}{2} \cdot 0 + \frac{1}{2} \cdot 1) = \frac{7}{6}$ .
$166666669 \cdot 6 = 7 \pmod {10^9 + 7}$ , so the answer is $166666669$ .
This is the tree from the second sample:
This is the tree from the third sample:

For the first sample, the arrays are almost fixed. If node $2$ is chosen initially, then the only possible array is $[2, 1, 3]$ ( $1$ inversion). If node $3$ is chosen initially, then the only possible array is $[3, 1, 2]$ ( $2$ inversions). If node $1$ is chosen initially, the arrays $[1, 2, 3]$ ( $0$ inversions) and $[1, 3, 2]$ ( $1$ inversion) are the only possibilities and equiprobable. In total, the expected number of inversions is $\frac{1}{3}\cdot 1 + \frac{1}{3} \cdot 2 + \frac{1}{3} \cdot (\frac{1}{2} \cdot 0 + \frac{1}{2} \cdot 1) = \frac{7}{6}$ .
$166666669 \cdot 6 = 7 \pmod {10^9 + 7}$ , so the answer is $166666669$ .
This is the tree from the second sample:
This is the tree from the third sample:

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