#P10789. [NOI2024] 登山

[NOI2024] 登山

Problem Description

"Why climb? Because the mountain is there."

There are nn locations on Muztagh Mountain, numbered from 11 to nn, where location 11 is the summit. These nn locations form a rooted tree, with node 11 as the root. For 2in2\leq i\leq n, the parent of node ii is node pip_i.

Let did_i be the number of edges on the path from node ii to the summit. Formally, d1=0d_1=0, and for 2in2\leq i\leq n, di=dpi+1d_i=d_{p_i}+1.

A climbing path is a plan that starts from some node among 2n2\sim n, and after several moves, reaches the summit.

A move starting from node i(2in)i(2\leq i\leq n) is one of the following two types:

  1. Sprint: within the given sprint range [li,ri][l_i,r_i], choose a positive integer kk such that likril_i\leq k\leq r_i, and move kk steps toward the summit, i.e. move to the kk-th ancestor of node ii in the rooted tree. It is guaranteed that 1liridi1\leq l_i\leq r_i\leq d_i.
  2. Rest: because the terrain of Muztagh Mountain is steep, when resting you will slip down to one of its children. Formally, choose a node jj such that pj=ip_j=i, and move to node jj. In particular, if node ii is a leaf in the rooted tree, then there is no jj satisfying pj=ip_j=i, so you cannot choose Rest in this case.

The climbing sequence corresponding to a climbing path is the sequence consisting of the starting node and every node reached after each move. Formally, the climbing sequence corresponding to a climbing path starting from node xx is a node sequence a1=x,a2,,am=1a_1=x,a_2,\dots,a_m=1 such that for 1i<m1\leq i<m, ai+1a_{i+1} is either the k(laikrai)k(l_{a_i}\leq k\leq r_{a_i})-th ancestor of aia_i, or satisfies pai+1=aip_{a_{i+1}}=a_i.

To ensure that every sprint can get closer to the summit, a legal climbing path must satisfy: for the starting node or any node ii reached after some move, every later node jj reached by sprinting must satisfy dj<dihid_j<d_i-h_i, where hih_i is a given parameter, guaranteed to satisfy 0hi<di0\leq h_i<d_i. Formally, a legal climbing path with climbing sequence a1,a2,,ama_1,a_2,\dots,a_m must satisfy: for all 1i<jm1\leq i<j\leq m, if pajaj1p_{a_j} \neq a_{j-1}, then daj<daihaid_{a_j}<d_{a_i}-h_{a_i}.

For all nodes 2n2\sim n, find the number of legal climbing paths starting from each of these nodes. Two climbing paths are different if and only if their corresponding climbing sequences are different. Since the answer may be large, you only need to output it modulo 998244353998\,244\,353.

Input Format

This problem contains multiple test cases.

The first line contains an integer cc, indicating the test point ID. c=0c=0 means this test point is the sample.

The second line contains an integer tt, the number of test cases.

Then each test case is given as follows:

The first line contains an integer nn, the number of locations on Muztagh Mountain.

The next n1n-1 lines: the (i1)(i-1)-th line (2in2\leq i\leq n) contains four integers pi,li,ri,hip_i,l_i,r_i,h_i. It is guaranteed that 1pi<i1\leq p_i<i, 1liridi1\leq l_i\leq r_i\leq d_i, and 0hi<di0\leq h_i<d_i.

Output Format

For each test case, output one line with n1n-1 integers, where the ii-th integer is the number of ways (modulo 998244353998\,244\,353) to reach the summit starting from node i+1i+1 (i.e. nodes 2n2\sim n).

0
3
5
1 1 1 0
2 1 1 0
2 1 2 1
4 2 3 0
6
1 1 1 0
2 1 2 0
3 1 3 2
4 1 4 1
5 1 5 3
6
1 1 1 0
2 1 2 0
2 1 2 0
3 1 2 0
3 2 3 2
3 3 2 4
5 9 3 21 6
4 10 5 14 1
见 mountain2.in/ans
这个样例满足测试点 2,3 的约束条件

见 mountain3.in/ans
这个样例满足测试点 9 的约束条件

见 mountain4.in/ans
这个样例满足测试点 11,12 的约束条件

见 mountain5.in/ans
这个样例满足测试点 13 的约束条件

Hint

[Sample 1 Explanation]

Sample 11 contains three test cases.

For the first test case, the structure of locations on Muztagh Mountain is as follows:

In this test case, d1=0d_1=0, d2=1d_2=1, d3=d4=2d_3=d_4=2, d5=3d_5=3.

There are 22 legal climbing paths starting from 44:

  1. Sprint directly to the 2-nd ancestor of 44, i.e. 11, reaching the summit. The corresponding climbing sequence is [4,1][4,1].
  2. Rest first and slip down to 55, then sprint from 55 to its 3-rd ancestor, reaching the summit. The corresponding climbing sequence is [4,5,1][4,5,1].

There are 44 legal climbing paths starting from 55:

  1. Sprint directly to the 3-rd ancestor of 55, i.e. 11, reaching the summit. The corresponding climbing sequence is [5,1][5,1].
  2. Sprint first to the 2-nd ancestor of 55, i.e. 22; then sprint from 22 to its 1-st ancestor, reaching the summit. The corresponding climbing sequence is [5,2,1][5,2,1].
  3. Sprint first to the 2-nd ancestor of 55, i.e. 22; then rest at 22 and slip down to 44; then sprint from 44 to its 2-nd ancestor, reaching the summit. The corresponding climbing sequence is [5,2,4,1][5,2,4,1].
  4. Sprint first to the 2-nd ancestor of 55, i.e. 22; then rest at 22 and slip down to 44; keep resting and slip down to 55; then sprint again from 55 to its 3-rd ancestor, reaching the summit. The corresponding climbing sequence is [5,2,4,5,1][5,2,4,5,1].

[Constraints]

For all test cases, it is guaranteed that: 1t41\leq t\leq 4, 2n1052\leq n\leq 10^5.

For any 2in2\leq i\leq n, it is guaranteed that: 1pi<i1\leq p_i<i, 1liridi1\leq l_i\leq r_i\leq d_i, 0hi<di0\leq h_i<d_i.

::cute-table{tuack}

Test point ID nn\leq Whether there exists li=ril_i=r_i Whether there exists hi=0h_i=0 Whether there exists pi=i1p_i=i-1
11 66 No
2,32,3 300300 ^
4,54,5 50005000
66 10510^5 Yes
77 ^ ^ ^ No
88 No Yes
99 ^ No
1010 No Yes
11,1211,12 ^ ^ No
1313 No Yes
142014\sim 20 ^ No

Translated by ChatGPT 5