CF1930G.Prefix Max Set Counting
NOI/NOI+/CTSC
通过率:0%
时间限制:5.00s
内存限制:512MB
AC君温馨提醒
该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。
题目描述
Define a function f such that for an array b, f(b) returns the array of prefix maxima of b. In other words, f(b) is an array containing only those elements bi, for which bi=max(b1,b2,…,bi), without changing their order. For example, f([3,10,4,10,15,1])=[3,10,10,15].
You are given a tree consisting of n nodes rooted at 1.
A permutation† p of is considered a pre-order of the tree if for all i the following condition holds:
- Let k be the number of proper descendants‡ of node pi.
- For all x such that i<x≤i+k, px is a proper descendant of node pi.
Find the number of distinct values of f(a) over all possible pre-orders a. Since this value might be large, you only need to find it modulo 998244353.
† A permutation of length n is an array consisting of n distinct integers from 1 to n in arbitrary order. For example, [2,3,1,5,4] is a permutation, but [1,2,2] is not a permutation (2 appears twice in the array), and [1,3,4] is also not a permutation (n=3 but there is 4 in the array).
‡ Node t is a proper descendant of node s if s=t and s is on the unique simple path from t to 1.
定义函数 f,使得对于数组 b,f(b) 返回 b 的前缀最大值数组。换言之,f(b) 是一个仅包含满足 bi=max(b1,b2,…,bi) 的元素 bi 的数组,且保持这些元素在原数组中的相对顺序不变。例如,f([3,10,4,10,15,1])=[3,10,10,15]。
给定一棵由 n 个节点构成的树,根节点为 1。
若排列† p 满足对所有 i 下列条件,则称其为该树的一个先序遍历序列(pre-order):
- 设 k 为节点 pi 的真后代节点数‡;
- 对所有满足 i<x≤i+k 的 x,px 均为节点 pi 的真后代节点。
求在所有可能的先序遍历序列 a 上,f(a) 的不同取值个数。由于该数值可能很大,你只需输出其对 998244353 取模的结果。
† 长度为 n 的排列是指由 1 到 n 中 n 个互不相同的整数按任意顺序组成的数组。例如,[2,3,1,5,4] 是一个排列,而 [1,2,2] 不是排列(数字 2 出现了两次),[1,3,4] 也不是排列(此时 n=3,但数组中出现了 4)。
‡ 若 s=t,且 s 位于从 t 到根节点 1 的唯一简单路径上,则称节点 t 是节点 s 的真后代节点。
输入格式
Each test contains 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 a single integer n (1≤n≤106) — the number of vertices.
The following next n−1 lines contain two integers u and v (1≤u,v≤n, u=v) — denoting an edge between nodes u and v. It is guaranteed that the given edges form a tree.
It is guaranteed that the sum of n over all test cases does not exceed 106.
每个测试包含多个测试用例。第一行包含一个整数 t(1≤t≤105)—— 测试用例的数量。随后是各测试用例的描述。
每个测试用例的第一行包含一个整数 n(1≤n≤106)—— 顶点的数量。
接下来的 n−1 行每行包含两个整数 u 和 v(1≤u,v≤n,且 u=v)—— 表示节点 u 与节点 v 之间存在一条边。保证所给边构成一棵树。
保证所有测试用例中 n 的总和不超过 106。
输出格式
For each test case, output the number of distinct values of f(a) modulo 998244353 that you can get.
对于每个测试用例,输出你能得到的 f(a) 对 998244353 取模后的不同值的个数。
输入输出样例
输入#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]. So the only possible value of f(a) is [1].
In the second test case, the only valid pre-order is a=[1,2]. So the only possible value f(a) is [1,2].
In the third test case, the two valid pre-orders are a=[1,2,3] and a=[1,3,2]. So the possible values of f(a) are [1,2,3] and [1,3].
In the fifth test case, the possible values of f(a) are:
- [1,5];
- [1,2,5];
- [1,3,5];
- [1,4,5];
- [1,2,3,5];
- [1,2,4,5];
- [1,3,4,5];
- [1,2,3,4,5].
在第一个测试用例中,唯一有效的前序遍历为 a=[1]。因此 f(a) 的唯一可能取值为 [1]。
在第二个测试用例中,唯一有效的前序遍历为 a=[1,2]。因此 f(a) 的唯一可能取值为 [1,2]。
在第三个测试用例中,两个有效的前序遍历为 a=[1,2,3] 和 a=[1,3,2]。因此 f(a) 的可能取值为 [1,2,3] 和 [1,3]。
在第五个测试用例中,f(a) 的可能取值为:
- [1,5];
- [1,2,5];
- [1,3,5];
- [1,4,5];
- [1,2,3,5];
- [1,2,4,5];
- [1,3,4,5];
- [1,2,3,4,5]。
输入解题思路,AI测评打分。不知道怎么写?