atcoder#ABC133F. [ABC133F] Colorful Tree
[ABC133F] Colorful Tree
Score : points
Problem Statement
There is a tree with vertices numbered to . The -th edge in this tree connects Vertex and Vertex , and the color and length of that edge are and , respectively. Here the color of each edge is represented by an integer between and (inclusive). The same integer corresponds to the same color, and different integers correspond to different colors.
Answer the following queries:
- Query (): assuming that the length of every edge whose color is is changed to , find the distance between Vertex and Vertex . (The changes of the lengths of edges do not affect the subsequent queries.)
Constraints
- The given graph is a tree.
- All values in input are integers.
Input
Input is given from Standard Input in the following format:
Output
Print lines. The -th line () should contain the answer to Query .
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
130
200
60
The graph in this input is as follows:
Here the edges of Color are shown as solid red lines, the edge of Color is shown as a bold green line, and the edge of Color is shown as a blue dashed line.
- Query : Assuming that the length of every edge whose color is is changed to , the distance between Vertex and Vertex is .
- Query : Assuming that the length of every edge whose color is is changed to , the distance between Vertex and Vertex is .
- Query : Assuming that the length of every edge whose color is is changed to (there is no such edge), the distance between Vertex and Vertex is . Note that the edges of Color now have their original lengths.