HDU 4409 - Family Name List (LCA)

10 篇文章 0 订阅

Family Name List

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


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
 
 
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
 

Source
 


注意不能把自己当祖先。

注意根的兄弟数是1。son[fa[root]]=1;

用string排序和输入和修改会简单


#include <iostream>
#include <stdio.h>
#include <math.h>
#include <string.h>
#include <string>
#include <algorithm>
#include <map>
#include <vector>
using namespace std;
#define LL long long
const int maxn = 40000+12;
int fa[maxn][20],d[maxn];
int son[maxn];
int n;
map<string,int> mp;
string name[maxn];
int on[70];
vector<int> G[maxn];

bool cmd(int a,int b)
{
    return name[a]<name[b];
}

void dfs(int u)
{
    sort(G[u].begin(),G[u].end(),cmd);
    for(int i=0;i<G[u].size();i++){
        int v=G[u][i];
        d[v]=d[u]+1;
        fa[v][0]=u;
        dfs(v);
    }
    son[u]=(int)G[u].size();

}
void lcainit()
{
    for(int I=1;(1<<i)<=n;i++){
        for(int j=1;j<=n;j++){
            fa[j][i]=fa[fa[j][i-1]][i-1];
        }
    }

}

void init()
{
    d[1]=1;
    mp.clear();
    for(int i=0;i<=n;i++)
        G[i].clear();
    memset(fa,0,sizeof fa);
    memset(son,0,sizeof son);
}


void pf(int u,int bei)
{
    for(int i=1;i<=bei;i++) printf(".");
    cout<<name[u]<<endl;
    for(int i=0;i<G[u].size();i++){
        int v=G[u][i];
        pf(v,bei+1);
    }
}


string query(int a,int b)
{
    if(d[a]<d[b]) swap(a, b);
    int dis=d[a]-d[b];
    for(int i=0;(1<<i)<=dis;i++){
        if((1<<i)&dis)
            a=fa[a][i];
    }
    if(a==b) return name[fa[a][0]];
    else{
        for(int i=(int)log2(n);i>=0;i--){
            if(fa[a][i]!=fa[b][i]){
                a=fa[a][i];
                b=fa[b][i];
            }
        }
        a=fa[a][0];
        return name[a];
    }

}
int main()
{
//     ios::sync_with_stdio(false);

    while(~scanf("%d",&n)&&n){
        init();
        for(int i=1;i<=n;i++){
            string nm;
            cin>>nm;
            int len=nm.length();
            int j;
            for(j=0;j<len;j++){
                if(nm[j]!='.')
                    break;
            }
            nm.erase(nm.begin(),nm.begin()+j);
            mp[nm]=i;
            on[j]=i;
            if(i!=1){
                G[on[j-1]].push_back(i);
            }
            name[i]=nm;
        }
        dfs(1);
        lcainit();
        int q;
        scanf("%d",&q);
        while(q--){
            char k;
            cin>>k;
            if(k=='L'){
                pf(1,0);
            }else if(k=='b'){
                string s;cin>>s;
                if(s==name[1]){
                    printf("1\n");
                }else
                    printf("%d\n",son[fa[mp[s]][0]]);
            }else{
                string s1,s2;
                cin>>s1>>s2;
                cout<<query(mp[s1],mp[s2])<<endl;
            }
        }

    }

    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值