Family Tree
InterviewTime limit3sMemory limit2048 MB
Given a family tree of n people, find the minimum width of a drawing where children sit one level below parents and pictures in a level may shift sideways.
Problem
While you are attending the yearly family gathering, you notice that your family keeps growing bigger and bigger. You are having a hard time remembering for every member of the family what their name is and to what part of the family they belong to. To solve this, you decide to create a family tree on a big sheet of paper. You collect a portrait picture from every member of the family and stick them onto the paper in the shape of a tree, putting pictures of children exactly one level below the picture of their parents.
Before actually sticking the pictures on the paper, you need to figure out how much paper you need. Environmentally aware as you are, you try to minimize the amount of paper needed. You decide to allow pictures within the same level to move to the left or right if this makes the tree less wide.
Input
One line with one integer: , the number of people in the family tree.
One line that indicates the earliest ancestor that you have information about.
lines, each in the format A - B, indicating that A is the parent of B. These lines have no particular order.
The name of each person consists of at most 20 characters from the English alphabet (A-Z and a-z).
Output
The minimum width of the family tree in the number of portrait pictures.