CF1930G.Prefix Max Set Counting

NOI/NOI+/CTSC

通过率:0%

时间限制:5.00s

内存限制:512MB

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

Define a function ff such that for an array bb, f(b)f(b) returns the array of prefix maxima of bb. In other words, f(b)f(b) is an array containing only those elements bib_i, for which bi=max⁡(b1,b2,…,bi)b_i=\max(b_1,b_2,\ldots,b_i), without changing their order. For example, f([3,10,4,10,15,1])=[3,10,10,15]f([3,10,4,10,15,1])=[3,10,10,15].

You are given a tree consisting of nn nodes rooted at 11.

A permutation†^\dagger pp of is considered a pre-order of the tree if for all ii the following condition holds:

  • Let kk be the number of proper descendants‡^\ddagger of node pip_i.
  • For all xx such that i<x≤i+ki \lt x \leq i+k, pxp_x is a proper descendant of node pip_i.

Find the number of distinct values of f(a)f(a) over all possible pre-orders aa. Since this value might be large, you only need to find it modulo 998 244 353998\,244\,353.

†^\dagger A permutation of length nn is an array consisting of nn distinct integers from 11 to nn in arbitrary order. For example, [2,3,1,5,4][2,3,1,5,4] is a permutation, but [1,2,2][1,2,2] is not a permutation (22 appears twice in the array), and [1,3,4][1,3,4] is also not a permutation (n=3n=3 but there is 44 in the array).

‡^\ddagger Node tt is a proper descendant of node ss if s≠ts \neq t and ss is on the unique simple path from tt to 11.

定义函数 ff,使得对于数组 bb,f(b)f(b) 返回 bb 的前缀最大值数组。换言之,f(b)f(b) 是一个仅包含满足 bi=max⁡(b1,b2,…,bi)b_i = \max(b_1, b_2, \ldots, b_i) 的元素 bib_i 的数组,且保持这些元素在原数组中的相对顺序不变。例如,f([3,10,4,10,15,1])=[3,10,10,15]f([3,10,4,10,15,1]) = [3,10,10,15]。

给定一棵由 nn 个节点构成的树,根节点为 11。

若排列†^\dagger pp 满足对所有 ii 下列条件,则称其为该树的一个先序遍历序列(pre-order):

  • 设 kk 为节点 pip_i 的真后代节点数‡^\ddagger;
  • 对所有满足 i<x≤i+ki < x \leq i + k 的 xx,pxp_x 均为节点 pip_i 的真后代节点。

求在所有可能的先序遍历序列 aa 上,f(a)f(a) 的不同取值个数。由于该数值可能很大,你只需输出其对 998 244 353998\,244\,353 取模的结果。

†^\dagger 长度为 nn 的排列是指由 11 到 nn 中 nn 个互不相同的整数按任意顺序组成的数组。例如,[2,3,1,5,4][2,3,1,5,4] 是一个排列,而 [1,2,2][1,2,2] 不是排列(数字 22 出现了两次),[1,3,4][1,3,4] 也不是排列(此时 n=3n = 3,但数组中出现了 44)。

‡^\ddagger 若 s≠ts \neq t,且 ss 位于从 tt 到根节点 11 的唯一简单路径上,则称节点 tt 是节点 ss 的真后代节点。

输入格式

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

The first line of each test case contains a single integer nn (1≤n≤1061 \leq n \leq 10^6) — the number of vertices.

The following next n−1n-1 lines contain two integers uu and vv (1≤u,v≤n1 \leq u, v \leq n, u≠vu \neq v) — denoting an edge between nodes uu and vv. It is guaranteed that the given edges form a tree.

It is guaranteed that the sum of nn over all test cases does not exceed 10610^6.

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

每个测试用例的第一行包含一个整数 nn(1≤n≤1061 \leq n \leq 10^6)—— 顶点的数量。

接下来的 n−1n-1 行每行包含两个整数 uu 和 vv(1≤u,v≤n1 \leq u, v \leq n,且 u≠vu \neq v)—— 表示节点 uu 与节点 vv 之间存在一条边。保证所给边构成一棵树。

保证所有测试用例中 nn 的总和不超过 10610^6。

输出格式

For each test case, output the number of distinct values of f(a)f(a) modulo 998 244 353998\,244\,353 that you can get.

对于每个测试用例,输出你能得到的 f(a)f(a) 对 998 244 353998\,244\,353 取模后的不同值的个数。

输入输出样例

  • 输入#1

    6
    1
    2
    1 2
    3
    1 2
    1 3
    3
    1 2
    2 3
    5
    1 2
    1 3
    1 4
    1 5
    10
    1 2
    2 3
    1 4
    2 5
    2 6
    4 7
    5 8
    4 9
    9 10

    输出#1

    1
    1
    2
    1
    8
    6

说明/提示

In the first test case, the only valid pre-order is a=[1]a=[1]. So the only possible value of f(a)f(a) is [1][1].

In the second test case, the only valid pre-order is a=[1,2]a=[1,2]. So the only possible value f(a)f(a) is [1,2][1,2].

In the third test case, the two valid pre-orders are a=[1,2,3]a=[1,2,3] and a=[1,3,2]a=[1,3,2]. So the possible values of f(a)f(a) are [1,2,3][1,2,3] and [1,3][1,3].

In the fifth test case, the possible values of f(a)f(a) are:

  • [1,5][1,5];
  • [1,2,5][1,2,5];
  • [1,3,5][1,3,5];
  • [1,4,5][1,4,5];
  • [1,2,3,5][1,2,3,5];
  • [1,2,4,5][1,2,4,5];
  • [1,3,4,5][1,3,4,5];
  • [1,2,3,4,5][1,2,3,4,5].

在第一个测试用例中,唯一有效的前序遍历为 a=[1]a=[1]。因此 f(a)f(a) 的唯一可能取值为 [1][1]。

在第二个测试用例中,唯一有效的前序遍历为 a=[1,2]a=[1,2]。因此 f(a)f(a) 的唯一可能取值为 [1,2][1,2]。

在第三个测试用例中,两个有效的前序遍历为 a=[1,2,3]a=[1,2,3] 和 a=[1,3,2]a=[1,3,2]。因此 f(a)f(a) 的可能取值为 [1,2,3][1,2,3] 和 [1,3][1,3]。

在第五个测试用例中,f(a)f(a) 的可能取值为:

  • [1,5][1,5];
  • [1,2,5][1,2,5];
  • [1,3,5][1,3,5];
  • [1,4,5][1,4,5];
  • [1,2,3,5][1,2,3,5];
  • [1,2,4,5][1,2,4,5];
  • [1,3,4,5][1,3,4,5];
  • [1,2,3,4,5][1,2,3,4,5]。

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

首页