1021. Deepest Root (25)-PAT

PAT甲级真题 1021 Deepest Root (25分) C++实现(两次DFS遍历,注意测试点3内存超限错误,需用邻接表存储图) 题目 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 r... 阅读详情

1021. Deepest Root (25)

时间限制
1500 ms
内存限制
32000 kB
代码长度限制
16000 B
判题程序
Standard
作者
CHEN, Yue

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

Output Specification:

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.

Sample Input 1:
5
1 2
1 3
1 4
2 5
Sample Output 1:
3
4
5
Sample Input 2:
5
1 3
1 4
2 5
3 4
Sample Output 2:
Error: 2 components
推荐指数:※※
来源:http://pat.zju.edu.cn/contests/pat-a-practise/1021
本题直接用dfs就可以了(本以为要加一些剪枝才能通过。。。)
#include<iostream>
#include<string.h>
#include<vector>
using namespace std;
#define N 10001
vector<int> edge[N];
int deepest[N];
int visited[N];
int n;
int  dfs(int now_node){
	if(visited[now_node]==1)
		return 0;
	visited[now_node]=1;
	if(edge[now_node].size()==0)
		return 1;
	int i,maxdeep=0,tmp;
	for(i=0;i<edge[now_node].size();i++){//find a deepest from this node
		if(visited[edge[now_node][i]]==0){
			tmp=dfs(edge[now_node][i]);
			maxdeep=maxdeep>tmp?maxdeep:tmp;
		}
	}
	return maxdeep+1;
}
int main()
{
	int i,j;
	cin>>n;
	for(i=1;i<=n-1;i++){
		int v1,v2;
		cin>>v1>>v2;
		edge[v1].push_back(v2);
		edge[v2].push_back(v1);
	}
	memset(deepest,0,sizeof(deepest));
	int max_deep=0;
	int flag=0;
	for(i=1;i<=n;i++){
		if(0==flag)
			memset(visited,0,sizeof(visited));
		deepest[i]=dfs(i);
		max_deep=max_deep>deepest[i]?max_deep:deepest[i];
		for(j=i;j<=n;j++){//check if it is a connected graph
			if(visited[j]==0){
				flag++;
				i=j-1;
				break;
			}
		}
	}
	if(flag>0){
		cout<<"Error: "<<flag+1<<" components"<<endl;
	}
	else{
		for(i=1;i<=n;i++){
			if(deepest[i]==max_deep)
				cout<<i<<endl;
		}
	}
	return 0;
}



 
【AI蛋白预测实战】零门槛在Colab上运行AlphaFold2全流程解析 本文为零基础用户提供了在谷歌Colab平台上运行AlphaFold2进行蛋白质结构预测的完整实战指南。通过ColabFold项目,用户无需本地高性能硬件,即可免费利用云端GPU完成从序列输入到3D结构生成和结果可视化的全流程,大幅降低了AI蛋白预测的技术门槛。 阅读详情

相关推荐

LED背光驱动电路解析:从MP3394芯片到液晶屏灯管控制

本文深入解析了以MP3394芯片为核心的LED背光驱动电路工作原理。从升压电路的能量转换到多路LED的均流控制,详细阐述了其作为液晶屏背光“大脑”的关键作用,并提供了黑屏、闪屏、亮度不均等常见故障的实战检修思路与排查方法。

agile9scrum的博客 822

pat 甲级 1021 Deepest Root

中文意思:一个无环连通图可以被视作一个树。树的高度取决于所选取的根节点。现在,你要找到可以使得树的高度最大的根节点。它被称为最深的根。

weixin_50671259的博客 1022

Visual Studio 2015 插件开发 从入门到放弃(一)

必需知识:C#和Visual Studio (1)安装 Visual Studio 2015 的时候选择 Visual Studio 扩展性工具(Visual Studio Extensibility Tools)。对于已经安装好 Visual Studio 2015 的小伙伴,可以找VS2015的安装包,或者在程序与功能中找VS2015双击,在VS2015界面中选择 修改,再选择

AncientPeople的博客 2万+

【PAT(甲级)】1021 Deepest Root

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

weixin_55202895的博客 406

logisim实验六:MIPS运算器设计

