hdu4409Family Name List

Family Name List

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1313    Accepted Submission(s): 435


Problem Description
Kong 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.
 

Input
There 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.
 

Output
Already mentioned in the input.
 

Sample Input
  
  
9 Kongs .son1 ..son1son2 ..son1son1 ...sonkson2son1 ...son1son2son2 ..son1son3 ...son1son3son1 .son0 7 L b son1son3son1 b son1son2 b sonkson2son1 b son1 c sonkson2son1 son1son2son2 c son1son3son1 son1son2 0
 

Sample Output
  
  
Kongs .son0 .son1 ..son1son1 ...son1son2son2 ...sonkson2son1 ..son1son2 ..son1son3 ...son1son3son1 1 3 2 2 son1son1 son1
题意:有三种操作,一是按祖先到后代,兄弟间按字典序由小到大排序,然后输出,二是输出自己的兄弟节点有多少个(包括自己),三是a和b两个节点的最近公共祖先,若a或b是公共祖先,则应输出他们的父亲。
思路:把整棵树整理一下,用son数组表示儿子,也就是兄弟的数量,fa数组表示父亲,排序可以直接用sort排序,然后用RMQ搞在线LCA。
代码:
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <queue>
#include <vector>
#include <cmath>
#include <map>
using namespace std;
const int N=55555;
const int M=111111;
map<string,int>mp;
int son[N],fa[N],cnt[N];
vector<int>g[N];
string ts[N],ns[N];
int a[M],b[M],dp[M][32],f[M],num,index;
void init()
{
    index=num=0;
    memset(a,0,sizeof(a));
    memset(b,0,sizeof(b));
    memset(dp,0,sizeof(dp));
    memset(f,0,sizeof(f));
}
void dfs(int u,int pre)
{
    int t=++index;
    f[t]=u;
    b[++num]=t;
    a[u]=num;
    for(int i=0;i<g[u].size();i++)
    {
        int v=g[u][i];
        if(v==pre)continue;
        dfs(v,u);
        b[++num]=t;
    }

}
void RMQ_init(int n)
{
    for(int i=1;i<=n;i++)dp[i][0]=b[i];
    int m=floor(log((double)n*1.0)/log((double)2.0));
    for(int j=1;j<=m;j++)
    {
        for(int i=1;i<=n-(1<<j)+1;i++)
        {
            dp[i][j]=min(dp[i][j-1],dp[i+(1<<(j-1)][j-1]);
        }
    }
}
int RMQ(int l,int r)
{
    int m=floor(log((double)r-l+1)/log((double)2.0));
    return min(dp[l][m],dp[r-(1<<m)+1][m]);
}
int LCA(int u,int v)
{
    if(a[u]>a[v])swap(u,v);
    return f[RMQ(a[u],a[v])];
}
void dfss(int u,int dep)
{
    for(int i=0;i<dep;i++)printf(".");
    cout<<ns[u]<<endl;
    for(int i=0;i<g[u].size();i++)
    {
        int v=g[u][i];
        dfss(v,dep+1);
    }
}
int main()
{
    int n;
    while(~scanf("%d",&n)&&n)
    {
        int i,j;
        init();
        mp.clear();
        memset(fa,0,sizeof(fa));
        memset(son,0,sizeof(son));
        memset(cnt,0,sizeof(cnt));
        for(i=0;i<=n;i++)
        {
            g[i].clear();
        }
        for(i=1;i<=n;i++)
        {
            cin>>ts[i];
            int len=ts[i].length();
            for(j=0;j<len;j++)
            {
                if(ts[i][j]!='.')break;
            }
            ns[i]=ts[i].substr(j,len-j);
        }
        sort(ns+1,ns+1+n);
        for(i=1;i<=n;i++)
        {
            mp[ns[i]]=i;
        }
        string s;
        for(i=1;i<=n;i++)
        {
            s=ts[i];
            int len=s.length();
            for(j=0;j<len;j++)
            {
                if(s[j]!='.')break;
            }
            string S=s.substr(j,len-j);
            cnt[j]=mp[S];
            if(j!=0)
            {
                fa[mp[S]]=cnt[j-1];
                son[cnt[j-1]]++;
                g[cnt[j-1]].push_back(mp[S]);
            }
            else fa[mp[S]]=0;
        }
        for(i=1;i<=n;i++)
        {
            sort(g[i].begin(),g[i].end());
        }
        for(i=1;i<=n;i++)
        {
            if(fa[i]==0)break;
        }
        int ff=i;
        dfs(ff,0);
        RMQ_init(num);
        int m;
        char c[5];
        scanf("%d",&m);
        while(m--)
        {
            scanf("%s",c);
            if(c[0]=='L')
            {
                dfss(ff,0);
            }
            else if(c[0]=='b')
            {
                string ss;
                cin>>ss;
                int k=mp[ss];
                if(k==ff)printf("1\n");
                else printf("%d\n",son[fa[k]]);
            }
            else
            {
                string s1,s2;
                cin>>s1>>s2;
                int m1=mp[s1];
                int m2=mp[s2];
                int kk=LCA(m1,m2);
                if(m1==kk||m2==kk)
                {
                    cout<<ns[fa[kk]]<<endl;
                }
                else cout<<ns[kk]<<endl;
            }
        }
    }
    return 0;
}


 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
辽B代驾管理系统对代驾订单管理、用户咨询管理、代驾订单评价管理、代驾订单投诉管理、字典管理、论坛管理、公告管理、新闻信息管理、司机管理、用户管理、管理员管理等进行集中化处理。经过前面自己查阅的网络知识,加上自己在学校课堂上学习的知识,决定开发系统选择小程序模式这种高效率的模式完成系统功能开发。这种模式让操作员基于浏览器的方式进行网站访问,采用的主流的Java语言这种面向对象的语言进行辽B代驾管理系统程序的开发,在数据库的选择上面,选择功能强大的Mysql数据库进行数据的存放操作。辽B代驾管理系统的开发让用户查看代驾订单信息变得容易,让管理员高效管理代驾订单信息。 辽B代驾管理系统具有管理员角色,用户角色,这几个操作权限。 辽B代驾管理系统针对管理员设置的功能有:添加并管理各种类型信息,管理用户账户信息,管理代驾订单信息,管理公告信息等内容。 辽B代驾管理系统针对用户设置的功能有:查看并修改个人信息,查看代驾订单信息,查看公告信息等内容。 辽B代驾管理系统针对管理员设置的功能有:添加并管理各种类型信息,管理用户账户信息,管理代驾订单信息,管理公告信息等内容。 辽B代驾管理系统针对用户设置的功能有:查看并修改个人信息,查看代驾订单信息,查看公告信息等内容。 系统登录功能是程序必不可少的功能,在登录页面必填的数据有两项,一项就是账号,另一项数据就是密码,当管理员正确填写并提交这二者数据之后,管理员就可以进入系统后台功能操作区。项目管理页面提供的功能操作有:查看代驾订单,删除代驾订单操作,新增代驾订单操作,修改代驾订单操作。公告信息管理页面提供的功能操作有:新增公告,修改公告,删除公告操作。公告类型管理页面显示所有公告类型,在此页面既可以让管理员添加新的公告信息类型,也能对已有的公告类型信息执行编辑更新,失效的公告类型信息也能让管理员快速删除。新闻管理页面,此页面提供给管理员的功能有:新增新闻,修改新闻,删除新闻。新闻类型管理页面,此页面提供给管理员的功能有:新增新闻类型,修改新闻类型,删除新闻类型。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值