HDU 4409 - Family Name List (LCA)

HyperMesh实战:5种复杂载荷场景下的field映射技巧(附TCL脚本) 本文详细介绍了HyperMesh在5种复杂载荷场景下的field映射技巧,包括轴承载荷的圆柱坐标系映射、粉末压力中的切向力处理、实验数据到有限元模型的场映射、多工况载荷的组合映射以及局部细化网格的边界条件传递。通过实战案例和TCL脚本,帮助工程师高效解决有限元分析中的载荷映射难题,提升工作效率。 阅读详情

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

PointMLP实战:5分钟用PyTorch搭建点云分类模型(附GitHub代码) 本文提供了基于ICLR 2022论文PointMLP的点云分类模型实战教程。通过PyTorch框架,详细介绍了从环境搭建、数据预处理、模型架构解析到训练推理的全流程,并分享了性能调优技巧。该模型利用残差MLP模块构建,结构清晰高效,适合开发者快速上手并应用于实际点云处理项目。 阅读详情

相关推荐

计算机网络实验六 RIP 配置实验

Man! What can I say?

AClerk的博客 1593

hdu 4409 Family Name List(LCA&有坑点)

Family Name List Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 882    Accepted Submission(s): 271 Problem Description Kong belong

记录成长的点点滴滴. . . . . 1291

从零构建FOC:手把手教你实现无刷电机磁场定向控制

本文是一份详尽的FOC(磁场定向控制)实战教程,指导开发者从零开始构建无刷电机驱动系统。文章深入解析了FOC的核心算法,包括坐标变换(Clarke/Park)、SVPWM调制和双环PI控制,并对比了有感与无感方案的实现差异。通过分步调试指南,帮助读者亲手实现稳定、高效的无刷电机控制,彻底掌握其底层原理。

h3i4j的博客 514

ACM网络赛金华赛区的一道关于树的题:Family Name List

 三场网络赛终于告一段落了!唉,实力太弱了!跟北大、清华这些家伙差距太远了,比"我在你身边你却不知道我爱你"的距离还要远!      这道题没有来得及提交,自己下来写完的,把样例过了!留在博客上作个记念吧!记念我大学生涯失败的网络赛!     题目是这样的(英文原版):   Description   Kong belongs to a huge family. Recently he g...

weixin_34357436的博客 211

避坑指南:PyCINRAD处理CINRAD雷达数据时遇到的5个典型报错及解决方案

本文针对使用PyCINRAD库处理CINRAD雷达基数据时常见的5个典型报错,提供了详细的解决方案。内容涵盖环境配置、数据读取、PPI可视化及性能优化等核心环节,重点解决了版本兼容性、数据异常处理和中文乱码等实际问题,帮助用户高效处理雷达基数据并生成准确的PPI图像。

cc789的博客 1068

VESC电调高级调参指南:电机校准与性能优化实战解析

本文详细解析了VESC电调的高级调参方法,包括电机校准与性能优化实战技巧。从基础参数设置到高级机械参数优化,再到动态性能调优,提供了全面的调参指南。特别针对电机类型、功率设置、电池参数配置等关键环节给出实用建议,帮助用户充分发挥VESC电调的性能潜力。

weixin_30772105的博客 196

Family Name List HDU - 4409

http://acm.hdu.edu.cn/showproblem.php?pid=4409 由G2 &lt;= G1 +1得 题目给的是一个DFS序列 然后单调栈找出每个节点的父节点 然后三种操作都很裸 有个坑点 题目中uv的lca不能等于uv 题目说的很隐晦 可以通过下面这句话猜一下 "Since Mr. X has no ancestor in the list, so it's gu...

sunyutian1998的博客 294

学Simulink--基于电力电子变换器的建模与仿真实例:LLC谐振变换器在高频AC-DC转换中的建模

本文详细介绍了使用Simulink+Simscape Electrical对LLC谐振变换器进行建模与仿真的完整流程。LLC谐振变换器因其高效率、低噪声的特点,广泛应用于高频AC-DC转换场景。文章从背景原理出发,逐步讲解系统结构设计、建模步骤(包括AC源、整流器、LLC变换器、负载等模块搭建)、控制策略实现(固定频率/变频/PID控制)和仿真设置。通过分析仿真结果的关键指标,提出参数整定和算法优化建议。该建模方法为电力电子设计人员提供了实用的仿真工具,有助于优化LLC变换器性能,缩短开发周期。

