#P17145. [NOI 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 nodes。The Rainbow Tree is a rooted tree with nodes numbered from 。Node is the root of the Rainbow Tree, and the parent of node () is 。
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 () contain distinct colors among its nodes。Then the colorfulness of the Rainbow Tree can be represented by the sequence 。
For example, in the figure below, nodes are blue, nodes are red, and node is yellow, so the colorfulness of the Rainbow Tree is 。
:::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 :for every node in set , the color of must be the same as the color of some ancestor。Formally, for all , there exists an ancestor of () such that and have the same color。
Even with the restriction above, the Rainbow Tree can still display different colorings, producing different colorfulness sequences。Let be the number of distinct kinds of colorfulness sequences of Rainbow Trees that satisfy rule 。Two colorfulness sequences are considered different if and only if at least one element differs between the two sequences。
Compute, for all possible rules, the sum of the numbers of kinds of colorfulness sequences under each rule, i.e. 。Since the answer may be large, output the result modulo 。
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);
- represent the test point ID and the number of nodes of the Rainbow Tree, respectively。 means this test point is the sample。
- is a sequence of length , where and () is the parent of node 。
- This function should return the sum, over all rules, of the number of kinds of colorfulness sequences modulo 。
- 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 。
- The second line contains non-negative integers 。
- 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
rainbowfunction。
- Output one line with one non-negative integer, which is the return value of the
0 3
0 0
8
0 6
0 1 0 3 1
279
Hint
【Sample explanation】
- For rule , there are three possible colorfulness sequences: , , 。
- For rule , there are two possible colorfulness sequences: , 。
- For rule , there are two possible colorfulness sequences: , 。
- For rule , there is one possible colorfulness sequence: 。
Therefore, the answer is 。
【Sample 】
See rainbow/rainbow3.in and rainbow/rainbow3.ans in the contestants’ directory。
This sample satisfies the constraints of test points 。
【Sample 】
See rainbow/rainbow4.in and rainbow/rainbow4.ans in the contestants’ directory。
This sample satisfies the constraints of test points 。
【Sample 】
See rainbow/rainbow5.in and rainbow/rainbow5.ans in the contestants’ directory。
This sample satisfies the constraints of test points 。
【Sample 】
See rainbow/rainbow6.in and rainbow/rainbow6.ans in the contestants’ directory。
This sample satisfies the constraints of test points 。
【Constraints】
For all testdata:
- ;
- For all , 。
::cute-table{tuack} | Test point ID | | Special property | |:-:|:-:|:-:| | | | None | | | | ^ | | | | ^ | | | | ^ | | | | | | | ^ | None | | | | ^ | | | | | | | ^ | | | | ^ | None |
Special property : For all , 。
Special property : For all , there are at most two such that 。
Translated by ChatGPT 5