CF1168D.Anagram Paths

普及/提高-

通过率:0%

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

Toad Ilya has a rooted binary tree with vertex 11 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 uu is a child of a vertex vv if uu and vv are connected by an edge and vv is closer to the root than uu . 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 qq 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 aa is an anagram of a string bb if it is possible to rearrange letters in aa (without changing the letters itself) so that it becomes bb . 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 cc is the maximum possible number of letters cc 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 f(c)ind(c)\sum{f(c) \cdot ind(c)} for all letters cc , where f(c)f(c) is the anagramnity for the letter cc , and ind(x)ind(x) is the index of this letter in the alphabet ( ind(ind( "a" )=1) = 1 , ind(ind( "b" )=2) = 2 , ..., ind(ind( "z" )=26) = 26 ).

输入格式

The first line of input contains two integers nn and qq ( 2n1500002 \leq n \leq 150\,000 , 1q1500001 \leq q \leq 150\,000 ) — the number of vertices in the tree and the number of queries.

The next n1n-1 lines describe the initial tree. The ii -th of them contains an integer pip_i and a character cic_i ( 1pii1 \leq p_i \leq i , cic_i is a lowercase English letter or the question mark '?') describing an edge between vertices pip_i and i+1i+1 with character cic_i written on it.

The root of this tree is the vertex 11 , and each vertex has at most two children.

The next qq lines describe the queries. The ii -th of them contains two integers vv and cc ( 2vn2 \leq v \leq n , cc is a lowercase English letter or the question mark '?'), meaning that updated character on the edge between pv1p_{v-1} to vv is cc . The updated character can be the same as was written before.

输出格式

Output qq lines. In the ii -th of them print "Fou" if the tree is not anagrammable after the first ii updates.

Otherwise output "Shi" and the f(c)ind(c)\sum{f(c) \cdot ind(c)} for all letters cc .

输入输出样例

  • 输入#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 11 such character on each path, so the answer is 1(1+2++26)=3511 \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 11=11 \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 12=21 \cdot 2 = 2 .

In the second example after the first query, you know that f(f( 'a' )=2) = 2 and f(c)=1f(c) = 1 for all other characters, so the answer is 1(2+3++26)+2=3521 \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 11+12=31 \cdot 1 + 1 \cdot 2 = 3 .

首页