#P17145. [NOI 2026] 彩虹树

    ID: 19493 远端评测题 1000ms 1024MiB 尝试: 0 已通过: 0 显示难度NOI/NOI+/CTS 上传者: 标签>动态规划 DP多项式NOI交互题动态规划优化树形 DP容斥原理差分2026

[NOI 2026] 彩虹树

Background

The statement and sample attachments come from QOJ

When submitting to Luogu, you do not need to include the header #include "rainbow.h"。Just copy

int rainbow(int c, int n, std::vector<int> f);

to the beginning of your program, and compile with a C++17 or higher compiler。

Problem Description

Legend says that in the Kingdom of Night, there is a Rainbow Tree with nn nodes。The Rainbow Tree is a rooted tree with nodes numbered from 0n10 \sim n-1。Node 00 is the root of the Rainbow Tree, and the parent of node ii (1i<n1 \le i < n) is fif_i

Each node on the Rainbow Tree can display any color。There are infinitely many colors, but the colorfulness of the Rainbow Tree depends only on the number of distinct colors that appear in each subtree, and does not depend on which colors they are。Specifically, let the subtree rooted at node ii (0i<n0 \le i < n) contain cic_i distinct colors among its nodes。Then the colorfulness of the Rainbow Tree can be represented by the sequence [c0,c1,,cn1][c_0, c_1, \ldots, c_{n-1}]
For example, in the figure below, nodes 0,1,50, 1, 5 are blue, nodes 2,42, 4 are red, and node 33 is yellow, so the colorfulness of the Rainbow Tree is [3,2,1,2,1,1][3,2,1,2,1,1]

:::align{center} :::

Changes in the clouds cause the colors of the Rainbow Tree to follow certain specific rules。Each rule can be described by a corresponding subset of nodes S{1,2,,n1}S \subseteq \{1,2,\ldots,n-1\}:for every node uu in set SS, the color of uu must be the same as the color of some ancestor。Formally, for all uSu \in S, there exists an ancestor pp of uu (pup \ne u) such that uu and pp have the same color。

Even with the restriction above, the Rainbow Tree can still display different colorings, producing different colorfulness sequences。Let wSw_S be the number of distinct kinds of colorfulness sequences of Rainbow Trees that satisfy rule SS。Two colorfulness sequences are considered different if and only if at least one element differs between the two sequences。

Compute, for all possible 2n12^{n-1} rules, the sum of the numbers of kinds of colorfulness sequences under each rule, i.e. S{1,2,,n1}wS\sum_{S \subseteq \{1,2,\ldots,n-1\}} w_S。Since the answer may be large, output the result modulo 998244353998244353

Input Format

【Implementation details】

Contestants do not need to, and should not, implement the main function。

Contestants need to ensure that the submitted program source file includes the header rainbow.h, i.e. add the following code at the beginning of the program:

#include "rainbow.h"

Contestants need to implement the following function in the submitted program source file rainbow.cpp:

int rainbow(int c, int n, std::vector<int> f);
  • c,nc, n represent the test point ID and the number of nodes of the Rainbow Tree, respectively。c=0c = 0 means this test point is the sample。
  • ff is a sequence of length nn, where f0=0f_0 = 0 and fif_i (1i<n1 \le i < n) is the parent of node ii
  • This function should return the sum, over all rules, of the number of kinds of colorfulness sequences modulo 998244353998244353
  • For each test point, this function will be called exactly once by the grader。

template_rainbow.cpp in this problem directory is the provided sample code。Contestants may refer to it and implement their own code。

Output Format

【Grader program mode】

Contestants can compile an executable file in this problem directory using the following command:

g++ grader.cpp rainbow.cpp -o rainbow -O2 -std=c++14 -static

For the compiled executable file rainbow:

  • The executable will read data from standard input in the following format:
    • The first line contains two non-negative integers c,nc, n
    • The second line contains n1n-1 non-negative integers f1,f2,,fn1f_1, f_2, \ldots, f_{n-1}
  • The executable will output data to standard output in the following format:
    • Output one line with one non-negative integer, which is the return value of the rainbow function。
0 3
0 0
8
0 6
0 1 0 3 1
279

Hint

【Sample 11 explanation】

  • For rule S=S = \varnothing, there are three possible colorfulness sequences: [1,1,1][1,1,1], [2,1,1][2,1,1], [3,1,1][3,1,1]
  • For rule S={1}S = \{1\}, there are two possible colorfulness sequences: [1,1,1][1,1,1], [2,1,1][2,1,1]
  • For rule S={2}S = \{2\}, there are two possible colorfulness sequences: [1,1,1][1,1,1], [2,1,1][2,1,1]
  • For rule S={1,2}S = \{1,2\}, there is one possible colorfulness sequence: [1,1,1][1,1,1]

Therefore, the answer is 3+2+2+1=83+2+2+1=8

【Sample 33

See rainbow/rainbow3.in and rainbow/rainbow3.ans in the contestants’ directory。

This sample satisfies the constraints of test points 3,43,4

【Sample 44

See rainbow/rainbow4.in and rainbow/rainbow4.ans in the contestants’ directory。

This sample satisfies the constraints of test points 575 \sim 7

【Sample 55

See rainbow/rainbow5.in and rainbow/rainbow5.ans in the contestants’ directory。

This sample satisfies the constraints of test points 8,98, 9

【Sample 66

See rainbow/rainbow6.in and rainbow/rainbow6.ans in the contestants’ directory。

This sample satisfies the constraints of test points 101210 \sim 12

【Constraints】

For all testdata:

  • 1n2001 \le n \le 200
  • For all 1i<n1 \le i < n, 0fi<i0 \le f_i < i

::cute-table{tuack} | Test point ID | nn \le | Special property | |:-:|:-:|:-:| | 11 | 44 | None | | 22 | 88 | ^ | | 3,43,4 | 1616 | ^ | | 575 \sim 7 | 5050 | ^ | | 8,98,9 | 10210^2 | BB | | 101210 \sim 12 | ^ | None | | 131513 \sim 15 | 150150 | ^ | | 1616 | 200200 | AA | | 171917 \sim 19 | ^ | BB | | 202520 \sim 25 | ^ | None |

Special property AA: For all 1i<n1 \le i < n, fi=i1f_i = i-1

Special property BB: For all 0i<n0 \le i < n, there are at most two jj such that fj=if_j = i

Translated by ChatGPT 5