#P1344E. Train Tracks
Train Tracks
Description
That's right. I'm a Purdue student, and I shamelessly wrote a problem about trains.
There are stations and trains. The stations are connected by one-directional railroads that form a tree rooted at station . All railroads are pointed in the direction from the root station to the leaves. A railroad connects a station to a station , and has a distance , meaning it takes time to travel from to . Each station with at least one outgoing railroad has a switch that determines the child station an incoming train will be directed toward. For example, it might look like this:

Initially, no trains are at any station. Train will enter station at time . Every unit of time, starting at time , the following two steps happen:
- You can switch at most one station to point to a different child station. A switch change takes effect before step .
- For every train that is on a station , it is directed toward the station indicated by 's switch. So, if the railroad from to has distance , the train will enter station in units of time from now.
Every train has a destination station . When it enters , it will stop there permanently. If at some point the train is going in the wrong direction, so that it will never be able to reach no matter where the switches point, it will immediately explode.
Find the latest possible time of the first explosion if you change switches optimally, or determine that you can direct every train to its destination so that no explosion occurs. Also, find the minimum number of times you need to change a switch to achieve this.
The first line contains two integers and () — the number of stations and trains, respectively.
The next lines describe the railroads. The -th line contains three integers (, ), denoting a railroad from station to station with distance . It is guaranteed that the railroads form a tree rooted at station . The switch of a station is initially directed towards the last outgoing railroad from that appears in the input.
The next lines describe the trains. The -th line contains two integers (, ) — the destination station and the time the -th train enters station , respectively.
Output two integers: the latest possible time of the first explosion (or if it is possible to never have an explosion) and the minimum number of switch changes to achieve it.
Input
The first line contains two integers and () — the number of stations and trains, respectively.
The next lines describe the railroads. The -th line contains three integers (, ), denoting a railroad from station to station with distance . It is guaranteed that the railroads form a tree rooted at station . The switch of a station is initially directed towards the last outgoing railroad from that appears in the input.
The next lines describe the trains. The -th line contains two integers (, ) — the destination station and the time the -th train enters station , respectively.
Output
Output two integers: the latest possible time of the first explosion (or if it is possible to never have an explosion) and the minimum number of switch changes to achieve it.
Note
For the first test, here's an example timeline:
- At time , train enters station . We switch station to point to station . Train is directed to station .
- At time , train enters station , and train enters station , where it stops permanently. We switch station to point to station . Train is directed to station .
- At time , train enters station . We switch station to point to station . Train is directed to station .
- At time , train enters station , where it stops permanently.
- At time , train enters station . We switch station to point to station . Train is directed to station .
- At time , train enters station , where it stops permanently. We switch station to point to station .
- At time , train enters station . We switch station to point to station . Train is directed to station .
- At time , train enters station . Train is directed to station .
- At time , train enters station , where it stops permanently.
For the second test, we switch nothing. At time , train is directed to station and train is directed to station . They both explode. It is impossible to prevent an explosion by time .
For the third test, denote a switch change by if we make station point to station at time . One solution is to make these switch changes: ,,,. At time , trains ,, and explode. It is impossible to prevent an explosion by time .