#P15761. [JAG 2025 Summer Camp #1] Colored Tree and Path

[JAG 2025 Summer Camp #1] Colored Tree and Path

题目描述

You are given a tree with NN vertices, numbered 11 through NN. Edge ii connects vertices aia_i and bib_i. Each vertex ii is assigned a color cic_i.

You are asked to process QQ queries. In each query, four integers u1,v1,u2,v2u_1, v_1, u_2, v_2 are given.

For each query, determine the maximum integer KK (0KN0 \leq K \leq N) such that the following condition holds:

  • For every j=1,2,,Kj = 1, 2, \ldots, K, the number of vertices of color jj on the path from u1u_1 to v1v_1 is equal to the number of vertices of color jj on the path from u2u_2 to v2v_2.

输入格式

The input is given in the following format:

$$\begin{aligned} & N \\ & a_1 \ b_1 \\ & a_2 \ b_2 \\ & \vdots \\ & a_{N-1} \ b_{N-1} \\ & c_1 \ c_2 \ \ldots \ c_N \\ & Q \\ & \text{Query}_1 \\ & \text{Query}_2 \\ & \vdots \\ & \text{Query}_Q \end{aligned}$$

Each Query is given in the following format:

u1 v1 u2 v2u_1 \ v_1 \ u_2 \ v_2
  • 1N100 0001 \leq N \leq 100\ 000
  • 1ai,biN1 \leq a_i, b_i \leq N (1iN11 \leq i \leq N - 1)
  • 1ciN1 \leq c_i \leq N (1iN1 \leq i \leq N)
  • 1Q100 0001 \leq Q \leq 100\ 000
  • 1u1,v1,u2,v2N1 \leq u_1, v_1, u_2, v_2 \leq N (1iQ1 \leq i \leq Q)
  • The given graph is a tree.
  • All input values are integers.

输出格式

Output QQ lines. On the ii-th line (1iQ1 \leq i \leq Q), output the answer for the ii-th query.

6
2 3
4 3
6 2
3 5
2 1
1 2 2 3 1 1
4
1 6 5 4
6 5 1 5
1 1 6 6
1 5 4 2
0
6
6
0