#P12639. [UOI 2020] Topological Sorting of a Tree

    ID: 14188 远端评测题 1000ms 256MiB 尝试: 0 已通过: 0 难度: 6 上传者: 标签>动态规划 DP2020树形 DP拓扑排序组合数学前缀和容斥原理UOI(乌克兰)

[UOI 2020] Topological Sorting of a Tree

题目描述

You are given a tree with nn vertices, numbered from 11 to nn. The root of the tree is the vertex with number 11. For each vv from 22 to nn, let's define pvp_v as the number of the vertex adjacent to vv that lies on the simple path between vertex vv and the root. Each edge (pv,v)(p_v, v) is labeled with the symbol <\tt{<} or >\tt{>}.

Find the number of ways to place the numbers from 11 to nn in the vertices of the tree, such that each number is used exactly once and all the relationships indicated on the edges are satisfied. That is, for all edges with the symbol <\tt{<}, a[pv]<a[v]a[p_v] < a[v] should hold, and for all edges with the symbol >\tt{>}, a[pv]>a[v]a[p_v] > a[v] should hold.

Since the answer can be very large, output it modulo 109+710^9 + 7.

输入格式

The first line contains a single integer nn (1n30001 \leq n \leq 3\,000) - the number of vertices in the tree.

Each of the next n1n-1 lines contains a single integer pip_i (1pi<i1 \leq p_i < i) and a character cic_i (ci{<,>}c_i \in \{\tt{<}, \tt{>}\}), indicating that the edge between vertices with indices pip_i and ii is labeled with the symbol cic_i. Note that the indexing starts from 22.

输出格式

Output a single integer - the number of ways to arrange all numbers from 11 to nn in the vertices of the tree such that all the relationships indicated on the edges are satisfied. Note that you should output the remainder of the division by 109+710^9 + 7, not the actual answer.

4
1 <
2 <
3 >
3
4
1 <
1 <
1 <
6
5
1 <
1 <
3 >
3 >
18

提示

  • (88 points) n10n \leq 10;
  • (66 points) n18n \leq 18;
  • (1010 points) ci=<c_i = \tt{<};
  • (44 points) pi=1p_i = 1;
  • (1313 points) pi=i1,1n200p_i = i - 1, 1 \leq n \leq 200;
  • (1919 points) pi=i1p_i = i - 1;
  • (2424 points) n200n \leq 200;
  • (1616 points) no additional constraints.