CF1929F.Sasha and the Wedding Binary Search Tree

提高+/省选-

通过率:0%

时间限制:2.00s

内存限制:256MB

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†^{\dagger}. 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 11. In this tree, the value at vertex vv is equal to valvval_v.

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][1, C]. Since this number can be very large, output it modulo 998 244 353998\,244\,353.

†^{\dagger}A binary search tree is a rooted binary tree in which for any vertex xx, the following property holds: the values of all vertices in the left subtree of vertex xx (if it exists) are less than or equal to the value at vertex xx, and the values of all vertices in the right subtree of vertex xx (if it exists) are greater than or equal to the value at vertex xx.

在克服了所有困难与艰辛之后,萨沙终于决定迎娶他的女友。为此,他需要送她一枚订婚戒指。然而,他的女友并不喜欢这种浪漫的举动,但她却钟爱二叉搜索树†^{\dagger}。于是萨沙决定送她这样一棵二叉搜索树。

他在程序员专用的婚礼网站上花费了大量时间,最终找到了一棵完美的二叉搜索树,其根节点为顶点 11。在这棵树中,顶点 vv 处的值为 valvval_v。

但过了一段时间后,他忘记了某些顶点上的数值。为了努力回忆起这棵已找到的树,萨沙不禁思考:若已知所有顶点上的值均为区间 [1,C][1, C] 内的整数,则该网站上可能存在的满足条件的二叉搜索树一共有多少棵?由于这个数目可能非常大,请将结果对 998 244 353998\,244\,353 取模后输出。

†^{\dagger} 二叉搜索树是一棵有根二叉树,满足如下性质:对任意顶点 xx,其左子树(若存在)中所有顶点的值均小于或等于顶点 xx 的值,而其右子树(若存在)中所有顶点的值均大于或等于顶点 xx 的值。

输入格式

Each test consists of multiple test cases. The first line contains a single integer tt (1≤t≤1051 \le t \le 10^5) — the number of test cases. The description of the test cases follows.

The first line of each test case contains two integers nn and CC (2≤n≤5⋅1052 \leq n \leq 5 \cdot 10^5, 1≤C≤1091 \leq C \leq 10^9) — the number of vertices in the tree and the maximum allowed value at the vertex.

The next nn lines describe the vertices of the tree. The ii-th line contains three integers Li,RiL_i, R_i and valival_i (−1≤Li,Ri≤n-1 \le L_i, R_i \le n, −1≤vali≤C-1 \le val_i \le C, Li,Ri,vali≠0L_i, R_i, val_i \ne 0) — the number of the left child, the number of the right child, and the value at the ii-th vertex, respectively. If Li=−1L_i = -1, then the ii-th vertex has no left son. If Ri=−1R_i = -1, then the ii-th vertex has no right son. If vali=−1val_i = -1, then the value at the ii-th vertex is unknown.

It is guaranteed that at least one suitable binary search tree exists.

It is guaranteed that the sum of nn over all test cases does not exceed 5⋅1055 \cdot 10^5.

每个测试包含多个测试用例。第一行包含一个整数 tt(1≤t≤1051 \le t \le 10^5),表示测试用例的数量。随后是各测试用例的描述。

每个测试用例的第一行包含两个整数 nn 和 CC(2≤n≤5⋅1052 \leq n \leq 5 \cdot 10^5,1≤C≤1091 \leq C \leq 10^9),分别表示树中顶点的数量以及顶点上允许的最大值。

接下来的 nn 行描述树的各个顶点。第 ii 行包含三个整数 LiL_i、RiR_i 和 valival_i(−1≤Li,Ri≤n-1 \le L_i, R_i \le n,−1≤vali≤C-1 \le val_i \le C,且 Li,Ri,vali≠0L_i, R_i, val_i \ne 0),分别表示第 ii 个顶点的左子节点编号、右子节点编号以及该顶点上的值。若 Li=−1L_i = -1,则第 ii 个顶点无左子节点;若 Ri=−1R_i = -1,则第 ii 个顶点无右子节点;若 vali=−1val_i = -1,则第 ii 个顶点上的值未知。

保证至少存在一棵满足条件的二叉搜索树。

保证所有测试用例中 nn 的总和不超过 5⋅1055 \cdot 10^5。

输出格式

For each test case, output a single integer — the number of suitable binary search trees modulo 998 244 353998\,244\,353.

对于每个测试用例,输出一个整数——满足条件的二叉搜索树的数量对 998 244 353998\,244\,353 取模的结果。

输入输出样例

  • 输入#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, 2], [2,2,3,2,3][2, 2, 3, 2, 3], [2,2,3,3,3][2, 2, 3, 3, 3], and [3,2,3,3,3][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.

在第一个测试用例中,二叉搜索树具有如下结构:

此时,各顶点可能的取值为:[2,2,3,2,2][2, 2, 3, 2, 2]、[2,2,3,2,3][2, 2, 3, 2, 3]、[2,2,3,3,3][2, 2, 3, 3, 3] 和 [3,2,3,3,3][3, 2, 3, 3, 3]。

在第二个测试用例中,所有顶点的值均已确定,因此仅存在唯一一棵满足条件的二叉搜索树。

输入解题思路,AI测评打分。不知道怎么写?

首页