Metro
Time limit2.5sMemory limit1024 MB
Given a tree and a metro line on each station pair, for each station sum the even-indexed values of the sorted line numbers that pass through it.
- Level
Medium7 of 10
- Topics
- Tree, DFS, Segment tree, Sorting
- Solved
- No attempts yet
Problem
In recent years the Compulsive Disorder Research Institute has become busy discovering and classifying all sorts of peculiar behaviors. You have been dispatched to investigate one such strange compulsion reported among ordinary metro travelers.
The underground metro has stations connected by bidirectional paths, and metro lines with linear routes, each with a unique number. Each station has a display that shows the numbers of the metro lines passing through it in sorted order. One subject claims that whenever he sees a list of integers , he cannot resist computing the sum of the even-indexed terms . Each display in the stations shows such a list.
Compute this sum for each of the displays.
Input
The first line contains two integers and . The next lines each contain two integers and , meaning there is a direct link between stations and . The next lines each contain three integers , , and , meaning the metro line numbered goes from station to station .
Output
Output lines. Line must contain a single number, the sum for the display in station . If no metro passes through station , print 0 on line .
Constraints
, , and for all . All stations are reachable from each other.