news 2026/4/18 20:01:55

UVa 115 Climbing Trees

作者头像

张小明

前端开发工程师

1.2k 24
文章封面图
UVa 115 Climbing Trees

题目分析

本题要求根据输入的父子关系对(child-parent pairs\texttt{child-parent pairs}child-parent pairs)构建一个家族树,然后对一系列查询对(query pairs\texttt{query pairs}query pairs)判断两人之间的亲属关系,并按照特定格式输出。关系包括直接父子、祖孙、兄弟姐妹、堂表亲等。

关键定义

  • pppqqqkkk-descendent\texttt{descendent}descendentkkk-ancestor\texttt{ancestor}ancestor)当且仅当存在一条长度为kkk的父子链。
  • 关系分类:
    1. child / parent\texttt{child / parent}child / parent:直接父子(k=0k=0k=0),grand child/parent\texttt{grand child/parent}grand child/parentk=1k=1k=1),great grand child/parent\texttt{great grand child/parent}great grand child/parentk≥2k \ge 2k2,前缀great\texttt{great}great重复k−1k-1k1次)。
    2. sibling\texttt{sibling}sibling:共享同一父母(即000代表000removed\texttt{removed}removed000-th cousin\texttt{th cousin}th cousin)。
    3. cousin\texttt{cousin}cousin:设pppqqq的最小公共祖先为rrrppprrrmmm-descendent\texttt{descendent}descendentqqqrrrnnn-descendent\texttt{descendent}descendent,则:
      • 他们是kkk-th cousin\texttt{th cousin}th cousin,其中k=min⁡(m,n)k = \min(m, n)k=min(m,n)
      • removed\texttt{removed}removedjjj次,其中j=∣m−n∣j = |m - n|j=mn
    4. no relation\texttt{no relation}no relation:不在同一家族树中。

输入限制

  • 最多300300300个不同名字,名字长度不超过303030
  • 最多100100100个查询对。
  • 无循环关系。

输出要求

  • 按格式输出关系,若removed\texttt{removed}removed次数为000则不输出removed 0
  • 数字后不加序数后缀(如st,nd,rd,th)。

解题思路

本题虽然名为“树”,但不需要显式建树,只需维护每个节点的父节点映射即可。由于名字是字符串,我们使用map<string, int>将名字映射为整数编号,便于处理。

步骤

  1. 读取父子关系

    • 每行读入child\texttt{child}childparent\texttt{parent}parent,直到遇到no.child no.parent
    • 为新名字分配编号,记录parent[child] = parent
  2. 处理查询
    对每个查询对(p,q)(p, q)(p,q)

    • 检查是否存在:若任一名字不在映射中,输出no relation
    • 检查是否在同一棵树:分别找到pppqqq的根祖先(不断找父节点直到DUMMY),若不同则输出no relation
    • 判断直接关系
      • pppqqq的祖先:根据深度输出parent / grand parent / great ... grand parent\texttt{parent / grand parent / great ... grand parent}parent / grand parent / great ... grand parent
      • pppqqq的后代:根据深度输出child / grand child / great ... grand child\texttt{child / grand child / great ... grand child}child / grand child / great ... grand child
      • pppqqq是兄弟姐妹(同一父母):输出sibling
    • 判断堂表亲关系
      • 先计算pppqqq到根节点的深度(mmmnnn)。
      • pppqqq提升到同一深度,然后同时向上找,直到找到同一父母。
      • 计算k=min⁡(m′,n′)k = \min(m', n')k=min(m,n)(提升后剩余深度),j=∣m−n∣j = |m - n|j=mn(深度差)。
      • 输出k cousin(若j=0j=0j=0)或k cousin removed j

注意:题目要求removed\texttt{removed}removed000时不输出removed 0,且数字后不加序数词。

代码

