PHƯỚC HƯNG OJ · THỬ THÁCH THUẬT TOÁN

#SRD0000089. Đếm giá trị trên đường đi II (Count on a tree II)

HỌC THEO LỘ TRÌNH

Bài tập thuộc lộ trình

1 lộ trình

Đếm giá trị trên đường đi II (Count on a tree II)

Count on a tree II

Source: SPOJ

Version: Phuoc Hung OJ Extended

Problem Statement

You are given a tree with nn vertices numbered from 11 to nn. Vertex uu carries an integer weight aua_u. For each query (u,v)(u,v), consider the unique simple path from uu to vv, including both endpoints, and output the number of distinct vertex weights on that path.

Input

  • Line 1: integers n,mn,m.
  • Line 2: nn integers a1,…,ana_1,\ldots,a_n.
  • The next n−1n-1 lines each contain an undirected edge u,vu,v; the edges form a tree.
  • The final mm lines each contain endpoints u,vu,v of a query.

Output

Print mm lines, one answer per path query.

Subtasks

  • Subtask 1 (10 points): 1≤n≤351\le n\le 35, 1≤m≤451\le m\le 45.
  • Subtask 2 (20 points): 1≤n≤10001\le n\le 1000, 1≤m≤10001\le m\le 1000.
  • Subtask 3 (30 points): 1≤n≤80001\le n\le 8000, 1≤m≤150001\le m\le 15000.
  • Subtask 4 (40 points): 1≤n≤400001\le n\le 40000, 1≤m≤1000001\le m\le 100000; trọng số là số nguyên có dấu 32-bit.

General limits: 1≤n≤400001\le n\le 40000, 1≤m≤1000001\le m\le 100000; signed 32-bit vertex weights. Vertices are numbered from 1 to nn.

Examples

Input

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

Output

4
4

Explanation

The path from 2 to 5 contains weights 2, 105, 9, 8, all distinct, so the answer is 4. The path from 7 to 8 also has four distinct weights, despite two vertices elsewhere having weight 7.