#K88157. Maximum Depth of a Tree
Maximum Depth of a Tree
Maximum Depth of a Tree
You are given a tree representing an organizational hierarchy. Your task is to compute its maximum depth. The depth of a tree is defined as follows:
For an empty tree, the depth is 0. For a non-empty tree with root node, the depth is given by \[ d = 1 + \max_{child \in children}(d(child)) \] where \(d(child)\) denotes the depth of a child subtree.
The tree is provided in a simple format where the first line is an integer \(n\) (number of nodes). Each of the following \(n\) lines describes a node in order from 1 to \(n\) (with node 1 being the root). Each node description begins with an integer \(k\) representing the number of children, followed by \(k\) integers indicating the indices of its children. If \(n = 0\), the tree is empty.
Your program should read from standard input and write the result (the maximum depth of the tree) to standard output.
inputFormat
The input is given via standard input and has the following format:
- An integer \(n\) on the first line representing the number of nodes in the tree. If \(n = 0\), the tree is empty.
- For each node \(i\) from 1 to \(n\), there is a line containing an integer \(k\) (the number of children of node \(i\)) followed by \(k\) space-separated integers representing the indices of its children.
Note: Node 1 is always the root if \(n > 0\).
outputFormat
The output should be a single integer written to standard output—the maximum depth of the tree.
## sample5
3 2 3 4
0
1 5
0
0
3