Under construction

Still being built. Things may change

Royal Succession

Interview

Time limit1sMemory limit128 MB

Summary
Given parent pairs for N people, compute each claimant's inherited fraction of the founder's blood and print the claimant with the largest fraction.
Level

Medium5 of 10

Topics
Graph, DFS, Hash map, Recursion
Solved
No attempts yet

Problem

The king of Utopia has died without leaving any descendants. Because the king named no successor, the royal nobles have each begun to claim the throne. The law of Utopia states that when the king has no successor, the person whose bloodline is closest to the country's founder shall rule.

The person whose bloodline is closest to the founder is a descendant of the founder whose blood is the least diluted by people other than the founder. Everyone inherits half of their bloodline from their father and half from their mother. A child of the founder carries 12\frac{1}{2} of the royal blood, and if that child marries someone who is not royalty, their child carries 14\frac{1}{4} of the royal blood. In this way, the fraction of blood inherited from the founder is determined generation by generation.

Write a program that, among the people claiming the throne, finds the one whose bloodline is closest to the founder, that is, the one who carries the largest fraction of the royal blood.

Input

The first line contains two integers NN and MM. (2≤N,M≤502 \le N, M \le 50)

The second line contains the name of the founder of Utopia.

Each of the next NN lines contains one piece of family information: three names separated by spaces. The first name is a child, and the other two names are that child's two parents.

Each of the next MM lines contains, one per line, the name of a person claiming the throne.

Every name is a string of 1 to 10 lowercase English letters. The founder never claims the throne and never appears as anyone's child.

Output

Print the name of the person whose bloodline is closest to the founder. The input is always such that the answer is unique.

Because the family relationships in this problem are built without regard to gender or age, some cases may not make sense in reality. However, every child has a unique pair of parents, no child is ever an ancestor of its own parents (there are no cycles), and no single person appears as a child more than once.

Examples8

  1. Example 1

    Input
    9 2
    edwardi
    charlesi edwardi diana
    philip charlesi mistress
    wilhelm mary philip
    matthew wilhelm helen
    edwardii charlesi laura
    alice laura charlesi
    helen alice bernard
    henrii edwardii roxane
    charlesii elizabeth henrii
    charlesii
    matthew
    
    Expected output
    matthew
    
  2. Example 2

    Input
    4 5
    andrew
    betsy andrew flora
    carol andrew betsy
    dora andrew carol
    elena andrew dora
    carol
    dora
    elena
    flora
    gloria
    
    Expected output
    elena
    
  3. Example 3

    Input
    2 2
    king
    alice king queenmum
    bob alice stranger
    alice
    bob
    
    Expected output
    alice
    
  4. Example 4

    Input
    2 3
    root
    child root outsider
    grand child mother
    child
    grand
    random
    
    Expected output
    child
    
  5. Example 5

    Input
    3 3
    founder
    a founder x
    b a y
    c b z
    a
    b
    c
    
    Expected output
    a
    
  6. Example 6

    Input
    3 3
    alpha
    beta alpha ext
    gamma alpha beta
    delta alpha gamma
    beta
    gamma
    delta
    
    Expected output
    delta
    
  7. Example 7

    Input
    5 2
    top
    pa top ea
    pb top eb
    gp pa pb
    ca top ec
    gc ca ed
    gp
    gc
    
    Expected output
    gp
    
  8. Example 8

    Input
    5 4
    crown
    sa crown oa
    sb crown ob
    ga sa oc
    gb crown sb
    mm ga gb
    gb
    mm
    ga
    sa
    
    Expected output
    gb