CF2182G.Short Garland

省选/NOI-

通过率:0%

时间限制:4.00s

内存限制:1024MB

AC君温馨提醒

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

题目描述

Monocarp wants to hang a garland on the Christmas tree.

The Christmas tree is a tree with nn vertices rooted at vertex 11. The distance between two vertices of the tree is the number of edges on the shortest path between them, and the depth of a vertex is the distance from it to the root.

The garland consists of nn bulbs connected by wire and numbered from 11 to nn. The length of the wire between every two adjacent bulbs is kk.

The garland must be hung on the tree according to the following rules:

  • every bulb must be placed on one of the vertices of the tree, and every vertex must have exactly one bulb on it;
  • bulb 11 must be placed on the root of the tree;
  • each subsequent bulb has to be placed on such a vertex that its parent vertex has a bulb on it. If there are multiple such vertices, the vertex with the largest depth is chosen. If there are still multiple options, any of them can be chosen;
  • for each pair of subsequent bulbs, the distance between the vertices they are placed on must not exceed kk.

Your task is to count the number of ways to hang the garland while following all the rules and output it modulo 998244353998244353. Two ways are considered different if there exists at least one integer i∈[1,n]i \in [1, n] such that the ii-th bulb is placed on different vertices in these two ways.

Monocarp 想要在一棵圣诞树上悬挂一串彩灯。

这棵圣诞树是一棵含 nn 个顶点的有根树,根节点为顶点 11。树中两个顶点之间的距离定义为它们之间最短路径上的边数;一个顶点的深度定义为该顶点到根节点的距离。

这串彩灯由 nn 个灯泡通过导线连接而成,灯泡编号为 11 至 nn。每两个相邻灯泡之间的导线长度为 kk。

彩灯必须按照以下规则悬挂在树上:

  • 每个灯泡必须放置在树的一个顶点上,且每个顶点上恰好放置一个灯泡;
  • 灯泡 11 必须放置在树的根节点上;
  • 对于后续每一个灯泡(即编号为 i=2,3,…,ni = 2, 3, \dots, n 的灯泡),它必须被放置在一个父节点已放置有灯泡的顶点上;若存在多个满足条件的顶点,则选择其中深度最大的那个;若仍存在多个候选顶点,则可任选其一;
  • 对于每一对相邻编号的灯泡(即灯泡 ii 和灯泡 i+1i+1,其中 i=1,2,…,n−1i = 1, 2, \dots, n-1),它们所放置的顶点之间的距离不得超过 kk。

你的任务是计算所有满足上述全部规则的悬挂方式总数,并将结果对 998244353998244353 取模后输出。若存在某个整数 i∈[1,n]i \in [1, n],使得在这两种方式中第 ii 个灯泡被放置在不同的顶点上,则认为这两种方式不同。

输入格式

The first line contains a single integer tt (1≤t≤1041 \le t \le 10^4) — the number of test cases.

The first line of each test case contains two integers nn and kk (2≤n≤3⋅1052 \le n \le 3 \cdot 10^5; 1≤k<n1 \le k \lt n) — the number of vertices in the tree and the distance constraint between two consecutive bulbs.

The second line contains n−1n-1 integers p2,p3,…,pnp_2, p_3, \dots, p_n (1≤pi<i1 \le p_i \lt i), where pip_i is the parent of the ii-th vertex in the tree.

Additional constraint on the input: the sum of nn over all test cases does not exceed 3⋅1053 \cdot 10^5.

第一行包含一个整数 tt(1≤t≤1041 \le t \le 10^4)—— 测试用例的数量。

每个测试用例的第一行包含两个整数 nn 和 kk(2≤n≤3⋅1052 \le n \le 3 \cdot 10^5;1≤k<n1 \le k \lt n)—— 树中顶点的数量以及相邻两个灯泡之间的距离约束。

每个测试用例的第二行包含 n−1n-1 个整数 p2,p3,…,pnp_2, p_3, \dots, p_n(1≤pi<i1 \le p_i \lt i),其中 pip_i 表示树中第 ii 个顶点的父节点。

输入的附加约束:所有测试用例的 nn 值之和不超过 3⋅1053 \cdot 10^5。

输出格式

For each test case, output one integer — the number of ways to hang the garland while following all the rules, modulo 998244353998244353.

对于每个测试用例,输出一个整数——在遵守所有规则的前提下悬挂彩灯的方式数目,对 998244353998244353 取模。

输入输出样例

  • 输入#1

    4
    5 1
    1 1 2 2
    5 2
    1 1 2 2
    5 3
    1 1 2 2
    8 4
    1 1 3 2 3 1 4

    输出#1

    0
    2
    4
    12

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

首页