CF1312G.Autocompletion

普及/提高-

通过率:0%

AC君温馨提醒

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

题目描述

You are given a set of strings SS . Each string consists of lowercase Latin letters.

For each string in this set, you want to calculate the minimum number of seconds required to type this string. To type a string, you have to start with an empty string and transform it into the string you want to type using the following actions:

  • if the current string is tt , choose some lowercase Latin letter cc and append it to the back of tt , so the current string becomes t+ct + c . This action takes 11 second;
  • use autocompletion. When you try to autocomplete the current string tt , a list of all strings sSs \in S such that tt is a prefix of ss is shown to you. This list includes tt itself, if tt is a string from SS , and the strings are ordered lexicographically. You can transform tt into the ii -th string from this list in ii seconds. Note that you may choose any string from this list you want, it is not necessarily the string you are trying to type.

What is the minimum number of seconds that you have to spend to type each string from SS ?

Note that the strings from SS are given in an unusual way.

输入格式

The first line contains one integer nn ( 1n1061 \le n \le 10^6 ).

Then nn lines follow, the ii -th line contains one integer pip_i ( 0pi<i0 \le p_i < i ) and one lowercase Latin character cic_i . These lines form some set of strings such that SS is its subset as follows: there are n+1n + 1 strings, numbered from 00 to nn ; the 00 -th string is an empty string, and the ii -th string ( i1i \ge 1 ) is the result of appending the character cic_i to the string pip_i . It is guaranteed that all these strings are distinct.

The next line contains one integer kk ( 1kn1 \le k \le n ) — the number of strings in SS .

The last line contains kk integers a1a_1 , a2a_2 , ..., aka_k ( 1ain1 \le a_i \le n , all aia_i are pairwise distinct) denoting the indices of the strings generated by above-mentioned process that form the set SS — formally, if we denote the ii -th generated string as sis_i , then S=sa1,sa2,,sakS = {s_{a_1}, s_{a_2}, \dots, s_{a_k}} .

输出格式

Print kk integers, the ii -th of them should be equal to the minimum number of seconds required to type the string sais_{a_i} .

输入输出样例

  • 输入#1

    10
    0 i
    1 q
    2 g
    0 k
    1 e
    5 r
    4 m
    5 h
    3 p
    3 e
    5
    8 9 1 10 6

    输出#1

    2 4 1 3 3
  • 输入#2

    8
    0 a
    1 b
    2 a
    2 b
    4 a
    4 b
    5 c
    6 d
    5
    2 3 4 7 8

    输出#2

    1 2 2 4 4

说明/提示

In the first example, SS consists of the following strings: ieh, iqgp, i, iqge, ier.

首页