#P5558. Maximum Lit Corridors

    ID: 18788 Type: Default 1000ms 256MiB

Maximum Lit Corridors

Maximum Lit Corridors

In the first year of the Jingning era (33 BC), on the eve before Zhaojun departs, a painter stumbles into her palace. In the dim corridors with walls painted with maple leaves—each representing the number of candles in that corridor—the painter recalls that Zhaojun loved autumn. Before departing forever, he wishes to leave her his brush by lighting as many corridors as he can, following her peculiar habit.

The palace has N rooms connected by N-1 corridors, forming a tree. The painter starts in room S (the entrance hall) and must reach room T (Zhaojun's room) along the unique simple path in the tree. Each corridor is adorned with a number of maple leaves, and this number exactly equals the number of candles provided therein.

Zhaojun’s habit is as follows: when she decides to light the candles in a corridor, she lights all of them. Once a corridor is lit, no corridor with fewer candles than this one will ever be lit again along the journey. In other words, if the painter lights a corridor with x candles, any corridor encountered later that has less than x candles must be left unlit.

This means that along the unique path from S to T, the painter can choose to light a subset of corridors such that the sequence of their candle counts is non-decreasing (equality is allowed). Your task is to help him determine the maximum number of corridors that can be lit while obeying this rule.

inputFormat

The first line contains three integers N, S, and T representing the number of rooms, the starting room, and Zhaojun's room respectively.

The following N-1 lines each contain three integers u, v, and w, indicating that there is a corridor connecting room u and room v with w candles (which equals the number of maple leaves drawn on its wall). It is guaranteed that the corridors form a tree.

outputFormat

Output a single integer: the maximum number of corridors that can be lit (i.e. the length of the longest non-decreasing subsequence of corridor candle counts) on the unique path from room S to room T.

sample

5 1 5
1 2 3
2 3 2
3 4 2
4 5 3
3