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

#LCA0000067. Ada và cây cam (Ada and Orange Tree)

HỌC THEO LỘ TRÌNH

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

1 lộ trình

Ada và cây cam (Ada and Orange Tree)

Ada and Orange Tree

Source: SPOJ

Version: Phuoc Hung OJ Extended

Problem Statement

Ada has an orange tree of nn vertices numbered 00 through n−1n-1. Each vertex has a shade in 1..2501..250. The tree is rooted at rr. For each query (a,b)(a,b), follow the unique path from aa to bb and count the distinct shades occurring in the union of the rooted subtrees of all vertices along that path. This is not merely a distinct-color count on the path itself.

Input

First line: n q rn\ q\ r. Next line has nn shades s0,…,sn−1s_0,\ldots,s_{n-1}. Then n−1n-1 edges u vu\ v and qq queries a ba\ b.

Output

Print the number of distinct shades for each query.

Subtasks

General constraints: 1≤n,q≤4.5⋅1051\le n,q\le 4.5\cdot10^5; 0≤r,a,b,u,v<n0\le r,a,b,u,v<n; 1≤si≤2501\le s_i\le250; dữ liệu là cây.

  • Subtask 1 (20 points): 1≤n≤401\le n\le 40; 1≤q≤501\le q\le 50.

  • Subtask 2 (30 points): 1≤n≤10001\le n\le 1000; 1≤q≤10001\le q\le 1000.

  • Subtask 3 (50 points): 1≤n≤4500001\le n\le 450000; 1≤q≤4500001\le q\le 450000.

Examples

Input

10 7 1
1 2 1 4 5 6 6 8 9 9
0 9
9 3
3 4
4 6
4 5
4 8
1 3
1 2
2 7
4 4
8 6
0 6
7 0
7 2
0 0
2 3

Output

3
3
5
7
2
1
7

Explanation

For query (4,4)(4,4), the path is one vertex and the union is the subtree rooted at 44, with three distinct shades.