实验目的 学生理解算术逻辑运算单元(ALU)的基本构成,掌握 Logisim 中各种运算组件的使用方法,熟悉多路选择器的使用,能利用前述实验完成的 32 位加法器、 Logisim 中的运算组件构造指定规格的 ALU 单元。 设计要求 利用前面实验封装好的 32 位加法器以及 logisim 平台中现有运算部件构建一个 32 位算术逻辑运算单元(禁用 Logisim 系统自带的加法器,减法器),可支持算术加、减、乘、除,逻辑与、或、非、异或运算、逻辑左移、逻辑右移、算术右移运算,支持常用程序状态标志(有符号

gyp0205的博客 2万+

Python自动化生成HyperMesh Tcl命令流:从几何建模到有限元分析

本文详细介绍了如何利用Python自动化生成HyperMesh的Tcl命令流,实现从几何建模到有限元分析的全流程二次开发。通过封装常用操作函数,工程师可以高效完成参数化建模、自动网格划分及求解器文件输出,显著提升CAE前处理效率与标准化水平。

weixin_29041767的博客 424

1021 Deepest Root (25 分) 图 DFS 求最长路径

1021Deepest Root(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 ...

树叶子的博客 422

3D打印必备:如何用Blender将NWD文件转成STL(附精度调整技巧)

本文详细介绍了如何使用Blender将NWD文件转换为STL格式,特别针对3D打印需求提供了精度调整技巧。从环境准备到分步转换流程,再到3D打印专属优化策略,帮助设计师解决破面、尺寸偏差等常见问题,实现高效、精准的格式转换。

week9的博客 272

1021 Deepest Root(dfs,图的联通子集个数,树的深度)

1021 Deepest Root

老顽固也可爱 371

医疗AI Agent实战:从零搭建一个能看病的智能助手(附完整代码)

本文详细介绍了如何从零搭建一个医疗AI Agent智能助手,涵盖核心架构设计、症状推理引擎开发、安全与合规实现以及系统集成等关键环节。通过完整的代码示例和实战案例,帮助开发者快速构建具备医疗咨询能力的智能Agent,提升医疗服务的效率和质量。

ss78901的博客 467

矩阵管理:一人100账号霸屏小红书,保姆级教程经验技巧

同时,如果你的账号表现很正常,很OK,它也增加账号权重。手机环境:一机一卡一号是基本操作,如果你觉得设备成本高,你就要知道WIN的云真机,一键新机,一键随机设备参数,一键还原,一键安装应用,自带批量发布笔记,私信聚合,自带回复私信,一键养号等等,一人批量注册100个小红书账号真的很简单!从流量逻辑来分析,大家有没有发现,从账号粉丝数来看,小红书很难出现类似抖音上超千万粉丝量级的账号,几十万粉丝的博主已经是头部账号了,原因在于,抖音的流量分发机制是80%的流量集中在 20% 的头部账号而。

FDF432DF的博客 5145

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

基于YOLOv8的农作物病虫害检测系统设计与实现

国内如中国农业大学的科研团队采用改进的Faster R-CNN模型对小麦病害进行检测,取得了较好的效果。农作物病虫害是影响农业生产的重要因素之一,每年因病虫害导致的农作物减产高达20%-40%。YOLO(You Only Look Once)系列算法作为目标检测领域的代表性模型,以其高效、准确的特点在众多领域得到广泛应用。YOLOv8作为该系列的最新版本,在精度和速度上都有显著提升,非常适合农作物病虫害检测这类需要实时处理的应用场景。然而,现有研究仍存在一些不足:一是模型复杂度高,难以部署到移动设备;

YOLO 794

【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

精雕软件(JDpaint) 5.5 绿色版

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

迈瑞BS系列全自动生化分析仪LIS协议接口手册(内含HL7和ASTM)

迈瑞BS系列全自动生化分析仪LIS协议接口手册,内含HL7和ASTM)

PAT甲级 1021 Deepest Root (25分)

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

weixin_43684054的博客 307

计算机控制技术及应用

《计算机控制技术及应用》通过总结作者的科研成果和吸收国内外的先进理论、方法和技术,以计算机控制系统的网络化、开放化、智能化和集成化发展趋势为主线索,主要介绍计算机控制系统的基本概念和计算机控制系统的典型形式,计算机控制系统的数学描述与基本分析方法,以及数字PID控制器、最少拍控制系统、达林算法、状态变量设计法等设计方法及其各种改进算法。重点突出计算机控制系统输入输出通道接口的设计方法和典型控制系统的设计案例与实现方法等。

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 results in a highest tree. Such a root i...

fried123123的博客 1005

PAT真题练习(甲级)1021 Deepest Root (25 分)

PAT真题练习(甲级)1021 Deepest Root (25 分) 原题网址: https://pintia.cn/problem-sets/994805342720868352/problems/994805482919673856 A graph which is connected and acyclic can be considered a tree. The height of...

Be Quick & Clever & Lucky 396

[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 1021. Deepest Root (25)

1021. Deepest Root (25) 时间限制 1500 ms 内存限制 32000 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue A graph which is connected and acyclic can b

凌风 5209

PAT A1021 Deepest Root (25分)

PAT A1021 Deepest Root 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 th

kyln1039的博客 207

PAT 1021 Deepest Root (25 分)

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 results in a hi...

peanWang的博客 271

PAT A 1021 Deepest Root (25 分)

1021 Deepest Root (25 分) 题意(意会版) N个点,两两连了N-1条线,变成了一张图。这张图里要是没有成环的部分而且全部点都连在一起,就可以被看成一个树。 给你N个点和N-1条线,问你这个图: ①是不是树(是不是没有成环/全图连通)(这两个是一个概念。N个点N-1条线,有环必分块,分块必有环。); ②是树的话,给出最长的那些路的两端的标号; ③不是树的话,给出分成了几块。 逻...

虚境的博客 219

PAT甲级1021. Deepest Root (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 i

liaotl10的博客 541

PAT A1021 Deepest Root (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 r...

xiao1guaishou的博客 255

PAT--1021 Deepest Root (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. Input Specification: Each in

jackson_j的博客 171

PAT甲级-1021 Deepest Root (25分)

点击链接PAT甲级-AC全解汇总 题目: 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 highe...

卷子的博客 336
上一篇: 1010-Tempter of the Bone -HDUOJ
下一篇: 1060. Are They Equal (25)-PAT
zoushidexing
博客等级 码龄18年 70粉丝 171原创
评论 1
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值