#P7518. [省选联考 2021 A/B 卷] 宝石

[省选联考 2021 A/B 卷] 宝石

Description

On the continent of Oai, there are nn cities, numbered from 11 to nn. All cities are connected by n1n - 1 undirected roads, meaning the nn cities and n1n - 1 roads form a tree.

In the market of each city, gems are sold. There are mm different types of gems in total, numbered from 11 to mm. The market in city ii sells gems of type wiw_i. One type of gem may be sold in the markets of multiple cities.

God K has a gem collector. This collector can collect at most cc gems in order, and the required order is: P1,P2,,PcP_1, P_2, \ldots, P_c. More specifically, the collector must first put in a gem of type P1P_1, then it can put in a gem of type P2P_2, then type P3P_3, and so on. Here P1,P2,,PcP_1, P_2, \ldots, P_c are all distinct.

After God K arrives at a city, if the type of gem sold in that city's market is the same as the type that the collector currently needs to put in, then he can buy one gem in that market and put it into the collector. Otherwise, he will just pass through the city and do nothing.

Now God K gives you qq queries. Each query gives a start city sis_i and an end city tit_i. He wants to know: if he starts from city sis_i and walks along the shortest path to city tit_i, what is the maximum number of gems he can collect in his collector? (In each query, the collector initially contains no gems. The gems sold in the markets of the start and end cities can also be tried to be collected.)

Input Format

The first line contains three positive integers n,m,cn, m, c, representing the number of cities, the number of gem types, and the capacity of the collector.
The second line contains cc positive integers PiP_i. The data guarantees 1Pim1 \le P_i \le m and all these numbers are distinct.
The third line contains nn positive integers wiw_i, where wiw_i indicates the type of gem sold in the market of city ii.
The next n1n - 1 lines each contain two positive integers ui,viu_i, v_i, representing a road connecting cities uiu_i and viv_i.
The next line contains one positive integer qq, representing the number of queries.
The next qq lines each contain two positive integers si,tis_i, t_i, representing the start city and the end city of the query.

Output Format

Output qq lines in the input order, each containing one integer, representing the answer to the query.

7 3 3
2 3 1
2 1 3 3 2 1 3
1 2
2 3
1 4
4 5
4 6
6 7
5
3 5
1 3
7 3
5 7
7 5

2
2
2
3
1

见附件中的 gem/gem2.in
见附件中的 gem/gem2.ans
见附件中的 gem/gem3.in
见附件中的 gem/gem3.ans

Hint

Constraints

For all testdata: 1n,q2×1051 \le n, q \le 2 \times {10}^5, 1cm5×1041 \le c \le m \le 5 \times {10}^4, 1wim1 \le w_i \le m.

The specific limits for each test point are shown in the table below:

Test Point ID n,qn, q \le Special Constraint
121 \sim 2 1010 None
353 \sim 5 10001000
6106 \sim 10 2×1052 \times {10}^5 m300m \le 300
111411 \sim 14 ui=iu_i = i, vi=i+1v_i = i + 1
152015 \sim 20 None

Translated by ChatGPT 5