Ants
Time limit5sMemory limit256 MB
On a weighted tree, for each ant, find the largest number of other ants it meets at one point in time, either inside a tunnel or at an anthill.
- Level
Hard9 of 10
- Topics
- Tree, Segment tree, Sorting, DFS
- Solved
- No attempts yet
Problem
In the Hundred Byte Wood, the ants have built anthills numbered through . The anthills are connected by two-way tunnels so that there is exactly one simple path between each pair of anthills.
Spring has come, and the queen ant announced an annual rotation of the anthill structure. The rotation involves worker ants. The -th ant must leave its anthill at time and travel to anthill along the shortest path. All ants move at the same speed, and none of them stops before reaching its destination.
The queen suspects that too many ants meeting at one point could start a secession. For each worker ant, find the largest number of other travelling ants that this worker meets at a single point, either inside a tunnel or at an anthill. Meetings happen only during travel. If the -th ant arrives at its destination at time , then ants and can meet only at a time in .
Input
The first line contains two integers and (), the number of anthills and the number of worker ants.
Each of the next lines contains three integers , , (, , ). This means anthills and are connected by a two-way tunnel, and a worker needs time units to pass through it.
Each of the last lines contains three integers , , (, , ) describing a worker ant.
Output
Print lines. The -th line contains the largest number of travelling ants (other than ant ) that ant meets at the same time.