CF207C3.Game with Two Trees

普及/提高-

通过率:0%

AC君温馨提醒

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

题目描述

The Smart Beaver from ABBYY has come up with a new developing game for children. The Beaver thinks that this game will help children to understand programming better.

The main object of the game is finite rooted trees, each of their edges contains some lowercase English letter. Vertices on any tree are always numbered sequentially from 11 to mm , where mm is the number of vertices in the tree. Before describing the actual game, let's introduce some definitions.

We'll assume that the sequence of vertices with numbers v1v_{1} , v2v_{2} , ...... , vkv_{k} ( k>=1k>=1 ) is a forward path, if for any integer ii from 11 to k1k-1 vertex viv_{i} is a direct ancestor of vertex vi+1v_{i+1} . If we sequentially write out all letters from the the edges of the given path from v1v_{1} to vkv_{k} , we get some string ( k=1k=1 gives us an empty string). We'll say that such string corresponds to forward path v1v_{1} , v2v_{2} , ...... , vkv_{k} .

We'll assume that the sequence of tree vertices with numbers v1v_{1} , v2v_{2} , ...... , vkv_{k} ( k>=1k>=1 ) is a backward path if for any integer ii from 11 to k1k-1 vertex viv_{i} is the direct descendant of vertex vi+1v_{i+1} . If we sequentially write out all the letters from the edges of the given path from v1v_{1} to vkv_{k} , we get some string ( k=1k=1 gives us an empty string). We'll say that such string corresponds to backward path v1v_{1} , v2v_{2} , ...... , vkv_{k} .

Now let's describe the game that the Smart Beaver from ABBYY has come up with. The game uses two rooted trees, each of which initially consists of one vertex with number 11 . The player is given some sequence of operations. Each operation is characterized by three values ( tt , vv , cc ) where:

  • tt is the number of the tree on which the operation is executed ( 11 or 22 );
  • vv is the vertex index in this tree (it is guaranteed that the tree contains a vertex with this index);
  • cc is a lowercase English letter.

The actual operation is as follows: vertex vv of tree tt gets a new descendant with number m+1m+1 (where mm is the current number of vertices in tree tt ), and there should be letter cc put on the new edge from vertex vv to vertex m+1m+1 .

We'll say that an ordered group of three integers ( ii , jj , qq ) is a good combination if:

  • 1<=i<=m11<=i<=m_{1} , where m1m_{1} is the number of vertices in the first tree;
  • 1<=j,q<=m21<=j,q<=m_{2} , where m2m_{2} is the number of vertices in the second tree;
  • there exists a forward path v1v_{1} , v2v_{2} , ...... , vkv_{k} such that v1=jv_{1}=j and vk=qv_{k}=q in the second tree;
  • the string that corresponds to the forward path in the second tree from vertex jj to vertex qq equals the string that corresponds to the backward path in the first tree from vertex ii to vertex 11 (note that both paths are determined uniquely).

Your task is to calculate the number of existing good combinations after each operation on the trees.

输入格式

The first line contains integer nn — the number of operations on the trees. Next nn lines specify the operations in the order of their execution. Each line has form " tt vv cc ", where tt is the number of the tree, vv is the vertex index in this tree, and cc is a lowercase English letter.

To get the full points for the first group of tests it is sufficient to solve the problem with 1<=n<=7001<=n<=700 .

To get the full points for the second group of tests it is sufficient to solve the problem with 1<=n<=70001<=n<=7000 .

To get the full points for the third group of tests it is sufficient to solve the problem with 1<=n<=1000001<=n<=100000 .

输出格式

Print exactly nn lines, each containing one integer — the number of existing good combinations after the corresponding operation from the input.

Please, do not use the %lld specifier to read or write 64-bit integers in С++. It is preferred to use the cin, cout streams or the %I64d specifier.

输入输出样例

  • 输入#1

    5
    1 1 a
    2 1 a
    1 2 b
    2 1 b
    2 3 a
    

    输出#1

    1
    3
    3
    4
    7
    

说明/提示

After the first operation the only good combination was (1,1,1)(1,1,1) . After the second operation new good combinations appeared, (2,1,2)(2,1,2) and (1,2,2)(1,2,2) . The third operation didn't bring any good combinations. The fourth operation added good combination (1,3,3)(1,3,3) . Finally, the fifth operation resulted in as much as three new good combinations — (1,4,4)(1,4,4) , (2,3,4)(2,3,4) and (3,1,4)(3,1,4) .

首页