#K57262. Longest Special Path in a Tree

    ID: 30382 Type: Default 1000ms 256MiB

Longest Special Path in a Tree

Longest Special Path in a Tree

Given a tree with \(n\) nodes where each node is assigned an integer value, you are to find the length of the longest special path starting from the root (node 1) and ending at any leaf.

A special path is defined as a path from the root to a leaf such that the sequence of node values is strictly increasing, i.e. \(a_{i+1} > a_i\) for every consecutive pair of nodes in the path. If an edge does not follow this rule, the path is restarted from that node, but note that only sequences that originally start from the root are regarded as valid.

Your task is to compute and output the maximum length of such a path.

inputFormat

The input is given via standard input (stdin). The first line contains an integer (n) denoting the number of nodes in the tree. The second line contains (n) space-separated integers representing the values of each node. Each of the following (n-1) lines contains two space-separated integers (u) and (v), indicating an undirected edge between nodes (u) and (v).

outputFormat

Output a single integer to standard output (stdout) representing the length of the longest special path from the root to any leaf.## sample

5
10 20 30 15 25
1 2
1 3
2 4
2 5
3