QOJ.ac

QOJ

Time Limit: 1 s Memory Limit: 1024 MB Total points: 100 Difficulty: [show] Hackable ✓

#18988. Rainbow Tree

Statistics

Legend has it that in 夜之国, there is a rainbow tree containing $n$ vertices. The rainbow tree is a rooted tree whose vertices are numbered from $0$ to $n-1$. Vertex $0$ is its root, and the parent of vertex $i$ ($1 \le i < n$) is $f_i$.

Each vertex of 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 appearing in each subtree, rather than on the specific colors. More precisely, let $c_i$ be the number of distinct colors among the vertices in the subtree rooted at vertex $i$ ($0 \le i < n$). Then the colorfulness of the rainbow tree can be represented by the sequence $[c_0,c_1,\ldots,c_{n-1}]$. For example, in the figure below, vertices $0,1,5$ are blue, vertices $2,4$ are red, and vertex $3$ is yellow. Therefore, the colorfulness of this rainbow tree is $[3,2,1,2,1,1]$.

problem_18988_27189dfce5c437d06699da0fd9c6fdbd.png

Changes in the clouds cause the colors of the rainbow tree to follow a particular pattern. Specifically, each pattern can be described by a corresponding subset of vertices $S \subseteq \{1,2,\ldots,n-1\}$: the color of each vertex $u$ in $S$ is the same as that of some ancestor. Formally, for every $u \in S$, there exists an ancestor $p$ of $u$ ($p \ne u$) such that $u$ and $p$ have the same color. Despite these restrictions, the rainbow tree can still display different colors and thus have different colorfulness sequences. Let $w_S$ denote the number of distinct colorfulness sequences of rainbow trees satisfying pattern $S$. Two colorfulness sequences are different if and only if they differ in at least one element.

Compute the sum of the numbers of distinct colorfulness sequences of rainbow trees satisfying each of the $2^{n-1}$ possible patterns, that is, $\sum_{S \subseteq \{1,2,\ldots,n-1\}} w_S$. Since the answer may be large, you only need to find its remainder modulo $998\,244\,353$.

Implementation Details

You do not need to, and should not, implement the main function.

You must ensure that your submitted source file includes the header rainbow.h, by adding the following code at the beginning of your program:

#include "rainbow.h"

You must implement the following function in your submitted source file rainbow.cpp:

int rainbow(int c, int n, std::vector<int> f);
  • $c$ and $n$ are the test case number and the number of vertices in the rainbow tree, respectively. $c=0$ indicates that the test case is an example.
  • $f$ is a sequence of length $n$, where $f_0=0$ and $f_i$ ($1 \le i < n$) is the parent of vertex $i$.
  • This function must return the sum of the numbers of distinct colorfulness sequences of rainbow trees satisfying each pattern, modulo $998\,244\,353$.
  • For each test case, the grader will call this function exactly once.

The file template_rainbow.cpp in this task's directory contains the provided sample code. You may refer to it when implementing your own code.

Testing Your Program

You can compile an executable in this task's directory using the following command:

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

For the resulting executable rainbow:

  • The executable reads data from standard input in the following format:

    • The first line contains two nonnegative integers $c,n$.
    • The second line contains $n-1$ nonnegative integers $f_1,f_2,\ldots,f_{n-1}$.
  • The executable writes data to standard output in the following format:

    • Output one line containing a nonnegative integer, the return value of the rainbow function.

Examples

Input 1

0 3
0 0

Output 1

8

Note

  • The rainbow tree has three possible colorfulness sequences satisfying pattern $S=\varnothing$: $[1,1,1]$, $[2,1,1]$, and $[3,1,1]$.
  • The rainbow tree has two possible colorfulness sequences satisfying pattern $S=\{1\}$: $[1,1,1]$ and $[2,1,1]$.
  • The rainbow tree has two possible colorfulness sequences satisfying pattern $S=\{2\}$: $[1,1,1]$ and $[2,1,1]$.
  • The rainbow tree has one possible colorfulness sequence satisfying pattern $S=\{1,2\}$: $[1,1,1]$.

Therefore, the answer is $3+2+2+1=8$.

Input 2

0 6
0 1 0 3 1

Output 2

279

Example 3

See rainbow/rainbow3.in and rainbow/rainbow3.ans in the contestant directory.

This example satisfies the constraints of test cases $3,4$.

Example 4

See rainbow/rainbow4.in and rainbow/rainbow4.ans in the contestant directory.

This example satisfies the constraints of test cases $5$ through $7$.

Example 5

See rainbow/rainbow5.in and rainbow/rainbow5.ans in the contestant directory.

This example satisfies the constraints of test cases $8,9$.

Example 6

See rainbow/rainbow6.in and rainbow/rainbow6.ans in the contestant directory.

This example satisfies the constraints of test cases $10$ through $12$.

Constraints

For all test data:

  • $1 \le n \le 200$;
  • For every $1 \le i < n$, $0 \le f_i < i$.
Test case number $n \le$ Special property
$1$ $4$ None
$2$ $8$
$3,4$ $16$
$5$–$7$ $50$
$8,9$ $10^2$ B
$10$–$12$ None
$13$–$15$ $150$
$16$ $200$ A
$17$–$19$ B
$20$–$25$ None

Special property A: For every $1 \le i < n$, $f_i=i-1$.

Special property B: For every $0 \le i < n$, there are at most two values of $j$ such that $f_j=i$.

Editorials

IDTypeStatusTitlePosted ByLast UpdatedActions
EditorialOpen Official EditorialQingyu- Download
#2220EditorialOpenqwqGotoHiotoriView
#2609EditorialOpen$O(n^2 \log^2 n)$ 题解 by ChatGPTAnonymousView

Discussions

About Discussions

The discussion section is only for posting: General Discussions (problem-solving strategies, alternative approaches), and Off-topic conversations.

This is NOT for reporting issues! If you want to report bugs or errors, please use the Issues section below.

Open Discussions 0
No discussions in this category.

Issues

About Issues

If you find any issues with the problem (statement, scoring, time/memory limits, test cases, etc.), you may submit an issue here. A problem moderator will review your issue.

Guidelines:

  1. This is not a place to publish discussions, editorials, or requests to debug your code. Issues are only visible to you and problem moderators.
  2. Do not submit duplicated issues.
  3. Issues must be filed in English or Chinese only.
Active Issues 0
No issues in this category.
Closed/Resolved Issues 0
No issues in this category.