// Climbing Trees// UVa ID: 115// Verdict: Accepted// Submission Date: 2011-11-27// UVa Run Time: 0.008s//// 版权所有(C)2011,邱秋。metaphysis # yeah dot net//// [解题方法]// 模拟题。由于只需要保存父子关系,不一定要构建树。虽然方法是直接的,但是有点繁琐。#include<bits/stdc++.h>usingnamespacestd;#defineMAXN310#defineDUMMY(-1)intparent[MAXN];map<string,int>genealogy;boolisAncestor(string fName,string sName){intnFirst=genealogy[fName];intnSecond=genealogy[sName];intdepth=0;boolfound=false;while(parent[nSecond]!=DUMMY){if(parent[nSecond]==nFirst){found=true;break;}nSecond=parent[nSecond];depth++;}if(found){if(depth==0)cout<<"parent\n";elseif(depth==1)cout<<"grand parent\n";else{for(inti=1;i<depth;i++)cout<<"great ";cout<<"grand parent\n";}returntrue;}returnfalse;}boolisDescendent(string fName,string sName){intnFirst=genealogy[fName];intnSecond=genealogy[sName];intdepth=0;boolfound=false;while(parent[nFirst]!=DUMMY){if(parent[nFirst]==nSecond){found=true;break;}nFirst=parent[nFirst];depth++;}if(found){if(depth==0)cout<<"child\n";elseif(depth==1)cout<<"grand child\n";else{for(inti=1;i<depth;i++)cout<<"great ";cout<<"grand child\n";}returntrue;}returnfalse;}boolisInTree(string fName,string sName){if(genealogy.find(fName)==genealogy.end()||genealogy.find(sName)==genealogy.end()){cout<<"no relation\n";returnfalse;}returntrue;}boolisInSameTree(string fName,string sName){intnFirst=genealogy[fName];intnSecond=genealogy[sName];while(parent[nFirst]!=DUMMY)nFirst=parent[nFirst];while(parent[nSecond]!=DUMMY)nSecond=parent[nSecond];if(nFirst!=nSecond){cout<<"no relation\n";returnfalse;}returntrue;}boolisSibling(string fName,string sName){intnFirst=genealogy[fName];intnSecond=genealogy[sName];if(parent[nFirst]!=DUMMY&&parent[nSecond]!=DUMMY&&parent[nFirst]==parent[nSecond]){cout<<"sibling\n";returntrue;}returnfalse;}boolisCousin(string fName,string sName){intnFirst=genealogy[fName];intnSecond=genealogy[sName];intn=0,m=0;while(parent[nFirst]!=DUMMY){nFirst=parent[nFirst];n++;}while(parent[nSecond]!=DUMMY){nSecond=parent[nSecond];m++;}nFirst=genealogy[fName];nSecond=genealogy[sName];intbackN=n,backM=m;if(n>m){while(n>m){nFirst=parent[nFirst];n--;}while(parent[nFirst]!=parent[nSecond]){nFirst=parent[nFirst];nSecond=parent[nSecond];n--;}backN-=n;backM-=n;cout<<backM<<" cousin removed "<<(backN-backM)<<"\n";}elseif(m>n){while(m>n){nSecond=parent[nSecond];m--;}while(parent[nFirst]!=parent[nSecond]){nFirst=parent[nFirst];nSecond=parent[nSecond];m--;}backN-=m;backM-=m;cout<<backN<<" cousin removed "<<(backM-backN)<<"\n";}else{while(parent[nFirst]!=parent[nSecond]){nFirst=parent[nFirst];nSecond=parent[nSecond];n--;m--;}cout<<(backN-n)<<" cousin\n";}returntrue;}intmain(intargc,charconst*argv[]){intnNames=0;string allNames[MAXN];string childName,parentName;for(inti=0;i<MAXN;i++)parent[i]=DUMMY;while(cin>>childName>>parentName){if(childName=="no.child"&&parentName=="no.parent")break;if(genealogy.find(childName)==genealogy.end()){genealogy[childName]=nNames;allNames[nNames++]=childName;}if(genealogy.find(parentName)==genealogy.end()){genealogy[parentName]=nNames;allNames[nNames++]=parentName;}parent[genealogy[childName]]=genealogy[parentName];}while(cin>>childName>>parentName){if(!isInTree(childName,parentName))continue;if(!isInSameTree(childName,parentName))continue;if(isAncestor(childName,parentName))continue;if(isDescendent(childName,parentName))continue;if(isSibling(childName,parentName))continue;if(isCousin(childName,parentName))continue;}return0;}

复杂度分析

  • 每次查询需要向上遍历祖先链,最坏深度为O(N)O(N)O(N)NNN为节点数(≤300\le 300300)。
  • 总查询次数≤100\le 100100,因此总时间复杂度为O(N⋅Q)O(N \cdot Q)O(NQ),完全可行。

本题关键在于理清亲属关系的定义,并仔细实现各种情况的判断与输出格式。

版权声明: 本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!
网站建设 2026/4/18 15:31:50

Chain-of-Thought提示法适配:引导VibeThinker分步推理技巧

Chain-of-Thought提示法适配&#xff1a;引导VibeThinker分步推理技巧 在AI大模型参数规模不断膨胀的今天&#xff0c;一个反向趋势正在悄然兴起——我们是否真的需要千亿参数才能解决复杂的逻辑问题&#xff1f;微博开源的 VibeThinker-1.5B-APP 给出了令人意外的答案&#xf…

作者头像 李华
网站建设 2026/4/16 18:07:45

容器环境突现未知进程,如何用Falco秒级发现并响应?

第一章&#xff1a;容器环境突现未知进程&#xff0c;如何用Falco秒级发现并响应&#xff1f; 在现代云原生架构中&#xff0c;容器运行时突发未知进程是常见的安全威胁之一。攻击者可能通过镜像漏洞或配置错误植入恶意进程&#xff0c;进而横向移动或窃取数据。Falco 作为一款…

作者头像 李华
网站建设 2026/4/15 12:07:25

基于RCGELAN-YOLOv11的路面损伤检测算法

导读&#xff1a; 本文针对路面损伤检测任务中传统方法效率低、易受环境干扰的痛点&#xff0c;提出了一种基于RCGELAN-YOLOv11的改进算法。在YOLOv11算法的基础上改进网络结构&#xff0c;通过设计RC-G-ELAN模块替代YOLOv11中的C3k2模块&#xff0c;实现了检测精度与计算效率…

作者头像 李华
网站建设 2026/4/18 13:26:41

小参数模型也能做大文章:VibeThinker训练策略揭秘

小参数模型也能做大文章&#xff1a;VibeThinker训练策略揭秘 在AI模型“军备竞赛”愈演愈烈的今天&#xff0c;千亿参数、万卡集群似乎成了通往智能高峰的唯一门票。然而&#xff0c;当大多数团队还在为算力门槛焦头烂额时&#xff0c;一款仅15亿参数的小模型却悄然在数学与编…

作者头像 李华
网站建设 2026/4/17 22:21:36

运维必看:掌握这6种健康检查模式,彻底告别手动排查

第一章&#xff1a;Docker健康检查的核心价值与演进 在容器化应用广泛普及的今天&#xff0c;服务的稳定性与可观测性成为运维关注的重点。传统的容器启动成功并不意味着应用已准备好对外提供服务&#xff0c;Docker健康检查机制正是为解决这一问题而生。它通过周期性探测容器内…

作者头像 李华