#P10391. [蓝桥杯 2024 省 A] 零食采购

    ID: 11918 远端评测题 1000ms 512MiB 尝试: 0 已通过: 0 难度: 6 上传者: 标签>2024最近公共祖先 LCA蓝桥杯省赛状压 DP

[蓝桥杯 2024 省 A] 零食采购

Problem Description

Xiaolan is preparing to travel in space. Before leaving, he wants to buy some snacks in this galaxy. There are nn planets in the galaxy, connected by n1n - 1 space routes, forming a connected graph. The ii-th planet sells the cic_i-th type of local snack.

Xiaolan comes up with qq purchasing plans. In the ii-th plan, the starting planet is sis_i and the destination planet is tit_i. For each plan, Xiaolan will travel from the start to the destination along the shortest routes, and he can buy all snacks sold on the planets he passes through (including the start and destination). Please compute, for each plan, the maximum number of different snack types he can buy.

Input Format

The first line contains two positive integers nn and qq, separated by a single space.
The second line contains nn integers c1,c2,,cnc_1,c_2,\cdots, c_n, with a single space between adjacent integers.
The next n1n - 1 lines each contain two integers ui,viu_i,v_i, separated by a single space, meaning there is a route connecting planet uiu_i and planet viv_i.
The next qq lines each contain two integers si,tis_i, t_i, separated by a single space, representing one purchasing plan.

Output Format

Output qq lines. Each line contains one integer, in order, representing the answer for each purchasing plan.

4 2
1 2 3 1
1 2
1 3
2 4
4 3
1 4
3
2

Hint

For the first plan, the route is {4,2,1,3}\{4, 2, 1, 3\}, and Xiaolan can buy snack types 1,2,31, 2, 3.
For the second plan, the route is {1,2,4}\{1, 2, 4\}, and Xiaolan can buy snack types 1,21, 2.

For 20%20\% of the testdata, 1n,q50001 \le n, q \le 5000.
For all testdata, 1n,q1051 \le n, q \le 10^5, 1ci201 \le c_i \le 20, 1ui,vin1 \le u_i, v_i \le n, 1si,tin1 \le s_i, t_i \le n

Translated by ChatGPT 5