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
1 lộ trìnhBài tập thuộc lộ trình
Mục lục đề ⌄
Đang đọc
Đế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 vertices numbered from to . Vertex carries an integer weight . For each query , consider the unique simple path from to , including both endpoints, and output the number of distinct vertex weights on that path.
Input
- Line 1: integers .
- Line 2: integers .
- The next lines each contain an undirected edge ; the edges form a tree.
- The final lines each contain endpoints of a query.
Output
Print lines, one answer per path query.
Subtasks
- Subtask 1 (10 points): , .
- Subtask 2 (20 points): , .
- Subtask 3 (30 points): , .
- Subtask 4 (40 points): , ; trọng số là số nguyên có dấu 32-bit.
General limits: , ; signed 32-bit vertex weights. Vertices are numbered from 1 to .
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.