WALK
Time limit1.5sMemory limit1024 MB
Given streets each with a recorded traversal time, find the longest walk from crossroad 1 to every crossroad where the times strictly increase.
- Level
Medium5 of 10
- Topics
- Dynamic programming, Graph, Sorting
- Solved
- No attempts yet
Problem
Who doesn't love walks?
The map of Shumen is a set of crossroads numbered from 1 to , joined by bidirectional streets numbered from 1 to . Each street joins two different crossroads, and no two crossroads are joined by more than one street. Any crossroad can be reached from any other by following the streets.
Kyusho and his dog Bobby went on a night walk the night before yesterday's party. They traversed every one of the streets at least once. Bobby is exceptionally intelligent but has trouble remembering the order of the streets, so his memory is chaotic and sometimes contradictory. For example, the dog may remember that some street was traversed at moment 10 while no street is remembered at moment 9. Formally, Bobby remembers that the -th street, between crossroads and , was traversed at moment .
The next morning Kyusho realized that he also has trouble remembering that night, so he wants to reconstruct the walk as well as he can. He starts a new walk at his hotel, crossroad 1. Each street he traverses must have a later moment (by Bobby's memory) than the street before it, that is, . He may pass through a crossroad more than once.
He wants the new walk to be as long as possible, and he has not decided where it should end. For each crossroad, find the longest walk that ends there and follows the rule above. The length is the number of streets passed.
Input
The first line contains two integers and , the number of crossroads and the number of streets. Each of the next lines contains three integers , , and , which describe a street between crossroads and that was traversed at moment according to Bobby's memory.
Output
Print integers separated by spaces: the maximal lengths of walks from crossroad 1 to crossroads . If no walk reaches a crossroad, print 0 for it.
Constraints
, , , , .
Hint
The figure below shows the streets and crossroads.

The optimal walk to crossroad 3 is . At crossroad 8, we cannot continue to crossroad 7, because that street has moment 5, and we arrived through a street with moment 7.