← 返回 goldmansachs 的题目列表Root of the Largest Tree in a Parent-Child Forest
类型:online_judge
Given parent -> child relationships that form a forest (acyclic, and every node has at most one parent), return the root of the tree with the largest number of nodes.
The size of a tree is the number of distinct nodes in it.
Every node appearing in the input belongs to the forest.
If multiple trees have the same maximum size, return the lexicographically smallest root.
Input Format
The first line contains an integer m, the number of relationships.
The next m lines each contain two strings, parent child, representing parent -> child.
Output Format
Print the root of the largest tree.
Constraints
0 <= m <= 2 * 10^5
Node names are non-empty strings without spaces.
Example
Input:
5
A B
A C
D E
D F
D G
Output:
D
Explanation: the tree rooted at A has 3 nodes, while the tree rooted at D has 4 nodes.
Example
Input
5
A B
A C
D E
D F
D G
Output
D