#P8616. [蓝桥杯 2014 国 C] 套娃

[蓝桥杯 2014 国 C] 套娃

Description

As a birthday gift from drd, atm recently got a Russian matryoshka doll. He is very interested in how it is constructed.

A matryoshka doll is nested layer by layer. Assume that: inside a matryoshka doll of size xx, it is possible to put any number of matryoshka dolls with sizes smaller than xx (while matryoshka dolls sold in the market usually allow only one smaller doll inside a bigger one).

drd tells atm that this matryoshka doll is made up of nn small dolls, and their sizes are all different. We label these small dolls by size from small to large as 11 to nn.

If atm wants to view the small doll of size kk, he will first check whether this doll is already on the table. If it is already on the table, then he can view it. Otherwise, he will open some matryoshka doll on the table, take out all small dolls directly inside it, and place them on the table.

At the beginning, there is only the doll of size nn (given by drd) on the table. Note that each time he opens a doll, he only takes out all small dolls directly inside it. If a small doll taken out still contains other dolls inside, he will not take out those deeper dolls.

Also, atm has an obsessive need for optimality. He will minimize the number of dolls he needs to open.

atm is a strange person. Sometimes he only wants to know how many dolls need to be opened to view the doll of size xx (but does not actually open them). Sometimes, after hearing from drd that a doll is especially beautiful, he will open it to view it. Now please output how many dolls he needs to open each time.

Input Format

The first line contains two integers n,mn,m, representing the number of dolls and the number of operations atm wants to perform.

The next n1n-1 lines each contain two integers u,vu,v, meaning that inside the doll of size uu there is a doll of size vv. It is guaranteed that u>vu>v.

The next mm lines each are in one of the following forms:

  • P x: atm must view the doll of size xx (this will open xx, and opening xx is not counted in the total answer);

  • Q x: atm only wants to know how many dolls he would need to open in order to view the doll of size xx, but he does not actually open them.

Output Format

Output mm lines. For each P operation or Q operation in the input, output the number of matryoshka dolls that need to be opened (or hypothetically opened).

5 5
5 3
5 4
3 2
3 1
Q 1
Q 4
P 2
Q 1
Q 4
2
1
2
0
0

Hint

For 30%30\% of the testdata: n,m1000n,m \le 1000.

For 100%100\% of the testdata: n,m100000n,m \le 100000.

Translated by ChatGPT 5