HihoCoder _ #1067 _ Recent common ancestor & amp; #183; 2 (LCA template)

Source: Internet
Author: User

HihoCoder _ #1067 _ Recent common ancestor & #183; II (LCA template)
#1067: Recent common ancestor-two-time limit: 10000 ms single-point time limit: 1000 ms memory limit: 256 MB

Description

The last time I said, Xiao Hi and Xiao Ho used very poor tools-or a rough means to create a magical website, this website can calculate who is the least common ancestor of two people. As far away as the United States, they used some wonderful technologies to obtain information from many people in China and set up a small website to handle requests from all directions.

But as we can imagine ...... Such a simple algorithm does not support a large amount of traffic, so there are only two options in front of Xiao Hi and Xiao Ho:

The first is to purchase more expensive servers and meet the needs by improving computer performance-but little Hi and little Ho do not have that much money; the second is to improve their algorithms, it seems more reliable to meet the demand by improving the utilization of computer performance.

So for the smooth operation of their first online product, Xiao Hi decided to conduct emergency training for Xiao Ho-to modify their algorithms.

In order to better Tell xiao Ho about this problem, Xiao Hi abstracts it into this way: Now Xiao Ho now knows N pairs of parent-child relationships-father and son's name, in addition, all the people involved in the N-pair parent-child relationship have a common ancestor (this ancestor appears in the N-pair parent-child relationship ), he needs to ask Xiao Hi several times-each time the question is the name of two people (the names of these two people appear in the previous parent-child relationship ), tell xiao Hi who is the least common ancestor of the two people?

 

Input

Each test point (input file) has only one set of test data.

The 1st behavior of each group of test data is an integer N, meaning as described above.

2nd ~ Line N + 1 describes a parent-child relationship with each line. The I + 1 act is a string consisting of uppercase and lowercase letters, Father_ I and Son_ I, indicating the father's name and son's name respectively.

The N + 2 behavior of each group of test data is an integer M, indicating the total number of requests of the small Hi.

N + 3 ~ of each group of test data ~ N + M + 2 rows, each line describes a query, where N + I + 2 acts two strings consisting of uppercase and lowercase letters Name1_ I, Name2_ I, the two names in the small Hi query.

For 100% of data, N <= 10 ^ 5, M <= 10 ^ 5, in addition, there are no two persons with the same name in all involved characters in the data (that is, the name uniquely identifies a person ),All the names in the query appear in the N-pair parent-child relationship described earlier. The first person with the same name is the public ancestor of all others..

Output

For each group of test data, the query for each small Hi is displayed in the order in which each row is output, indicating the query result: the name of the person with the lowest generation among all their common ancestor.

Sample Input
4Adam SamSam JoeySam MichealAdam Kevin3Sam SamAdam SamMicheal Kevin
Sample output
SamAdamAdam

 

Analysis: bare offline LCA algorithm. As a template.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.