news 2026/1/18 12:09:59

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/1/8 18:07:55

基于springboot + vue宠物商城平台网站系统

宠物商城平台 目录 基于springboot vue宠物商城平台系统 一、前言 二、系统功能演示 三、技术选型 四、其他项目参考 五、代码参考 六、测试参考 七、最新计算机毕设选题推荐 八、源码获取&#xff1a; 基于springboot vue宠物商城平台系统 一、前言 博主介绍&…

作者头像 李华
网站建设 2026/1/17 18:36:35

‌自动化测试脚本维护的7个技巧

在敏捷开发和DevOps盛行的时代&#xff0c;自动化测试脚本已成为软件质量保障的核心支柱。然而&#xff0c;脚本维护的挑战常被忽视——脆弱的脚本会导致测试失败、资源浪费和发布延迟。本文针对软件测试从业者&#xff0c;分享7个实用技巧&#xff0c;涵盖从代码优化到团队协作…

作者头像 李华
网站建设 2026/1/8 20:32:36

持续测试(CT)在DevOps中的角色

DevOps时代的测试变革 在当今快速迭代的软件开发环境中&#xff0c;DevOps已从流行词演变为行业标准&#xff0c;它通过整合开发&#xff08;Dev&#xff09;和运维&#xff08;Ops&#xff09;&#xff0c;实现了软件交付的自动化和加速。然而&#xff0c;DevOps的成功高度依…

作者头像 李华
网站建设 2026/1/9 0:34:44

Streamlit整合案例:构建交互式数学问题求解演示系统

构建交互式数学问题求解演示系统&#xff1a;VibeThinker-1.5B 与 Streamlit 的轻量级实践 在人工智能加速渗透教育、科研和工程领域的今天&#xff0c;如何让前沿模型真正“可用”而非仅“可研”&#xff0c;成为开发者与研究者共同面对的课题。尤其在数学推理、算法设计这类高…

作者头像 李华
网站建设 2026/1/9 9:00:12

基于springboot申家沟村务管理系统

文章目录详细视频演示项目介绍技术介绍功能介绍核心代码系统效果图详细视频演示 文章底部名片&#xff0c;获取项目的完整演示视频&#xff0c;免费解答技术疑问 项目介绍 申家沟村务管理系统是一款以SpringBoot框架为核心构建的数字化治理工具&#xff0c;旨在通过信息化手段…

作者头像 李华
网站建设 2026/1/12 20:42:11

Docker日志监控已过时?90%企业正在转向Falco实时防护

第一章&#xff1a;Docker日志监控已过时&#xff1f;重新定义容器安全边界随着容器化技术的深度应用&#xff0c;传统仅依赖 Docker 日志采集的安全监控模式已显乏力。攻击者可通过删除容器、伪造日志或利用特权容器逃逸等手段绕过日志审计&#xff0c;使得基于日志的威胁检测…

作者头像 李华