PAT(甲)1021 Deepest Root (25)(详解)

1021 Deepest Root (测试点4、5报错的解决办法) (代码来自柳婼https://github.com/liuchuo/PAT/blob/master/AdvancedLevel_C%2B%2B/1021.%20Deepest%20Root%20(25)%20.cpp) 步骤: ①先以节点1为根节点进行DFS,找出最长的几个路径的终点temp[ ]并输入到set中,同时记录连通分量个数cnt (这里必须把终点全部输入到set中,如果只输入temp[... 阅读详情

1021 Deepest Root (25)(25 分)

题目描述:

A graph which is connected and acyclic can be considered a tree. The height of the tree depends on the selected root. Now you are supposed to find the root that results in a highest tree. Such a root is called the deepest root.


  • 输入格式:
    Each input file contains one test case. For each case, the first line contains a positive integer N (<=10000) which is the number of nodes, and hence the nodes are numbered from 1 to N. Then N-1 lines follow, each describes an edge by given the two adjacent nodes’ numbers.

  • 输出格式:
    For each test case, print each of the deepest roots in a line. If such a root is not unique, print them in increasing order of their numbers. In case that the given graph is not a tree, print “Error: K components” where K is the number of connected components in the graph.


题目大意:
题目就是要对一个树形结构,去寻找能够以某个结点以其为根,深度最大,如果有多个,则升序输出。解题思路参考自这里

解题方法:
采用dfs对图进行遍历,因为如果是连通图,dfs就能把所有结点都遍历完,而如果不是连通图,则需要至少2次以上,因此根据这个特性可以用来判断是否是树。同时,通过dfs,可以将最深的叶子结点找出来。这里需要注意一点,类似于(1-2-3)这样的树形结构,1和3都可以被称为root结点,因此,我们可以通过对第一遍dfs得到遍历结果中,随便取一个,反向dfs,最终把结果全部放入set中,把两次dfs的并集放入。


程序:

#include <stdio.h>
#include <set>
#include <vector>
#include <algorithm>
using namespace std;

int maxHight = 0;
set <int> s;    /* 保存深度最大的根结点集合(默认升序) */
vector <vector<int> > v;    /* 保存点与点之间的联系 */
vector <int> temp;  /* 保存深度最大点的向量 */
bool visit[10001];

void dfs(int node, int height)
{   /* 深度优先搜索遍历 */
    if (height > maxHight)  /* 如果当前结点的深度比最大深度还要大 */
    {
        maxHight = height;
        temp.clear();
        temp.push_back(node);
    }
    else if (height == maxHight)  /* 如果相同,将相同深度的结点放入temp */
        temp.push_back(node);
    visit[node] = true;     /* 置该点为已访问 */
    for (int i = 0; i < v[node].size(); i++)  /* 遍历v[node]的所有孩子 */
        if (visit[v[node][i]] == false)
            dfs(v[node][i], height+1);
}

int main(int argc, char const *argv[])
{
    int N, father, child, cnt = 0;
    scanf("%d", &N);
    fill(visit, visit+N+1, false);    /* 初始化visit */
    v.resize(N+1);  /* 调整vector的大小 */
    for (int i = 0; i < N-1; i++) /* 输入父节点和孩子节点之间的联系 */
    {   
        scanf("%d %d", &father, &child);
        v[father].push_back(child);
        v[child].push_back(father);
    }
    for (int i = 1; i <= N; i++)    /* 编号从1开始 */
        if (visit[i] == false)  /* 如果还未访问过 */
        {
            dfs(i, 1);
            if (i == 1)
                for (int j = 0; j < temp.size(); j++)   /* 把当前的叶子结点存入 */
                    s.insert(temp[j]);
            cnt++;  /* 每执行一次cnt++,用于判断是否是树 */
        }
    if (cnt >= 2)   /* 如果不是树 */
        printf("Error: %d components\n", cnt);
    else    /* 从叶子结点出发,去寻找最深的根结点 */
    {
        fill(visit, visit+N+1, false);    /* 重置visit */
        temp.clear();
        maxHight = 0;   
        dfs(temp[0], 1);    /* 因为树是连通的,所以从任意一个叶节点出发都能遍历到所有最深的父节点 */
        for (int j = 0; j < temp.size(); j++)
            s.insert(temp[j]);
        set <int>::iterator it;
        for (it = s.begin(); it != s.end(); it++)
            printf("%d\n", *it);
    }
    return 0;
}

如果对您有帮助,帮忙点个小拇指呗~

手把手教你用UNet实现轨道缺陷检测(附RSDDs数据集下载与完整代码) 本文详细介绍了如何使用UNet模型实现轨道缺陷检测,包括RSDDs数据集的下载与处理、UNet架构的改进与优化、训练策略设计以及模型部署的全流程。通过实战案例,展示了如何利用深度学习技术提升工业质检效率,特别适合从事工业缺陷检测和图像处理的开发者参考。 阅读详情

相关推荐

GB28181 2022 配置信令

基本参数视频参数选项视频参数属性配置巡航轨迹

ww506772362的专栏 1628

1021 Deepest Root (层序遍历解法)

首先任选一个结点进行,层序遍历求最后一层的结点,然后再将最后一层的结点再分别作为根结点,求他们对应的最后一层的结点,对这些所有的“最后一层的结点”去重排序就是答案了。题意:给定一个图,求以哪些结点为根,能够得到最深的树。

qq_63128300的博客 499

使用Label Studio标注车牌数据:YOLOv26模型训练指南

在计算机视觉领域,数据标注是模型训练的基础环节,直接影响最终模型的识别精度。Label Studio作为专业的标注工具,支持多种标注类型和格式导出,特别适合目标检测任务的数据准备。通过多边形标注技术,可以精确捕捉车牌的四个角点,生成旋转矩形框(OBB)标注数据,这种标注方式相比传统水平框能更好地处理倾斜车牌场景。标注完成后,数据可导出为YOLO格式,直接用于YOLOv26等先进目标检测模型的训练。在实际工程中,合理的标注策略和严格的质量控制能显著提升车牌识别模型在复杂场景下的鲁棒性,适用于智能交通、停车场管

chuozhi7142的博客 904

Pat【甲级】 1021 Deepest Root(附中文题目)

文章目录Deepest Root中文题目代码运行结果 Deepest Root A graph which is connected and acyclic can be considered a tree. The height of the tree depends on the selected root. Now you are supposed to find the root that results in a highest tree. Such a root is called the de

Jacksqh的博客 434

波束管理 Beam Management

Beam Management in 5G NR   波束管理,是5G NR中基于信道质量来动态选择每个UE和gNB之间以什么方向和频率的波束beam来进行通信。   背景   5G NR(New Radio)支持非常高的数据速率和更低的延迟。5G NR支持FR1和FR2频段,FR1为亚6 GHz(450至6000 MHz);FR2为毫米波段(24.25 GHz至52.6 GHz)。   由于毫米波频带使用非常高的频率,因此会导致传播损耗和其他损耗。为了补偿损失,在这种频率下定向通信是必不可少的。

最忆是江南的博客 5826

【HDR】曝光融合(Exposure Fusion)

在曝光融合(Exposure Fusion)算法问世之前,多曝光序列合成用于显示的HDR需要两个步骤,第一步是将多张不同曝光的低动态范围图像合成为HDR(例如Debevec提出的加权融合方法),通常HDR为12bit或者16bit;第二步是通过tonemapping对高动态范围HDR进行压缩以支持低动态范围显示设备(例如Durand提出的基于双边滤波的tonemapping算法),一般会压缩至8bit。

专注人工智能,主攻CV 1万+

【PAT(甲级)】1021 Deepest Root

【PAT(甲级)】1021 Deepest Root 思路分析,易错点分析

weixin_55202895的博客 406

DeepSeek+知网研学:三步搞定文献矩阵与对比分析(实战指南)

本文提供了一套高效实用的文献管理分析实战指南,详细介绍了如何结合DeepSeek大模型与知网研学平台,通过三个核心步骤快速完成文献梳理。该方法能自动提炼文献核心要素、生成结构化对比矩阵,并辅助发现研究空白,显著提升学术研究者的文献分析效率与深度。

weixin_29061509的博客 491

1021 Deepest Root

【代码】1021 Deepest Root。

CY_hhxx的博客 176

S4HANA Rebate Management(条件合同) -Single Step

S4HANA 条件合同管理(Condition Contract Management), 包括如何创建条件合同,场景应用

m0_50200150的博客 2307

用 SAP CDS View 和 Fiori Elements 快速打造 CRM Service Order 应用──十分钟跑通从数据模型到上线的全流程

通过CDS 视图 + Fiori Elements的组合,我们得以在极短时间内把传统 CRM Service Order 场景迁移到现代化的 Fiori 界面,而且代码量极小、可维护性高。若后续需要离线能力,可考虑将同一 OData 服务暴露给 Mobile Services,再配合 MDK 或 UI5 Mobile Toolkit;若要支持复杂审批流,则可以在同一 Launchpad 内嵌入 Process Visibility Dashboard,实现端到端可视化。

2007 年 ~ 2025 年,深耕 SAP 技术 18 年 390

【PAT A1021】Deepest Root

【代码】【PAT A1021】Deepest Root。

Honeyxuu的博客 180

手把手教您解决 AdGuard Home DNS重写功能失效的问题

AdGuard Home 设置了 DNS 重写,规则明明没问题,客户端却死活解析不到内网 IP?🤯 别急着重装!🚫 本文手把手教你如何解决软路由 DNS 解析“玄学”故障!🛠️🚀

XiaoqiangClub的博客 917

PAT甲1021 Deepest Root (25)(25 分)

#include &lt;stdio.h&gt; #include &lt;vector&gt; #include &lt;queue&gt; #include &lt;algorithm&gt; #include &lt;string.h&gt; using namespace std; vector&lt;int&gt; G[10010]; int height[10010]; bool v...

一点点 145

IMSL CNL 7.0.0 (x86-64)Crack

IMSL CNL 7.0.0 (x86-64)Crack intel的算法库破解文件,这是C++版本的破解文件

IDEA的使用指南

IDEA基于java的使用手册,可以最快速的上手,在使用中不断的丰富自己的使用技巧.

【PAT甲级】1021 Deepest Root (25分)

题目链接:1021Deepest Root(25分) #include <iostream> #include <vector> using namespace std; int DFS(int s, vector<int> &vis, vector<int> adj[], int k){ int maxDepth = 0; vis[s] = 1; for(int i = 0; i < adj[s].size...

五百零一 129

Data-Structures-and-Algorithm-Analysis-in-Java.pdf

作者: Weiss, Mark A. 出版社: Addison Wesley Data Structures and Algorithm Analysis in Java is an “advanced algorithms” book that fits between traditional CS2 and Algorithms Analysis courses. In the old ACM Curriculum Guidelines, this course was known as CS7. This text is for readers who want to learn good programming and algorithm analysis skills simultaneously so that they can develop such programs with the maximum amo...

【PAT 甲级】1021 Deepest Root

1021 Deepest Root

AmiaYukiAchami的博客 703

OV9734 DATASHEET,完整规格书

OV9734完整规格书,包括电气参数,尺寸规格,寄存器配置表等

PAT甲级 1021 Deepest Root

PAT甲级 1021 Deepest Root A graph which is connected and acyclic can be considered a tree. The height of the tree depends on the selected root. Now you are supposed to find the root that results in a hi...

旺崽的博客 2905

PAT 1021. Deepest Root

题目:http://pat.zju.edu.cn/contests/pat-a-practise/1021 题解: 给N个点,N-1条边,先判断是否为树,如果不是树输出有几块,如果是树,求当第i个节点为树根时,树的高度最大,符合条件的i是哪些。 先用并查集求所给的是否为树,然后通过两次dfs求最长的树径,第二次dfs时就可以记录符合条件的i,最后从小到大排序输出。 注:测试数据中有n=1的

Ted's 游乐园 2494

PAT-1021 Deepest Root

A graph which is connected and acyclic can be considered a tree. The height of the tree depends on the selected root. Now you are supposed to find the root that results in a highest tree. Such a root is called the deepest root. Input Specification: Each i

weixin_42100456的博客 171

PAT 甲级 1021(Deepest Root )

PAT 甲级 1021题目要求翻译代码思路 题目要求 A graph which is connected and acyclic can be considered a tree. The height of the tree depends on the selected root. Now you are supposed to find the root that results in a highest tree. Such a root is called the deepest root. I

qq_42865772的博客 471

[PAT-A 1021]Deepest Root

题目大意: 给出N个节点和N-1条边,问它们能否形成一颗N个节点的树,如果能,则从中选出节点作为树根,使得可以得到的树的高度最大 思路: 1.判断图是否联通,可以使用dfs或者并查集,每次读入一条边和两个端点,判断这两个端点是否属于相同的集合,即集合根节点是否相同,如果不同,将它们合并到一个集合中去,处理完之后判断产生集合的个数是否为1来判断给定的图是否联通。 2.当图联通时,由于题目保证只有N...

RegulusF的博客 209

PAT(Advanced)1021 Deepest Root (DFS)(并查集)(邻接表)C++实现

PAT(Advanced)甲级1021 Deepest Root (DFS)(并查集)(邻接表)C++实现 题目链接 PAT(Advanced)1021 题目大意 输入为无向无环图,任意顶点作为树根时能够达到树的最大深度的所有顶点(递增输出),同时,输入的图可能不连通,即无法构成一个树,此时输出该图树的数量(即根节点数目)。 AC代码 #include<bits/stdc++.h> u...

qq_44486439的博客 520

PAT甲级 - 1021 Deepest Root (25 分)

题目链接:PAT甲级 - 1021 Deepest Root (25 分) A graph which is connected and acyclic can be considered a tree. The hight of the tree depends on the selected root. Now you are supposed to find the root that r...

231

【PAT1021】Deepest Root 代码记录

1. 第一次使用邻接矩阵,由于题目中的节点数为N,但是边数为N-1,所以矩阵稀疏,使得测试时测试点3内存超限 23分/25分 代码记录如下: //这里是一个稀疏矩阵 #include <iostream> #include <vector> #include <algorithm> #include <queue> using namespac...

qq_41035731的博客 240

PAT甲级 1021 Deepest Root (25分)

1021 Deepest Root (25分) 题目链接:PAT A 1021 题目大意:给出一个数n代表节点的个数(节点编号1-n),给出各个边的连接方式,问哪个节点为根节点时,这棵树最高,如果有多个这样的结点,则按升序的方式输出。如果给出的图不是一棵树,那么输出它有几个部分组成。 思路分析:注意题中输入第一行是节点数目,接下来的n-1行是边。由于连通,节点数为n,边为n-1的图一定是一棵树...

weixin_43684054的博客 307

[PAT-Advanced] A1021 Deepest Root (25)

文章目录A1021 Deepest Root (25point(s))Input Specification:Output Specification:Sample Input 1:Sample Output 1:Sample Input 2:Sample Output 2:CodeAnalysis A1021 Deepest Root (25point(s)) A graph which is ...

Duke 335
上一篇: PAT(甲) 1020 Tree Traversals (25)(详解)
下一篇: PAT(甲)1022 Digital Library (30)(30 分)
大鲨鱼冲鸭
博客等级 码龄9年 249粉丝 185原创
评论 2
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值