logo AlgoBeat OnlineJudge
登录 注册

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

内存限制:1024 MiB 时间限制:4000 ms 标准输入输出
题目类型:VJudge(洛谷) 评测方式:VJudge
上传者: 匿名

题目描述

You are given a tree with vertices, numbered through . Edge connects vertices and . Each vertex is assigned a color .

You are asked to process queries. In each query, four integers are given.

For each query, determine the maximum integer () such that the following condition holds:

  • For every , the number of vertices of color on the path from to is equal to the number of vertices of color on the path from to .

输入格式

The input is given in the following format:

Each Query is given in the following format:

  • ()
  • ()
  • ()
  • The given graph is a tree.
  • All input values are integers.

输出格式

Output lines. On the -th line (), output the answer for the -th query.

样例

样例输入 1

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

样例输出 1

0
6
6
0