Discount Event
Time limit4sMemory limit1024 MB
Given a weighted tree, for each query (X, Y) set every edge on the X to Y path to cost 0 and output the largest distance between any two cities.
- Level
Hard8 of 10
- Topics
- Tree, Dynamic programming, DFS
- Solved
- No attempts yet
Problem
The country consists of cities and roads. The cities are numbered from to , and the roads are numbered from to . Road connects cities and in both directions, and traveling along it costs . For any two cities, you can travel between them using one or more roads.
The distance between two cities is the minimum cost of getting from one city to the other by using one or more roads.
You are the president of a road company, and you plan a discount event for the holidays. There are plans. In plan , every road on the shortest path from city to city is discounted so that its cost becomes 0. For each plan, output the maximum distance between two cities.
Input
The first line contains the number of cities . Each of the next lines contains three integers , , and , separated by spaces. These are the two cities connected by road and the travel cost of that road.
The next line contains the number of plans . Each of the next lines contains two integers and , separated by a space, which describe plan .
Output
Print lines. The -th line contains the answer for plan .
Constraints
- ()
- ()
- ()
- The given country forms a valid tree.
- ()
- ()