子树中的众数AI (CI翻译)
Problem Statement
题目描述
AI (CI翻译)You are given a rooted tree with $N$ vertices numbered $1$ through $N$. Vertex $1$ is the root, and the parent of vertex $i$ is vertex $p_i$ ($p_i \lt i$).
Each vertex is colored: vertex $i$ is colored with color $c_i$ ($1 \leq c_i \leq N$).
For each $v = 1, 2, \dots, N$, solve the following problem:
给定一棵有 $N$ 个顶点的有根树,顶点编号为 $1$ 至 $N$。顶点 $1$ 为根,顶点 $i$ 的父亲是顶点 $p_i$($p_i \lt i$)。
每个顶点都有一种颜色:顶点 $i$ 的颜色为 $c_i$($1 \leq c_i \leq N$)。
对于每个 $v = 1, 2, \dots, N$,请解决以下问题:AI (CI翻译)
Let
$f_i$be the number of vertices colored with color$i$among the vertices in the subtree rooted at vertex$v$.Find:
- the maximum value
$m$among the values in the sequence$(f_1, f_2, \dots, f_N)$, and- the number
$k$of positive integers$i$not greater than$N$such that$f_i = m$.
AI (CI翻译)设
$f_i$表示以顶点$v$为根的子树中,颜色为$i$的顶点数。求:
- 序列
$(f_1, f_2, \dots, f_N)$中的最大值$m$;- 不超过
$N$的正整数$i$中满足$f_i = m$的$i$的个数$k$。
Input/Output Format
输入/输出格式
AI (CI翻译)The input and output for this problem follow a special format.
本题的输入和输出采用特殊格式。AI (CI翻译)
Input Format
输入格式
AI (CI翻译)From Standard Input, you are given the integer $N$ along with integers $\mathrm{seed}, M, F$ and $q_2, q_3, \dots, q_M$, $d_1, d_2, \dots, d_M$. Reconstruct $p_2, p_3, \dots, p_N$ and $c_1, c_2, \dots, c_N$ using the computation described by the following pseudocode. (Here, 2^31 means $2^{31}=2147483648$. Note that $\mathrm{state}$ is a variable, and that a $64$-bit integer type is required for computing $\mathrm{state}$.)
从标准输入中给出整数 $N$,以及整数 $\mathrm{seed}, M, F$ 和 $q_2, q_3, \dots, q_M$、$d_1, d_2, \dots, d_M$。请按照下述伪代码中的计算方式还原出 $p_2, p_3, \dots, p_N$ 和 $c_1, c_2, \dots, c_N$。(这里,2^31 表示 $2^{31}=2147483648$。注意,state 是一个变量,计算 state 时需要使用 $64$ 位整数类型。)AI (CI翻译)
state = seed
for i=2 to N:
if i <= M:
p[i] = q[i]
else:
p[i] = (state mod (i-1)) + 1
state = (state * 1103515245 + 12345) mod 2^31
for i=1 to N:
if i <= M:
c[i] = d[i]
else:
c[i] = (state mod F) + 1
state = (state * 1103515245 + 12345) mod 2^31
state = seed
for i=2 to N:
if i <= M:
p[i] = q[i]
else:
p[i] = (state mod (i-1)) + 1
state = (state * 1103515245 + 12345) mod 2^31
for i=1 to N:
if i <= M:
c[i] = d[i]
else:
c[i] = (state mod F) + 1
state = (state * 1103515245 + 12345) mod 2^31AI (CI翻译)
Output Format
输出格式
AI (CI翻译)Let $m_i$ and $k_i$ denote $m$ and $k$ when $v=i$, respectively. Output this value:
设当 $v=i$ 时得到的 $m$ 和 $k$ 分别为 $m_i$ 和 $k_i$。输出以下值:AI (CI翻译)
$\left(\displaystyle \sum_{i=1}^N (m_i \oplus i)\times (k_i \oplus i)\right) \bmod 998244353$
$\left(\displaystyle \sum_{i=1}^N (m_i \oplus i)\times (k_i \oplus i)\right) \bmod 998244353$AI (CI翻译)
Here, $\oplus$ denotes bitwise XOR. Beware of overflow during computation.
其中,$\oplus$ 表示按位 XOR。计算过程中请注意防止溢出。AI (CI翻译)
Constraints
约束条件
AI (CI翻译)$2 \leq N \leq 2.5 \times 10^6$$1 \leq p_i \lt i$$1 \leq c_i \leq N$$1 \leq \mathrm{seed} \lt 2^{31}$$2 \leq M \leq \min(N, 10^5)$$1 \leq F \leq N$$1 \leq q_i \lt i$$1 \leq d_i \leq N$- All input values are integers.
$2 \leq N \leq 2.5 \times 10^6$$1 \leq p_i \lt i$$1 \leq c_i \leq N$$1 \leq \mathrm{seed} \lt 2^{31}$$2 \leq M \leq \min(N, 10^5)$$1 \leq F \leq N$$1 \leq q_i \lt i$$1 \leq d_i \leq N$- 所有输入值均为整数。
Input
输入
AI (CI翻译)The input is given from Standard Input in the following format:
输入从标准输入按以下格式给出:AI (CI翻译)
N \mathrm{seed} M F
q_2 q_3 \dots q_M
d_1 d_2 \dots d_M
N \mathrm{seed} M F
q_2 q_3 \dots q_M
d_1 d_2 \dots d_MAI (CI翻译)
Output
输出
AI (CI翻译)Output $\left(\displaystyle \sum_{i=1}^N (m_i \oplus i)\times (k_i \oplus i)\right) \bmod 998244353$.
输出 $\left(\displaystyle \sum_{i=1}^N (m_i \oplus i)\times (k_i \oplus i)\right) \bmod 998244353$。AI (CI翻译)
样例
4 454 4 2
1 2 2
1 2 2 3
29
6 123 2 2
1
1 2
101
15 1 4 5
1 2 3
5 3 1 3
1199
题面与样例版权归 AtCoder 及出题人所有;中文翻译由 AI 生成(gpt-6.1-sol),如有出入请以原文为准。