A12609 | Anagram Paths
时间限制1s
内存限制256MB
通过 / 提交0/0
题目描述
Toad Ilya has a rooted binary tree with vertex $1$ being the root. A tree is a connected graph without cycles. A tree is rooted if one vertex is selected and called the root. A vertex $u$ is a child of a vertex $v$ if $u$ and $v$ are connected by an edge and $v$ is closer to the root than $u$ . A leaf is a non-root vertex that has no children.
In the tree Ilya has each vertex has at most two children, and each edge has some character written on it. The character can be a lowercase English letter or the question mark '?'.
Ilya will $q$ times update the tree a bit. Each update will replace exactly one character on some edge. After each update Ilya needs to find if the tree is anagrammable and if yes, find its anagramnity for each letter. Well, that's difficult to explain, but we'll try.
To start with, a string $a$ is an anagram of a string $b$ if it is possible to rearrange letters in $a$ (without changing the letters itself) so that it becomes $b$ . For example, the string "fortyfive" is an anagram of the string "overfifty", but the string "aabb" is not an anagram of the string "bbba".
Consider a path from the root of the tree to a leaf. The characters on the edges on this path form a string, we say that this string is associated with this leaf. The tree is anagrammable if and only if it is possible to replace each question mark with a lowercase English letter so that for all pair of leaves the associated strings for these leaves are anagrams of each other.
If the tree is anagrammable, then its anagramnity for the letter $c$ is the maximum possible number of letters $c$ in a string associated with some leaf in a valid replacement of all question marks.
Please after each update find if the tree is anagrammable and if yes, find the $\sum{f(c) \cdot ind(c)}$ for all letters $c$ , where $f(c)$ is the anagramnity for the letter $c$ , and $ind(x)$ is the index of this letter in the alphabet ( $ind($ "a" $) = 1$ , $ind($ "b" $) = 2$ , ..., $ind($ "z" $) = 26$ ).
In the tree Ilya has each vertex has at most two children, and each edge has some character written on it. The character can be a lowercase English letter or the question mark '?'.
Ilya will $q$ times update the tree a bit. Each update will replace exactly one character on some edge. After each update Ilya needs to find if the tree is anagrammable and if yes, find its anagramnity for each letter. Well, that's difficult to explain, but we'll try.
To start with, a string $a$ is an anagram of a string $b$ if it is possible to rearrange letters in $a$ (without changing the letters itself) so that it becomes $b$ . For example, the string "fortyfive" is an anagram of the string "overfifty", but the string "aabb" is not an anagram of the string "bbba".
Consider a path from the root of the tree to a leaf. The characters on the edges on this path form a string, we say that this string is associated with this leaf. The tree is anagrammable if and only if it is possible to replace each question mark with a lowercase English letter so that for all pair of leaves the associated strings for these leaves are anagrams of each other.
If the tree is anagrammable, then its anagramnity for the letter $c$ is the maximum possible number of letters $c$ in a string associated with some leaf in a valid replacement of all question marks.
Please after each update find if the tree is anagrammable and if yes, find the $\sum{f(c) \cdot ind(c)}$ for all letters $c$ , where $f(c)$ is the anagramnity for the letter $c$ , and $ind(x)$ is the index of this letter in the alphabet ( $ind($ "a" $) = 1$ , $ind($ "b" $) = 2$ , ..., $ind($ "z" $) = 26$ ).
输入格式
The first line of input contains two integers $n$ and $q$ ( $2 \leq n \leq 150\,000$ , $1 \leq q \leq 150\,000$ ) — the number of vertices in the tree and the number of queries.
The next $n-1$ lines describe the initial tree. The $i$ -th of them contains an integer $p_i$ and a character $c_i$ ( $1 \leq p_i \leq i$ , $c_i$ is a lowercase English letter or the question mark '?') describing an edge between vertices $p_i$ and $i+1$ with character $c_i$ written on it.
The root of this tree is the vertex $1$ , and each vertex has at most two children.
The next $q$ lines describe the queries. The $i$ -th of them contains two integers $v$ and $c$ ( $2 \leq v \leq n$ , $c$ is a lowercase English letter or the question mark '?'), meaning that updated character on the edge between $p_{v-1}$ to $v$ is $c$ . The updated character can be the same as was written before.
The next $n-1$ lines describe the initial tree. The $i$ -th of them contains an integer $p_i$ and a character $c_i$ ( $1 \leq p_i \leq i$ , $c_i$ is a lowercase English letter or the question mark '?') describing an edge between vertices $p_i$ and $i+1$ with character $c_i$ written on it.
The root of this tree is the vertex $1$ , and each vertex has at most two children.
The next $q$ lines describe the queries. The $i$ -th of them contains two integers $v$ and $c$ ( $2 \leq v \leq n$ , $c$ is a lowercase English letter or the question mark '?'), meaning that updated character on the edge between $p_{v-1}$ to $v$ is $c$ . The updated character can be the same as was written before.
输出格式
Output $q$ lines. In the $i$ -th of them print "Fou" if the tree is not anagrammable after the first $i$ updates.
Otherwise output "Shi" and the $\sum{f(c) \cdot ind(c)}$ for all letters $c$ .
Otherwise output "Shi" and the $\sum{f(c) \cdot ind(c)}$ for all letters $c$ .
输入输出样例
输入 #1
3 4 1 ? 1 ? 2 ? 2 a 3 b 2 b
输出 #1
Shi 351 Shi 1 Fou Shi 2
输入 #2
5 2 1 ? 1 ? 2 ? 3 ? 4 a 5 b
输出 #2
Shi 352 Shi 3
In the first example after the first query, for each character, you can set all edges equal to that character, and you will get $1$ such character on each path, so the answer is $1 \cdot (1+2+\ldots+26) = 351$ .
In the first example after the second query, you know that all paths should be an anagram of "a", so all paths should be "a", so the answer is $1 \cdot 1 = 1$ .
In the first example after the third query, you have two paths with strings "a" and "b", but these strings are not anagrams, so the answer is "Fou".
In the first example after the fourth query, you know that all paths should be "b", so the answer is $1 \cdot 2 = 2$ .
In the second example after the first query, you know that $f($ 'a' $) = 2$ and $f(c) = 1$ for all other characters, so the answer is $1 \cdot (2 + 3 + \ldots + 26) + 2 = 352$ .
In the second example after the second query, you know that each path should contain one 'a' and one 'b', so the answer is $1 \cdot 1 + 1 \cdot 2 = 3$ .
In the first example after the second query, you know that all paths should be an anagram of "a", so all paths should be "a", so the answer is $1 \cdot 1 = 1$ .
In the first example after the third query, you have two paths with strings "a" and "b", but these strings are not anagrams, so the answer is "Fou".
In the first example after the fourth query, you know that all paths should be "b", so the answer is $1 \cdot 2 = 2$ .
In the second example after the first query, you know that $f($ 'a' $) = 2$ and $f(c) = 1$ for all other characters, so the answer is $1 \cdot (2 + 3 + \ldots + 26) + 2 = 352$ .
In the second example after the second query, you know that each path should contain one 'a' and one 'b', so the answer is $1 \cdot 1 + 1 \cdot 2 = 3$ .
C++ 编辑器
输入
输出
可保存默认模板;新题优先使用已保存模板。
当前快捷键仅展示,暂不支持修改。
- 撤销
Ctrl / ⌘ + Z - 重做
Ctrl / ⌘ + Y - 查找
Ctrl / ⌘ + F - 全选
Ctrl / ⌘ + A - 复制
Ctrl / ⌘ + C - 剪切
Ctrl / ⌘ + X - 粘贴
Ctrl / ⌘ + V - 自动排版
工具栏排版按钮 - 草稿保存
编辑时自动保存到本机
历史
提交记录
状态说明时间源码
AI
作答助手
你好,我是作答助手。可以问思路、复杂度、样例含义或代码报错原因;不会直接给出完整 AC 代码。
确定要清空代码吗?
提交通过
评测结果:Accepted