100 atcoder#ABC070D. [ABC070D] Transit Tree Path
[ABC070D] Transit Tree Path
Score : points
Problem Statement
You are given a tree with vertices. Here, a tree is a kind of graph, and more specifically, a connected undirected graph with edges, where is the number of its vertices. The -th edge connects Vertices and , and has a length of .
You are also given queries and an integer . In the -th query :
- find the length of the shortest path from Vertex and Vertex via Vertex .
Constraints
- The given graph is a tree.
Input
Input is given from Standard Input in the following format:
Output
Print the responses to the queries in lines. In the -th line , print the response to the -th query.
5
1 2 1
1 3 1
2 4 1
3 5 1
3 1
2 4
2 3
4 5
3
2
4
The shortest paths for the three queries are as follows:
- Query : Vertex → Vertex → Vertex → Vertex : Length
- Query : Vertex → Vertex → Vertex : Length
- Query : Vertex → Vertex → Vertex → Vertex → Vertex : Length
7
1 2 1
1 3 3
1 4 5
1 5 7
1 6 9
1 7 11
3 2
1 3
4 5
6 7
5
14
22
The path for each query must pass Vertex .
10
1 2 1000000000
2 3 1000000000
3 4 1000000000
4 5 1000000000
5 6 1000000000
6 7 1000000000
7 8 1000000000
8 9 1000000000
9 10 1000000000
1 1
9 10
17000000000