PHƯỚC HƯNG OJ · THỬ THÁCH THUẬT TOÁN
#LCA0000097. Cây nhiều màu (Colorful Tree)
HỌC THEO LỘ TRÌNH
1 lộ trìnhBài tập thuộc lộ trình
Mục lục đề ⌄
Đang đọc
Cây nhiều màu (Colorful Tree)
Colorful Tree
Source: AtCoder
Version: Phuoc Hung OJ Extended
Problem Statement
You are given an undirected tree with vertices numbered from to . Each edge has a color and a length . Colors are integers from to .
For each of independent queries , imagine replacing the length of every edge of color with . Output the resulting distance along the unique path between and . The original edge lengths are restored before the next query.
Input
- The first line contains .
- Each of the next lines contains : endpoints, color and length of an edge.
- Each of the next lines contains .
Output
Print one answer per query.
Subtasks
- Subtask 1 (20 points): , ; , , ; ; . Các cạnh tạo thành một cây. Additionally, and the number of queries/plans is at most .
- Subtask 2 (30 points): , ; , , ; ; . Các cạnh tạo thành một cây. Additionally, and the number of queries/plans is at most .
- Subtask 3 (50 points): , ; , , ; ; . Các cạnh tạo thành một cây.
Examples
Input
5 3
1 2 1 10
1 3 2 20
2 4 4 30
5 2 1 40
1 100 1 4
1 100 1 5
3 1000 3 4
Output
130
200
60
Explanation
For the first query, the edge of color contributes and the edge of color contributes , totaling . The second path consists of two edges of color , giving . For the third query, the queried color does not occur on the path, so its length stays .