xiaoheshang_123的博客 298

Family Name List (HDU 4409)

周赛的题目,废了好大劲才看懂的,STL不够熟悉,需要近一步训练啊 点击打开链接http://blog.sina.com.cn/s/blog_79aa283901018w01.html 转载地址 #include #include #include #include #include #include #include using namespace std; int

du_shujun的专栏 427

这种绑定设备的文件拷贝出来了,怎么放到一台新的P100上使用

🏆 本文收录于《全栈Bug调优(实战版)》专栏,致力于分享我在项目实战过程中遇到的各类Bug及其原因,并提供切实有效的解决方案。无论你是初学者还是经验丰富的开发者,本文将为你指引出一条更高效的Bug修复之路,助你早日登顶,迈向财富自由的梦想🚀!同时,欢迎大家关注、收藏、订阅本专栏,更多精彩内容正在持续更新中。让我们一起进步,Up!Up!Up!    备注: 部分问题/难题源自互联网,经过精心筛选和整理,结合数位十多年大厂实战经验资深大佬经验总结所得,数条可行方案供所需之人参考。

**My Coding Family** 1136

给跨代引用记一本账:Java GC 的分配路径、卡表写屏障与收集器版图快速回顾

本文把 Java GC 的零散名词串成一条推理链路:从 TLAB 指针碰撞分配、逃逸分析,到 Eden、Survivor 与晋升的四条路,重点拆解卡表与写屏障如何为老年代指向新生代的引用记账,及 Minor GC 如何只扫脏卡。基于 JDK 8 实测:分散写使 Young GC 停顿涨 10~15 倍,条件卡标记消除卡表伪共享,计数循环让 System.gc 等待 2.7 秒,Survivor 过小使晋升量增加约 400 倍。文末梳理 JDK 8 到 27 的收集器演进、选型路径与排查顺序。

曾经“等你生日那天”都遥远得像未来,如今却可欢愉的挥手说“下个十年见” 17万+

