CF1929F.Sasha and the Wedding Binary Search Tree
普及/提高-
通过率:0%
AC君温馨提醒
该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。
题目描述
Having overcome all the difficulties and hardships, Sasha finally decided to marry his girlfriend. To do this, he needs to give her an engagement ring. However, his girlfriend does not like such romantic gestures, but she does like binary search trees † . So Sasha decided to give her such a tree.
After spending a lot of time on wedding websites for programmers, he found the perfect binary search tree with the root at vertex 1 . In this tree, the value at vertex v is equal to valv .
But after some time, he forgot the values in some vertices. Trying to remember the found tree, Sasha wondered — how many binary search trees could he have found on the website, if it is known that the values in all vertices are integers in the segment [1,C] . Since this number can be very large, output it modulo 998244353 .
† A binary search tree is a rooted binary tree in which for any vertex x , the following property holds: the values of all vertices in the left subtree of vertex x (if it exists) are less than or equal to the value at vertex x , and the values of all vertices in the right subtree of vertex x (if it exists) are greater than or equal to the value at vertex x .
输入格式
Each test consists of multiple test cases. The first line contains a single integer t ( 1≤t≤105 ) — the number of test cases. The description of the test cases follows.
The first line of each test case contains two integers n and C ( 2≤n≤5⋅105 , 1≤C≤109 ) — the number of vertices in the tree and the maximum allowed value at the vertex.
The next n lines describe the vertices of the tree. The i -th line contains three integers Li,Ri and vali ( −1≤Li,Ri≤n , −1≤vali≤C , Li,Ri,vali=0 ) — the number of the left child, the number of the right child, and the value at the i -th vertex, respectively. If Li=−1 , then the i -th vertex has no left son. If Ri=−1 , then the i -th vertex has no right son. If vali=−1 , then the value at the i -th vertex is unknown.
It is guaranteed that at least one suitable binary search tree exists.
It is guaranteed that the sum of n over all test cases does not exceed 5⋅105 .
输出格式
For each test case, output a single integer — the number of suitable binary search trees modulo 998244353 .
输入输出样例
输入#1
3 5 5 2 3 -1 -1 -1 2 4 -1 3 -1 5 -1 -1 -1 -1 3 69 2 3 47 -1 -1 13 -1 -1 69 3 3 2 3 -1 -1 -1 -1 -1 -1 -1
输出#1
4 1 10
说明/提示
In the first test case, the binary search tree has the following form:
Then the possible values at the vertices are: [2,2,3,2,2] , [2,2,3,2,3] , [2,2,3,3,3] , and [3,2,3,3,3] .
In the second test case, the values at all vertices are known, so there is only one suitable binary search tree.