• [1554] Lord of Minecraft

  • 时间限制: 1000 ms 内存限制: 65535 K
  • 问题描述
  • Hungar wants to be a lord of her own world of Minecraft. At first she builds a main territory.

    We assume the yellow territory is the King's. And blue ones are dukes', purple ones are marquis' while the gray ones are earls'.

    And there're countless levels of title of nobility. One noble only have one superior, and his territory only have the direct roads between his direct superior's territory and direct inferior's.
    After Hungar building such a world, she lets men in and allocations them territories.
    Now the question is when a man wants to visit another one's territory, should he pass a territory that belongs a noble that has the same level with him?
  • 输入
  • This problem contains several cases, ends with EOF.
    The first line of each case contains two integers N (1 ~ 10000) and Q (1 ~ 100000), indicates the number of nobles (or territories) and the number of that men want to visit others' territories.
    Then N lines followed. Each line contains two names which the first name represent one noble and the second name is his/her superior. You should know "Hungar" is the King, and she ONLY appears in the second names. You can assume everyone belong to the Hungar King directly or indirectly.
    Next follow Q lines. Each line contains two names that means the man who wants to visit others and his/her destination.
    Assume that the length of each name will not exceed 10 and contains only letters.
  • 输出
  • For each case, you should count the number of that one will pass through a territories that equals his level.
  • 样例输入
  • 4 4
    B Hungar
    C B
    D B
    E Hungar
    C D
    C Hungar
    C E
    C B
    
  • 样例输出
  • 1
    
  • 提示
  • 来源
  • XadillaX
  • 操作

显示春菜