#P2959. Bessie's Maximum Trail Adventure

    ID: 16217 Type: Default 1000ms 256MiB

Bessie's Maximum Trail Adventure

Bessie's Maximum Trail Adventure

Bessie gazes out from the barn on a beautiful spring morning and dreams of wandering through a lush pasture with soft grass tickling her hooves. However, her path to the pasture is full of forked trails. Starting at the barn (node 1), she is faced with a sequence of bifurcations. Each fork node is described by three integers C, D1, D2, where C is the node number and D1 and D2 are the two destinations. If a destination is 0, that path directly leads to a lush grassland.

There are exactly \(P-1\) fork nodes (with \(1 \le C \le P-1\)) and \(P\) grasslands. From any given fork node, there is a unique path from the barn to that node. Bessie wants to choose the route that allows her to walk through as many distinct trails as possible before reaching a grassland. Your task is to compute the maximum number of trails she can traverse on her way to a grassland.

Note: Once Bessie leaves the barn (node 1), she immediately faces two choices. At each fork node, if a destination is 0, that edge counts as one trail leading directly to a grassland. Otherwise, continue exploring recursively.

inputFormat

The input begins with an integer \(P\) (\(1 \le P \le 1000\)) on the first line. This is followed by \(P-1\) lines, each containing three integers: C, D1 and D2.

If a destination value is 0, it indicates that the path leads directly to a grassland. The barn is represented as node 1.

outputFormat

Output a single integer representing the maximum number of trails Bessie can traverse on her way from the barn (node 1) to a grassland.

sample

4
1 2 3
2 0 0
3 0 0
2