P9663 [ICPC 2021 Macao R] Permutation on Tree
题目描述
给定一个有 $n$ 个顶点的树,其中顶点 $r$ 是根,如果一个排列 $p_1, p_2, \cdots, p_n$ 满足以下约束条件,我们称其为好排列:
- 设 $a_x$ 是排列中 $x$ 的索引(即 $p_{a_x} = x$)。对于所有 $1 \le u, v \le n$,如果顶点 $u$ 是树中顶点 $v$ 的祖先,则 $a_u < a_v$。
定义排列的分数为 $\sum\limits_{i=1}^{n-1} |p_i - p_{i+1}|$,其中 $|x|$ 表示 $x$ 的绝对值。计算所有不同好排列的分数之和。
输入格式
无
输出格式
无
说明/提示
For the first sample test case, there are three good permutations: $\{2, 1, 3, 4\}$, $\{2, 1, 4, 3\}$ and $\{2, 3, 1, 4\}$. Their scores are $4$, $5$ and $6$ respectively so the answer is $4 + 5 + 6 = 15$.
For the second sample test case, there is only one good permutation: $\{1, 2, 3\}$. It's score is $2$.