Number of junctions

3.5

12 votes
Data Structures, Heavy Light Decomposition, Advanced Algorithms, Algorithms, Graphs
Problem

Your town has N junctions numbered 1 to N. These junctions are connected by exactly N1 bidirectional roads in such a way that there is a unique simple path between each pair of junctions.

M oil tankers are transported through the town. Each of these tankers had oil leaking from them. It is known that the amount of oil that is leaking increases by one per road as the tanker moves ahead on its path. In other words, if K liters of oil spills on the road between the ith and (i+1)th junction on its path, then up to K+1 litres of oil can spill on the road between the (i+1)th and (i+2)th junction on its path.

For each j such that 1jM, you know Sj, Dj, and Lj that denote the source, destination, and the amount of oil in the jth tanker. Also, you know that exactly one liter of oil is spilled on the first road on the path of each of the tankers.

It is obvious that the amount of oil spilled from an oil tanker can't exceed the amount of oil it was carrying.

You are currently at junction 1. You select a junction X and travel from junction 1 to junction X. Now, you collect all the spilled oil on the roads that you travel along the way. If you choose X optimally, then what is the maximum amount of oil that you can collect?

Input format

  • The first line of input contains the integer N (2N2105).
  • The next N1 lines contain two space-separated integers u,v (1u,vN) denoting a bidirectional road between junction u and junction v.
  • The next line contains the integer M (1M2105).
  • The next M lines contain three space-separated integers Sj, Dj, and Lj (1Sj, DjN, 1Lj109) denoting the source, destination, and the amount of oil in the jth oil tanker.

Output format

In the only line of the output, print a single integer denoting the answer to the problem.

Time Limit: 3
Memory Limit: 256
Source Limit:
Explanation

The first tanker will follow the path 4213 and spill 1, 2 and 3 litres of oil on the roads in the path.
The second tanker will follow the path 52136 and spill 1, 2, 3 and 3 litres of oil on the roads in the path (because the tanker has only 9 litres of oil).
The optimal X for this example is 6. You can travel from 16 and collect 9 litres of oil.

Editor Image

?