Hihocoder #1067 Recent public ancestor · Two

Source: Internet
Author: User

#1067: Recent public ancestor • Two time limit:10000mscase time Limit:1000msmemory LIMIT:256MB Description

Last said, small hi and small ho with very poor--or rough means of the cottage out of a magical site, this site can calculate the two people of all the common ancestor generational the lowest one is who. Far away in the United States, they used some wonderful technology to get information about many people in the country and set up a small website to deal with requests from all directions.

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

One is to buy more expensive servers to meet demand by improving computer performance-but little Hi and Little ho don't have that much money, and the second is to improve their algorithms to meet demand by improving the utilization of computer performance-an idea that sounds more plausible.

So for the smooth operation of their first online product, little Hi decided to train the little ho urgently--to modify their algorithms well.

And in order to better explain the problem to small ho, little hi has abstracted this question into this way: Suppose now little ho now knows the N-to-father-son relationship--the name of fathers and Sons, and that N for all involved in a parent-child relationship has a common ancestor (this ancestor appears in the N-parent relationship), He needs to ask a few questions about little Hi-the name of two people per question (the names of the two people in the previous parent-child relationship), and tell Little hi who is the lowest generational in all the common ancestors of these two people?

Tip One: Honestly the situation discussion will not be wrong!

Hint two: And check set actually looks like a tree, don't you think?

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, to satisfy the n<=10^5,m<=10^5, and all the characters involved in the data does not exist two names of the same person (that is, the name of the only one identified), all of the names of the queries have been described in the previous N-parent-child relationship, The first name to be identified is the common ancestor of all other people .

Output

For each set of test data, for each small hi query, according to the order in the input, each output line, indicating the results of the queries: all of their common ancestors generational the lowest of a person's name.

Sample Input
4Adam Samsam joeysam michealadam kevin3sam samadam sammicheal Kevin
Sample Output
Samadamadam

Problem Solving: LCA

1#include <iostream>2#include <cstdio>3#include <cstring>4#include <cmath>5#include <algorithm>6#include <climits>7#include <vector>8#include <queue>9#include <cstdlib>Ten#include <string> One#include <Set> A#include <map> -#include <stack> - #defineLL Long Long the #defineINF 0x3f3f3f3f - #definePII pair<int,int> - using namespacestd; - Const intMAXN =200010; + structarc{ -     intTo,next; +Arcintx =0,inty =-1){ Ato =x; atNext =y; -     } - }; -Arc e[1000000]; - intHEAD[MAXN],UF[MAXN],FA[MAXN],ANS[MAXN]; - inttot,cnt,n,m; in BOOLVIS[MAXN]; -vector< PII >QUERY[MAXN]; to voidAddintUintv) { +E[tot] =arc (V,head[u]); -Head[u] = tot++; the } * intFind (intx) { $     if(x = uf[x]) uf[x] =Find (uf[x]);Panax Notoginseng     returnUf[x]; - } the voidTarjan (intu) { +Fa[u] =u; A      for(inti = Head[u]; ~i; i =E[i].next) { the         //if (vis[e[i].to]) continue; + Tarjan (e[i].to); -         intx =Find (e[i].to); $UF[X] =uf[find (U)]; $Fa[find (u)] =u; -     } -Vis[u] =true; the      for(inti = query[u].size ()-1; I >=0; --i) -         if(Vis[query[u][i].first]) Ans[query[u][i].second] =Fa[find (Query[u][i].first)];Wuyi } the voidinit () { -      for(inti =0; i < MAXN; ++i) { WuHead[i] =-1; -Vis[i] =false; About query[i].clear (); $Uf[i] =i; -     } -tot =0; -CNT =1; A } +map<string,int>MP; the stringname[maxn],a,b; - intMain () { $     intu,v; the      while(~SCANF ("%d",&N)) { the init (); the mp.clear (); the          for(inti =0; I < n; ++i) { -Cin>>a>>b; in             if(Mp[a]) U =Mp[a]; the             Else{ theU = mp[a] =CNT; Aboutname[cnt++] =A; the             } the             if(Mp[b]) v =Mp[b]; the             Else{ +v = mp[b] =CNT; -name[cnt++] =b; the             }Bayi Add (u,v); the         } thescanf"%d",&m); -          for(inti =0; I < m; ++i) { -Cin>>a>>b; the Query[mp[a]].push_back (Make_pair (mp[b],i)); the Query[mp[b]].push_back (Make_pair (mp[a],i)); the         } the          for(inti =1; I < CNT; ++i) { -             if(!Vis[i]) Tarjan (i); the         } the         //Tarjan (1); the          for(inti =0; I < m; ++i)94cout<<name[ans[i]]<<Endl; the     } the     return 0; the}
View Code

Hihocoder #1067 Recent public ancestor · Two

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.