QOJ.ac

QOJ

Límite de tiempo: 2 s Límite de memoria: 512 MB Puntuación total: 100

#18812. Tree and Query 9

Estadísticas

There is a tree (an undirected connected graph with no cycles) consisting of $N$ vertices. The vertices are numbered from $1$ to $N$, and the edges are numbered from $1$ to $N-1$. Each vertex has a weight.

Write a program that processes the following query:

  • u v: Print the number of distinct weights of vertices on the path from $u$ to $v$.

Input

The first line contains $N$ ($2 \le N \le 100{,}000$).

The second line contains the weights of the vertices in order from vertex $1$ to vertex $N$.

The next $N-1$ lines each contain two integers $u$ and $v$ that an edge connects.

The next line contains $M$ ($1 \le M \le 100{,}000$), the number of queries.

The next $M$ lines each contain one query.

The weight of each vertex is a natural number not exceeding $1{,}000{,}000$.

Output

For each query, output the result in order, one per line.

Examples

Input 1

8
105 2 9 3 8 5 7 7
1 2
1 3
1 4
3 5
3 6
3 7
4 8
2
2 5
7 8

Output 1

4
4

Discussions

About Discussions

The discussion section is only for posting: General Discussions (problem-solving strategies, alternative approaches), and Off-topic conversations.

This is NOT for reporting issues! If you want to report bugs or errors, please use the Issues section below.

Open Discussions 0
No discussions in this category.

Issues

About Issues

If you find any issues with the problem (statement, scoring, time/memory limits, test cases, etc.), you may submit an issue here. A problem moderator will review your issue.

Guidelines:

  1. This is not a place to publish discussions, editorials, or requests to debug your code. Issues are only visible to you and problem moderators.
  2. Do not submit duplicated issues.
  3. Issues must be filed in English or Chinese only.
Active Issues 0
No issues in this category.
Closed/Resolved Issues 0
No issues in this category.