Family Tree

시간 제한3초메모리 제한2048 MB

요약
n명으로 이루어진 루트 트리가 주어질 때, 각 레벨의 노드를 좌우로 옮겨 전체 가로 폭을 초상화 개수 단위로 최소화한다.
난이도

어려움10점 중 8점

유형
트리, 동적 계획법, DFS, 그리디
정답자
아직 제출이 없습니다

문제

While you are attending the yearly family gathering of your family, 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 (see also the examples below).

입력

  • One line with one integer: 2≤n≤1052 \leq n \leq 10^5, the number of people in the family tree.
  • One line that indicates the earliest ancestor that you have information about.
  •  n−1n - 1 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).

출력

The minimum width of the family tree in the number of portrait pictures.

예제2

  1. 예제 1

    입력
    8
    Alice
    Alice - Bob
    Alice - Carol
    Alice - Dave
    Bob - Eve
    Bob - Frank
    Carol - Grace
    Carol - Heidi
    
    예상 출력
    4
    
  2. 예제 2

    입력
    8
    Alice
    Alice - Bob
    Alice - Carol
    Alice - Dave
    Alice - Eve
    Bob - Frank
    Bob - Grace
    Carol - Heidi
    
    예상 출력
    4