HDU 4409 Family Name List(LCA)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4409 题意:给定一个家谱。有三种询问: (1)L重新打印家谱,同一代的人按照升序; (2)b name  输出name的亲兄弟有多少个? (3)c name1 name2输出name1和name2 的最近公共祖先。 注意(3):如果最近公共祖先是name1或者name2,那么要输出其父亲。(...

weixin_33854644的博客 174

代谢组数据分析(零):从质谱样本制备到MaxQuant搜库(蛋白质组和代谢组)

本文详细介绍了代谢组数据分析的过程,从LC-MS/MS技术的样本制备、分离、质谱鉴定到MaxQuant软件的使用。LC-MS/MS包括样本制备、液相色谱分离、质谱上机和鉴定等步骤。MaxQuant在蛋白质定性和定量中扮演关键角色,通过搜库和数据处理提供精准结果。此外,文章还涉及了数据采集模式、质谱图谱解析、数据库和软件选择以及结果评估等方面

专注生信领域 8590

HDU-4409 Family Name List LCA求解,TC+DFS || tarjan

题目:http://acm.hdu.edu.cn/showproblem.php?pid=4409        题意:就是要你求LCA。        这个题目是很典型的LCA问题,常见的有在线的TC+RMQ算法,离线的targan算法。算法的描述自己写太麻烦了,摘抄别人的(—,—): 在线算法DFS+TC描述(思想是:将树看成一个无向图,u和v的公共祖先一定在u与v之间的最短路径上):

岁月流风、、 1152

假人有限元模型

假人模型,车辆碰撞假人有限元模型,适合做乘员安全的研究者利用

jdk-8u144-linux-x64.tar.gz.

该资源为搭建Linux系统Java环境用,资源的后缀为tar.gz格式的压缩包

hdu 4409 Family Name List LCA +stl

http://acm.hdu.edu.cn/showproblem.php?pid=4409 赛后才过只能说悲剧了,知道思路,stl不熟悉,所以导致写的很慢....占据了很多时间,手速+代码准确度。。哎。。。 题意: 给你一个家谱,n个人的姓名,姓名前边的点代表了他是第几代人。每个人的祖先肯定在他之前出现。有三种操作: L:输出这个家族的序列,不是按层数,而是递归的输出,还要保证字典序升序...

weixin_30768661的博客 79

C# WinForm 基于DirectShow.dll编写的程序,可边预览边保存视频,音频和图片

基于微软的DirectShow.dll的二次开发1.自动识别连接的摄像头跟麦克风且可以对其进行选择(默认选中第一个)2.可录制wmv和avi两种格式的视频并且都带有音频,avi可以录制的分辨率和帧数进行选择,wmv默认进行了淸晰度优化如不喜欢可去除代码中有相关提示3.可在预览点开之后进行拍照,也可边录制视频边拍照4.添加了视频和音频的压缩功能(酌情使用)这次二次开发发现C#对DirectShow的相关信息太少,这次做完也不敢藏私共享出来,还望大家指正

HDU 4409 Family Name List --乱搞、LCA

题意: 给出一些名字,名字间有父子关系,有三种操作: 1.按祖先到后代,兄弟间按字典序由小到大排序,然后输出 2.求某个节点的兄弟节点有多少个,包括自己(注意,根节点的兄弟节点是1) 3.求节点a和b的公共祖先 (注意:如果公共祖先是a或b,必须要输出其父亲,与传统的LCA可以是自己不同) 解法: 先把整棵树整理出来,son[u]表示u的儿子个数,用来求兄弟个数, fa[u]表示父亲,Gs

whatbeg 613

精雕软件(JDpaint) 5.5 绿色版

精雕软件(JDpaint) 5.5 绿色版精雕软件(JDpaint) 5.5 绿色版精雕软件(JDpaint) 5.5 绿色版

HDU 4409 - Family Name List(模拟树+lca)

题目: http://acm.hdu.edu.cn/showproblem.php?pid=4409 思路: 模拟输出树,暴力求解LCA AC. #include #include #include #include #include #include #include using namespace std; const int maxn = 30005; char s

Halo 536

HDU 4409 Family Name List ( 模拟 + LCA)

  昨晚脑子发涨,代码写的跟坨屎的,到底没过,今天早晨来看了看,查了几个bug过了。。。 纯模拟+LCA找最近公共祖先,需要注意的几个地方: 1、出处顺序,可以用dfs输出,不过每个dfs里面要开一个vector&lt;&gt;,然后排序。 2、b操作可能出现询问Mr.X,Mr.X的brother个数为1 3、c操作时LCA不能是他两个本身。可以开一个pre,如果是他俩中的一个,再往前找一...

weixin_34177064的博客 125

Hdu 4409 Family Name List (LCA 家谱 STL 2012金华网赛)

题意:给出一个家谱,处理三种操作。 (1)L重新打印家谱,同一代的人按照升序; (2)b name  输出name的亲兄弟有多少个? (3)c name1 name2输出name1和name2 的最近公共祖先。 注意(3):如果最近公共祖先是name1或者name2,那么要输出其父亲。(因为自己不能是自己的祖先。。) #pragma warning(disable:4786) #in

whyorwhnt的专栏 1168

hdu 4409 Family Name List

/* Three operations L : the same as input, correspond to Preorder traversal. b x : correspond to ask how many sons does x's father have. c x y : LCA(x, y), there is a trick, if LCA(x, y)

Dwylkz 879

HDU 4409 Family Name List 简单树操作

题意:按照题目描述给一棵树(家族谱,节点n           c name name输出两个人的lca是多少(lca不能是name本身)。 题解:vector存储节点数这样很容易排序,然后保存下L的序列,sum[ i ]表示当前 i 节点的直接儿子节点的个数,rmq处理出lca。 Sure原创,转载请注明出处。 #pragma comment (linker , "/ST

Uhh, call me zacky NOW!!! 907

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 t

Oops的博客 276

HDU 4409 大模拟 + LCA

HDU 4409 题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=4409 题意: 大模拟看题很关键。 思路: 大模拟+LCA。 坑点 1)自己不能是自己的祖先。 2)对于Mr.X,他的兄弟数为1。 源码: //#pragma comment(linker, “/STACK:1024000000,1024000000”)inclu

beihai2013 431
上一篇: HDU 3078 - Network (LCA)【tarjan离线/DFS倍增】
下一篇: POJ 1222 - EXTENDED LIGHTS OUT (高斯消元)
Mr_Treeeee
博客等级 码龄10年 38粉丝 551原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值