Time limit: 10000ms single point time limit: 1000ms memory limit: 256MB description
Little Ho has recently discovered a magical website! It's not as magical as 58, but this site still makes little ho happy, but why?
"For what?" Little Hi asked, in his observation that the small Ho has been addicted to the site for a week, and even his beloved tree toys are discarded one side.
"Hey, little Hi, you have to come and see!" Little Ho said hello.
"You see, enter my name in this dialog box, enter your name in another dialog, and then click the Query button to find out ..." What the! We actually have the same Zouzouzouzou grandpa? ”
"Eh, really ah ... This site is a bit tough. "Little hi not by exclamation."
"Yes, this is what algorithm ah, so powerful!" Little Ho also echoed the way.
"2, I said he can get these data is very powerful, and the human breeding tree this kind of layer of relatively shallow trees to the requirements of this kind of algorithm is very simple, you can write it!" "Little Hi Way."
"Ah?" Can I write it? But...... Where should we start? Little Ho was puzzled.
The problem for little Ho is this, assuming that now he knows N personal information-who their father is, he needs to ask every question about the little hi--two people's names, tell little hi whether the same ancestor of the two people, if present, then all of their common ancestors generational the lowest one is who?
Hint: No hurry, slowly, in addition I have a question: excavator technology which strong?!
Input
Each test point (input file) has and has only one set of test data.
The 1th behavior of each set of test data is an integer n, meaning as described earlier.
Each group of test data in line 2~n+1, each line describes a pair of parent-child relationship, where the i+1 behavior of two a string of uppercase and lowercase letters Father_i, son_i, respectively, the name of the father and the son.
The n+2 behavior of each set of test data is an integer m, which indicates the number of times a small hi has been asked.
Each group of test data in line n+3~n+m+2, each line describes a query, where the n+i+2 behavior of two characters of uppercase and lowercase letters name1_i, name2_i, respectively, a small hi asked two names.
For 100% of the data, the n<=10^2,m<=10^2 is satisfied, and all the characters involved in the data do not have two people with the same name (that is, the name uniquely identifies a person).
Output
For each set of test data, for each small hi query, the output line, indicating the results of the inquiry: if according to the known information, you can determine that the two people in the inquiry have a common ancestor, then output all of their common ancestor generational the lowest name of a person, otherwise output-1.
-
-
Sample input
-
-
11JiaYan Jiadaihuajiadaihua Jiafujiadaihua jiajingjiajing jiazhenjiazhen Jiarongjiayuan Jiadaishanjiadaishan Jiashejiadaishan jiazhengjiashe Jialianjiazheng Jiazhujiazheng jiabaoyu3jiabaoyu JiaLianJiaBaoyu JiaZhengJiaBaoyu Lindaiyu
-
-
Sample output
-
JiaDaishanJiaZheng-1
1#include"iostream"2#include"Map"3#include"algorithm"4#include"string"5 using namespacestd;6 7map<string,string>Pre;8map<string,int>Vis;9 Ten intMain () One { A intN, M; - while(Cin >>N) { - stringA, B; the while(n--) - { -Cin >> a >>b; -PRE[B] =A; + } -CIN >>m; + while(m--) A { at vis.clear (); -Cin >> a >>b; - while(!A.empty ()) { -Vis[a] =1; -A =Pre[a]; - } in while(Vis[b] = =0&&!b.empty ()) b =Pre[b]; - if(!b.empty ()) tocout << b <<Endl; + Elsecout <<"-1"<<Endl; - } the pre.clear (); * } $ return 0;Panax Notoginseng} code June
Recent public ancestor (simple version)