hdu 4409 Family Name List(LCA&有坑點),hdu4409

來源:互聯網
上載者:User

hdu 4409 Family Name List(LCA&有坑點),hdu4409
Family Name ListTime Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 882    Accepted Submission(s): 271


Problem DescriptionKong belongs to a huge family. Recently he got a family name list which lists all men (no women) in his family over many generations. 

The list shows that the whole family has a common ancestor, let's call him Mr. X. Of course, everybody except Mr.X in the list is Mr. X's descendant. Everybody's father is shown in the list except that Mr. X's father is not recorded. We define that Mr. X's generation number is 0. His son's generation number is 1.His grandson's generation number is 2, and so on. In a word, everybody's generation number is 1 smaller than his son's generation number. Everybody's generation number is marked in some way in the list.

Now Kong is willing to pay a lot of money for a program which can re-arrange the list as he requires ,and answer his questions such as how many brothers does a certain man have, etc. Please write this program for him.
 
InputThere are no more than 15 test cases. 
For each test case:
The first line is an integer N( 1 <= N <= 30,000), indicating the number of names in the list.
The second line is the name of Mr. X.
In the next N-1 lines, there is a man's name in each line. And if the man's generation number is K, there are K dots( '.') before his name.

Please note that :
1) A name consists of only letters or digits( '0'-'9').
2) All names are unique.
3) Every line's length is no more than 60 characters.
4) In the list, a man M's father is the closest one above M whose generation number is 1 less than M.
5) For any 2 adjacent lines in the list, if the above line's generation number is G1 and the lower line' s generation number is G2, than G2 <= G1 +1 is guaranteed. 

After the name list, a line containing an integer Q(1<=Q<=30,000) follows, meaning that there are Q queries or operations below.

In the Next Q lines, each line indicates a query or operation. It can be in the following 3 formats:
1) L
Print the family list in the same format as the input, but in a sorted way. The sorted way means that: if A and B are brothers(cousins don’t count), and A's name is alphabetically smaller than B's name, then A must appear earlier than B. 
2) b name
Print out how many brothers does "name" have, including "name" himself.
3) c name1 name2
Print out the closest common ancestor of "name1" and "name2". "Closest" means the generation number is the largest. Since Mr. X has no ancestor in the list, so it's guaranteed that there is no question asking about Mr. X's ancestor. 

The input ends with N = 0.
 
OutputAlready mentioned in the input.
 
Sample Input

9Kongs.son1..son1son2..son1son1...sonkson2son1...son1son2son2..son1son3...son1son3son1.son07Lb son1son3son1b son1son2b sonkson2son1b son1c sonkson2son1 son1son2son2c son1son3son1 son1son20 
 
Sample Output
Kongs.son0.son1..son1son1...son1son2son2...sonkson2son1..son1son2..son1son3...son1son3son11322son1son1son1
 
Source2012 ACM/ICPC Asia Regional Jinhua Online 
Recommendzhoujiaqi2010   |   We have carefully selected several similar problems for you:  5053 5052 5051 5050 5049  題意:給你一棵樹。有三種操作。1.輸出樹的dfs序。字典序小的先輸出。2.輸出一個結點的父親有多少兒子。包括自己。3.輸出u,v的LCA。思路:這題輸入比較蛋疼。我是用一個棧來建樹的。不知道有其它什麼進階方法沒。然後對於操作1.由於要字典序小的的先dfs。那麼只好用 不是很熟悉的vector存邊了。然後對邊按名字字典序排序。然後dfs一次把答案存起來。對於2記錄下一個結點的父親是誰就行了。對於3.tarjan離線處理。這題有個坑點就是LCA不能是自己。over。詳細見代碼:
#include<cstdio>#include<vector>#include<algorithm>#include<string>#include<cstring>#include<iostream>#include<map>using namespace std;const int maxn=30010;int cnt,ptr,pp,vis[maxn],ty[maxn],aans[maxn];int st[maxn],rk[maxn],fa[maxn],pa[maxn],uu[maxn],vv[maxn];char na[100];vector<int> G[maxn];string name[maxn],ans[maxn];map<string,int> mp;struct node{    int v,id;    node *next;} ed[maxn<<1],*head[maxn];void adde(int u,int v,int id){    ed[ptr].v=v;    ed[ptr].id=id;    ed[ptr].next=head[u];    head[u]=&ed[ptr++];}bool cmp(int a,int b){    return name[a]<name[b];}void dfs(int u){    string op=".";    ans[pp]="";    for(int i=0;i<rk[u];i++)        ans[pp]+=op;    ans[pp++]+=name[u];    for(int i=0;i<G[u].size();i++)    {        pa[G[u][i]]=u;        dfs(G[u][i]);    }}int getfa(int x){    if(fa[x]==x)        return x;    return fa[x]=getfa(fa[x]);}void tarjan(int u){    vis[u]=1,fa[u]=u;    for(node *p=head[u];p!=NULL;p=p->next)    {        if(vis[p->v])            aans[p->id]=getfa(p->v);    }    for(int i=0;i<G[u].size();i++)    {        tarjan(G[u][i]);        fa[G[u][i]]=u;    }}int main(){    int i,j,n,m,tp,ct,id,u,v;    string aa,bb;    char cmd[20];    while(scanf("%d",&n),n)    {        for(i=0;i<=n;i++)            G[i].clear();        mp.clear();        tp=cnt=1;        st[0]=0,rk[0]=-1;        for(i=0;i<n;i++)        {            scanf("%s",na);            ct=0;            for(j=0;na[j];j++)                if(na[j]=='.')                    ct++;                else                    break;            string nna(na+ct);            //cout<<nna<<endl;            if(!mp.count(nna))            {                name[cnt]=nna;                rk[cnt]=ct;                mp[nna]=cnt++;            }            id=mp[nna];            while(rk[st[tp-1]]>=rk[id])                tp--;            G[st[tp-1]].push_back(id);            st[tp++]=id;        }        for(i=1;i<=n;i++)            sort(G[i].begin(),G[i].end(),cmp);        pp=0;        dfs(1);        ptr=0;        memset(head,0,sizeof head);        memset(vis,0,sizeof vis);        scanf("%d",&m);        for(i=0;i<m;i++)        {            scanf("%s",cmd);            if(cmd[0]=='L')                ty[i]=0;            else if(cmd[0]=='b')            {                ty[i]=1;                cin>>aa;                id=mp[aa];                aans[i]=G[pa[id]].size();            }            else            {                ty[i]=2;                cin>>aa>>bb;                u=mp[aa],v=mp[bb];                uu[i]=u,vv[i]=v;                adde(u,v,i);                adde(v,u,i);            }        }        tarjan(1);        for(i=0;i<m;i++)        {            if(ty[i]==0)            {                for(j=0;j<n;j++)                    cout<<ans[j]<<endl;            }            else if(ty[i]==1)                printf("%d\n",aans[i]);            else            {                if(aans[i]==uu[i]||aans[i]==vv[i])                    aans[i]=pa[aans[i]];                cout<<name[aans[i]]<<endl;            }        }    }    return 0;}





聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.