Description
Now there is a valid binary tree. the nodes in the tree are represented by numbers. Now, given all the parent-child relationships on the tree, Please calculate the height of the tree.
Input description:
The first line indicates the number of nodes N (1 ≤ n ≤ 1000, and the number of nodes is 0 to n-1,
There are n-1 rows. Each row has two integers. The first number indicates the number of the parent node, and the second number indicates the number of the child node.
Output description:
The height of the output tree, which is an integer.
Example 1
Input
5
0 1
0 2
1 3
1 4
Output
3
# Import sys # sys. stdin = open('input.txt ', 'R') n = int (input () tree = [1 For I in range (n)] childnum = [0 for I in range (n)] For I in range (n-1): parent, child = map (INT, input (). split () If childnum [Parent]> = 2: tree [child] = 0 childnum [child] = 2 continue tree [child] + = tree [Parent] childnum [Parent] + = 1 print (max (tree ))
Tree height (no need to build a tree, no need to consider who is the child, because the input ensures the parent node)