#P4099. [HEOI2013] SAO

    ID: 3035 远端评测题 1000ms 125MiB 尝试: 0 已通过: 0 难度: 8 上传者: 标签>动态规划,dp2013各省省选河北枚举,暴力拓扑排序前缀和

[HEOI2013] SAO

Description

Welcome to SAO (Strange and Abnormal Online). This is a VR MMORPG with nn levels. However, deciding the order to challenge the levels is a big problem.

The game imposes n1n-1 constraints on the order of levels, such as level ii must be challenged before level jj, or you can attempt level ll only after finishing level kk. Moreover, if we ignore the directions of the constraints, then under these n1n-1 constraints, any two levels are connected in some way. That is, we cannot partition all levels into two nonempty disjoint subsets such that there is no constraint between the two subsets.

Input Format

The first line contains an integer TT, the number of test cases.

For each test case, the first line contains an integer nn, the number of levels. Then follow n1n - 1 lines, each in the form i sign ji \text{ sign } j, where 0i,jn10 \leq i, j \leq n - 1 and iji \neq j, and sign\text{sign} is <\tt{<} or >\tt{>}, meaning level ii must be completed before (if sign\text{sign} is <\tt{<}) or after (如果 sign\text{sign} is >\tt{>}) level jj.

Output Format

For each test case, output one integer on a single line: the number of valid orders to conquer the levels, output mod(109+7)\bmod (10^9+7).

2 
5 
0 < 2 
1 < 2 
2 < 3 
2 < 4 
4 
0 < 1 
0 < 2 
0 < 3
4 
6

Hint

  • For 20%20\% of the testdata, n10n \le 10.
  • For 40%40\% of the testdata, n100n \le 100.
  • For another 20%20\% of the testdata, it is guaranteed that sign\text{sign} is only <\tt{<} and i<ji<j.
  • For 100%100\% of the testdata, T5T \le 5 and 1n10001 \le n \le 1000.

Translated by ChatGPT 5