CF1540B.Tree Array

提高+/省选-

通过率:0%

时间限制:2.00s

内存限制:256MB

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

You are given a tree consisting of nn 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 aa 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 aa is the number of pairs of indices (i,j)(i, j) such that i<ji \lt j and ai>aja_i \gt a_j. For example, the array [4,1,3,2][4, 1, 3, 2] contains 44 inversions: (1,2)(1, 2), (1,3)(1, 3), (1,4)(1, 4), (3,4)(3, 4).

给你一棵包含 nn 个节点的树。你将通过逐个标记节点的方式,从该树生成一个数组。

初始时,没有任何节点被标记,此时从整棵树中等概率地随机选择一个节点并将其标记。

此后,只要仍有未标记节点,就从所有至少有一条边与已标记节点相连的未标记节点中等概率地随机选择一个节点并将其标记。

可以证明,该过程最终会标记树中的所有节点。

最终得到的数组 aa 即为各节点按其被标记时间顺序排列的标签序列。

请计算:由该树及上述过程所生成的数组中,逆序对数量的期望值。

数组 aa 中的逆序对数量,定义为满足 i<ji \lt j 且 ai>aja_i \gt a_j 的下标对 (i,j)(i, j) 的个数。例如,数组 [4,1,3,2][4, 1, 3, 2] 包含 44 个逆序对:(1,2)(1, 2)、(1,3)(1, 3)、(1,4)(1, 4)、(3,4)(3, 4)。

输入格式

The first line contains a single integer nn (2≤n≤2002 \le n \le 200) — the number of nodes in the tree.

The next n−1n - 1 lines each contains two integers xx and yy (1≤x,y≤n1 \le x, y \le n; x≠yx \neq y), denoting an edge between node xx and yy.

It's guaranteed that the given edges form a tree.

第一行包含一个整数 nn(2≤n≤2002 \le n \le 200)—— 表示树中节点的数量。

接下来的 n−1n - 1 行,每行包含两个整数 xx 和 yy(1≤x,y≤n1 \le x, y \le n;x≠yx \neq y),表示节点 xx 与节点 yy 之间存在一条边。

保证所给的边构成一棵树。

输出格式

Output the expected number of inversions in the generated array modulo 109+710^9+7.

Formally, let M=109+7M = 10^9+7. It can be shown that the answer can be expressed as an irreducible fraction pq\frac{p}{q}, where pp and qq are integers and q≢0(modM)q \not \equiv 0 \pmod{M}. Output the integer equal to p⋅q−1 mod Mp \cdot q^{-1} \bmod M. In other words, output such an integer xx that 0≤x<M0 \le x \lt M and x⋅q≡p(modM)x \cdot q \equiv p \pmod{M}.

输出生成数组中逆序对的期望数量对 109+710^9+7 取模的结果。

形式化地,令 M=109+7M = 10^9+7。可以证明,该答案可表示为既约分数 pq\frac{p}{q},其中 pp 和 qq 为整数,且 q≢0(modM)q \not \equiv 0 \pmod{M}。请输出整数 p⋅q−1 mod Mp \cdot q^{-1} \bmod M。换言之,请输出满足 0≤x<M0 \le x \lt M 且 x⋅q≡p(modM)x \cdot q \equiv p \pmod{M} 的整数 xx。

输入输出样例

  • 输入#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 22 is chosen initially, then the only possible array is [2,1,3][2, 1, 3] (11 inversion). If node 33 is chosen initially, then the only possible array is [3,1,2][3, 1, 2] (22 inversions). If node 11 is chosen initially, the arrays [1,2,3][1, 2, 3] (00 inversions) and [1,3,2][1, 3, 2] (11 inversion) are the only possibilities and equiprobable. In total, the expected number of inversions is 13⋅1+13⋅2+13⋅(12⋅0+12⋅1)=76\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⋅6=7(mod109+7)166666669 \cdot 6 = 7 \pmod {10^9 + 7}, so the answer is 166666669166666669.

This is the tree from the second sample:

This is the tree from the third sample:

这是第一个样例中的树:

对于第一个样例,数组几乎被唯一确定。若初始选择节点 22,则唯一可能的数组为 [2,1,3][2, 1, 3](含 11 个逆序对);若初始选择节点 33,则唯一可能的数组为 [3,1,2][3, 1, 2](含 22 个逆序对);若初始选择节点 11,则仅可能得到数组 [1,2,3][1, 2, 3](含 00 个逆序对)和 [1,3,2][1, 3, 2](含 11 个逆序对),且二者等概率出现。因此,逆序对数的期望值为

13⋅1+13⋅2+13⋅(12⋅0+12⋅1)=76.\frac{1}{3}\cdot 1 + \frac{1}{3} \cdot 2 + \frac{1}{3} \cdot \left(\frac{1}{2} \cdot 0 + \frac{1}{2} \cdot 1\right) = \frac{7}{6}.

由于 166666669⋅6=7(mod109+7)166666669 \cdot 6 = 7 \pmod {10^9 + 7},故答案为 166666669166666669。

这是第二个样例中的树:

这是第三个样例中的树:

输入解题思路,AI测评打分。不知道怎么写?

首页