#15871. [ICPC 2021 Macao R] Permutation on Tree

    ID: 15871 传统题 1000ms 512MiB 尝试: 0 已通过: 0 难度: (无) 上传者: 标签>P9663动态规划 DP2021O2优化树形 DPICPC澳门

[ICPC 2021 Macao R] Permutation on Tree

[ICPC 2021 Macao R] Permutation on Tree

题目描述

Given a tree with $n$ vertices where vertex $r$ is the root, we say a permutation $p_1, p_2, \cdots, p_n$ of $n$ is good if it satisfies the following constraint:
  • Let axa_x be the index of xx in the permutation (That is, pax=xp_{a_x} = x). For all 1≤u,v≤n1 \le u, v \le n, if vertex uu is an ancestor of vertex vv in the tree, then au<ava_u < a_v.

Define the score of a permutation to be ∑i=1n−1∣pi−pi+1∣\sum\limits_{i=1}^{n-1} |p_i - p_{i+1}| where ∣x∣|x| is the absolute value of xx. Calculate the sum of scores of all different good permutations.

输入格式

There is only one test case in each test file.

The first line contains two integers nn and rr (2≤n≤2002 \le n \le 200, 1≤r≤n1 \le r \le n) indicating the size of the tree and the root.

For the following (n−1)(n - 1) lines, the ii-th line contains two integers uiu_i and viv_i (1≤ui,vi≤n1 \le u_i, v_i \le n) indicating an edge connecting vertex uiu_i and viv_i in the tree.

输出格式

For each test case output one line containing one integer indicating the sum of scores of all different good permutations. As the answer may be large, output the answer modulo $(10^9 + 7)$.
4 2
1 2
2 3
1 4
15
3 1
1 2
2 3
2

提示

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}\{1, 2, 3\}. It's score is 22. 标签: P9663|动态规划 DP|2021|O2优化|树形 DP|ICPC|澳门

来源

P9663|[ICPC 2021 Macao R] Permutation on Tree