#P10006. [集训队互测 2023] 超现实树

    ID: 11110 远端评测题 1000ms 512MiB 尝试: 0 已通过: 0 难度: 9 上传者: 标签>点分治集训队互测2023O2优化快速数论变换 NTT根号分治

[集训队互测 2023] 超现实树

Background

Alek likes competitive programming, and especially likes surreal trees. A surreal tree, as the name suggests, is a tree with surreal numbers on it.

Problem Description

Alek thinks that, for a constant kk, a string is called a “kk-surreal number string” if it contains only the characters {,|,}\texttt{\{}, \texttt{|}, \texttt{\}}, and:

  • The empty string is a kk-surreal number string.
  • If s,ts, t are kk-surreal number strings, then s+ts + t is a kk-surreal number string.
  • If k+1k + 1 strings s1,s2,,sk+1s_1, s_2, \cdots, s_{k + 1} are all kk-surreal number strings, then $\texttt{\{} + s_1 + \texttt{|} + s_2 + \texttt{|} + \cdots + \texttt{|} + s_{k + 1} + \texttt{\}}$ is a kk-surreal number string.
  • kk-surreal number strings are only those described above.

You are given an unrooted tree with nn nodes, numbered 1n1 \sim n. Each node ii has a character ai{{,|,}}a_i \in \{\texttt{\{}, \texttt{|}, \texttt{\}}\}.

Given an integer mm, for each k=0,1,,mk = 0, 1, \cdots, m, Alek wants you to compute: how many ordered pairs (x,y)(x, y) with 1x,yn1 \leq x, y \leq n are such that the string obtained by concatenating, in order, the characters along the unique simple path from node xx to node yy is a kk-surreal number string.

Input Format

The first line contains two integers n,mn, m, representing the number of nodes in the tree, and the upper limit of kk for which you need to compute answers.

The second line contains a string aa, where the ii-th character of aa is the character on node ii.

The next n1n - 1 lines each contain two integers x,yx, y, indicating that there is an edge connecting node xx and node yy.

Output Format

Output one line with m+1m + 1 integers, representing the answers for k=0,1,,mk = 0, 1, \cdots, m, respectively.

5 3
|{}}}
2 1
3 2
4 1
5 1
1 2 0 0
10 8
|}||}{|{{{
2 1
3 1
4 3
5 2
6 5
7 5
8 4
9 2
10 3
2 0 1 1 0 0 0 0 0
见附加文件 ex_surreal3.in。
见附加文件 ex_surreal3.ans。

Hint

For all testdata, 2n1052 \leq n \leq 10^5, 0mn20 \leq m \leq n - 2, ai{{,|,}}a_i \in \{\texttt{\{}, \texttt{|}, \texttt{\}}\}.

  • Subtask 1 (5 points): n4601n \leq 4601.
  • Subtask 2 (20 points): For every edge (x,y)(x, y), y=x+1y = x + 1.
  • Subtask 3 (5 points): ai|a_i \neq \texttt{|}, m=0m = 0.
  • Subtask 4 (15 points, depends on Subtask 3): m3m \leq 3.
  • Subtask 5 (25 points, depends on Subtask 1): n5×104n \leq 5 \times 10^4.
  • Subtask 6 (30 points, depends on Subtask 1, 2, 3, 4, 5): No special constraints.

Translated by ChatGPT 5