The Leisurely Stroll

Time limit1sMemory limit128 MB

Summary
Given a rooted tree of choice-nodes where leaf edges lead to pastures, find the maximum number of edges on any root-to-pasture path.
Level

Easy3 of 10

Topics
Tree, DFS, Recursion, Implementation
Solved
No attempts yet

Problem

Bessie steps out of the barn on a beautiful spring day and wants to enjoy the longest possible walk to the pastures before breakfast. Starting at the barn (node 11), she walks along a path until she reaches a choice-node, where she picks one of two paths. She keeps choosing at each choice-node until a path finally leads her to a pasture.

Bessie wants to make the set of choices that lets her walk over the greatest number of cow paths on the way to a pasture. Given the layout of the paths, determine how many cow paths she traverses when she walks to the furthest pasture.

The farm has PP (1≤P≤10001 \le P \le 1000) pastures, reached through P−1P-1 choice-nodes numbered 1…P−11 \dots P-1 and connected by paths. From the barn (node 11) there is exactly one route to any choice-node or pasture, so the paths form a tree rooted at the barn.

The picture below shows the paths (lines), the pastures (%), and, on the right, one highlighted (#) route to a pasture:

                 %                             %
                /                             /
      2----%   7----8----%          2----%   7####8----%
     / \      /      \             # #      #      #
    1   5----6        9----%      1   5####6        9----%
     \   \    \        \           \   \    \        #
      \   %    %        %           \   %    %        %
       \                             \
        3-----%                       3-----%
         \                             \
          4----%                        4----%
           \                             \
            %                             %

The pasture reached through choice-node 99 is one of two pastures that let Bessie walk over seven different cow paths on the way to breakfast; these are the furthest pastures from the barn (node 11).

Each choice-node is described by three integers CnC_n, D1D_1, and D2D_2. CnC_n is the node number (1≤Cn≤P−11 \le C_n \le P-1); D1D_1 and D2D_2 are the two destinations reachable from that node (0≤D1≤P−10 \le D_1 \le P-1, 0≤D2≤P−10 \le D_2 \le P-1). A destination of 00 means that direction leads to a pasture; any other value is the number of the choice-node reached in that direction.

Input

  • Line 11: a single integer PP.
  • Lines 2…P2 \dots P: line i+1i+1 contains three space-separated integers describing one choice-node: CnC_n, D1D_1, and D2D_2.

Output

  • Line 11: a single integer, the largest number of cow paths Bessie can traverse on the way to the furthest pasture.

Hint

The route 1-2-5-6-7-8-9-P (ending at a pasture) is one of the longest possible walks.

Examples1

  1. Example 1

    Input
    10
    7 8 0
    5 0 6
    9 0 0
    6 0 7
    3 4 0
    2 5 0
    8 0 9
    4 0 0
    1 2 3
    
    Expected output
    7