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



 
从AAAI 2025被拒到ACM MM 2025录用:我的论文投稿经验与教训总结 本文分享了作者从AAAI 2025被拒到ACM MM 2025录用的论文投稿经验与教训。详细解析了格式要求、可复现性清单等关键细节,提供了精准转投策略和提升录用概率的实用技巧,帮助研究者避免常见错误并优化投稿策略。 阅读详情

相关推荐

哨兵一号数据snap预处理_利用SNAP软件进行Sentinel-1A卫星微波影像的预处理

利用SNAP软件对雷达影像进行预处理,主要包括热噪声去除、轨道文件校正、辐射定标、滤波校正、多普勒地形校正,最终获得入射角和后向散射信息。数据:本次实验所用数据为哨兵一号(Sentinel-1A)干涉宽幅模式(Interferometric Wideswath,IW)下经过多视处理和地距转换的GRD格式产品一般处理流程如下:0 SNAP软件安装软件安装:安装成功后打开SNAP后如图所示:1 数据...

weixin_30000407的博客 1万+

pat 甲级 1021 Deepest Root

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

weixin_50671259的博客 1022

VFIO框架源码分析(一)- 从初始化到设备访问

本文深入剖析了Linux内核中的VFIO(Virtual Function I/O)框架,详细解读了其从初始化到设备访问的全过程。VFIO通过Container->Group->Device的层次化设计,利用IOMMU提供安全隔离,允许用户空间直接访问物理设备。文章基于Linux v5.15版本,重点分析了vfio_init模块入口、Container创建与配置、Group关联以及最终设备访问等关键环节,揭示了VFIO如何通过逐层授权机制构建安全可靠的设备直通模型,为虚拟化技术提供核心支持。

qq_36071928的博客 380

【PAT(甲级)】1021 Deepest Root

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

weixin_55202895的博客 406

风力发电机并网模型建立及仿真验证

传统能源日益匮乏,环境污染日趋严重,全球向安全、可靠和环保的可持续能源体系转型迫在眉睫,风电迎来了重要的发展契机。近年来,全球风电迅猛发展,我国已成为世界风电新增装机容量第一的国家。然而,随着风电规模的不断增大,风电场接入对系统影响不容忽视。随着变速恒频风力发电系统优越的性能突出和大容量电力电子技术的成熟,风速随机性引起的功率波动及故障情况下的动态响应,对电能质量,系统电压、频率的稳定性产生的影响需要深入研究。因而,本文结合实际项目,针对变速恒频风力发电系统建模,端口动态特性及对电网电能质量、运行稳定性的影

毕业设计区博客 1640

合宙Air780EP速连OneNET:AT+MQTT应用指南

本文详细讲解了合宙Air780EP使用AT指令通过MQTT接入OneNET的过程。

2401_82812461的博客 1390

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

别再瞎调参数了!用MATLAB fmincon优化傅里叶级数,为你的机器人设计“完美”激励轨迹

本文详细介绍了如何利用MATLAB的fmincon优化器设计机器人激励轨迹,通过优化傅里叶级数参数,解决传统试错法的低效问题。文章涵盖了目标函数设计、约束条件转化及优化算法选择,帮助工程师实现高精度的系统辨识和动力学参数优化。

weixin_42613501的博客 240

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

1021 Deepest Root

老顽固也可爱 371

从光猫到机顶盒:H10G-13融合网关双系统网络配置全解析(含超密获取技巧)

本文详细解析了中国移动H10G-13融合网关的双系统网络配置,涵盖光猫路由与安卓电视盒子的协同工作。通过晶晨S905L3芯片的优化,设备实现高效网络接入与安卓9.0系统的定制化应用,特别适合酒店和民宿行业的智能化改造。文章还提供了超密获取技巧和线刷固件包的实用指南。

weixin_42525264的博客 948

AI处理器组合

编号0-3的处理器处于同一个链路中,编号4-7的处理器处于另外一个链路中,不通链路中的处理器不能通信,如下图所示。- 如果申请处理器个数为1,则选择同一链路,剩余可用的处理器数量为1个的最佳,其次是剩余3个的为次佳,然后是剩余2个,最后是剩余4个。输出为组合列表,当array=[0, 1, 4, 5, 6, 7] ,num=1时,输出为[[0], [1]]- 如果申请处理器个数为2,则选择同一链路剩余可用的处理器数量2个的为最佳,其次是剩余4个,最后是剩余3个。任务申请1个处理器。

面试高手的博客 1万+

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

2xmax96717+max96724+agx orin驱动代码

说明:这里主要解决max96717的设备地址和sensor地址映射,vc0->vc1多颗max96717+max96724使用配置思路如下: 1、改变max96717的pipez stream id,然后max96724的pipe绑定使用相应的stream id 2、max96717默认是vc0,把vc0改写成vc1\vc2\vc3,然后max96724再重新映射vc输出

mantouyouyou的博客 113

【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

IEM模型,地表粗糙度模拟

利用微波反演土壤水分时候,利用IEM模型可以实现对随机地表的模拟

VB摄像头监控

该VB模块可对摄像头捕获的画面进行录像等,可在创建的窗口进行控制

PAT甲级 1021 Deepest Root (25分)

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

weixin_43684054的博客 307

Devexpress16.2.6破解工具

Devexpress16.2.6破解工具Devexpress16.2.6破解工具Devexpress16.2.6破解工具Devexpress16.2.6破解工具

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

老版QQ原生表情gif动态

这是老版的QQ表情,不过全是动态的的,Windows照片查看器是不会动的,可以右击用浏览器打开查看,再说一遍是老版的,只是分享给宁愿用老版也要动态的人。

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、付费专栏及课程。

余额充值