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



 
手把手教你学GPU的KMD专栏--3.1 设备初始化流程深度解析 本文深入解析了KMD(内核模式驱动)设备初始化流程,这是GPU驱动开发最基础且关键的环节。初始化流程包括6个核心阶段:KMD模块加载、PCIe设备探测匹配、GPU硬件初始化、显存分配映射、中断系统配置和设备注册。每个阶段都有详细步骤解析,涉及关键数据结构和错误处理机制。文章还分析了常见问题(驱动加载失败、显存分配失败、中断配置失败)的解决方案,并指出未来趋势将向动态初始化、安全增强和AI优化方向发展。正确高效的初始化流程是确保GPU被操作系统安全使用的前提,也是后续内存管理、任务调度等功能的基础。 阅读详情

相关推荐

谈谈SAP顾问的几个发展趋势

大家新年好,今天想聊一下对SAP顾问发展趋势的一些个人见解。过去10多年得益于我国的经济发展,外资不断加大在中国的投资和中国企业的发展,SAP顾问行业得到了很大的发展和壮大。那么未来几年SAP顾问的职业发展到底怎么样呢?

syounger的专栏 2514

pat 甲级 1021 Deepest Root

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

weixin_50671259的博客 1022

泛微OA与金蝶云星空审批流程对接实战:从报销单到采购合同的完整指南

本文详细介绍了泛微OA与金蝶云星空审批流程的深度集成方案,涵盖从报销单到采购合同的完整对接指南。通过API层面的无缝连接,实现流程自动化、数据同源和权限联动,显著提升企业业务效率。文章还提供了环境配置、数据映射、接口安全等实用技巧,助力企业轻松完成系统对接。

weixin_29268637的博客 245

【PAT(甲级)】1021 Deepest Root

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

weixin_55202895的博客 406

别再手动量了!用Python+Open3D给BIM模型做‘CT扫描’,自动揪出施工误差(附完整代码)

本文介绍如何利用Python和Open3D技术实现BIM模型的毫米级施工误差自动检测。通过点云预处理、模型配准和偏差分析,系统能自动生成三维差异热力图和验收报告,显著提升施工质量检测效率。特别适合混凝土结构错位和钢结构安装偏差等常见问题的智能分析。

weixin_29284657的博客 243

从Verilog到GDS:手把手教你搞定Calibre LVS物理验证(附常见报错解决)

本文详细介绍了从Verilog到GDS的Calibre LVS物理验证全流程,包括工具链配置、RTL到SPICE网表转换、LVS运行配置及高级技巧。重点解析了常见报错如网络不匹配、器件不匹配和电源引脚缺失的解决方案,帮助工程师高效完成芯片物理验证,确保设计正确性。

culing2941的博客 508

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

保姆级教程:用TIA Portal V17给S7-1200 PLC和HMI做个联调仿真(附抢答器完整项目包)

本文提供了一份详细的TIA Portal V17教程,指导如何实现S7-1200 PLC与HMI的联合仿真开发。通过抢答器项目实例,涵盖从环境搭建、PLC编程到HMI设计的全流程,帮助工程师高效完成工业自动化系统的虚拟调试,显著提升开发效率。

weixin_30417487的博客 630

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

1021 Deepest Root

老顽固也可爱 371

手把手用ROS实现Ego-Planner动态避障:无人机撞树问题终结方案

本文详细介绍了如何在ROS和Gazebo仿真环境中,通过配置和调优Ego-Planner算法,实现无人机的动态避障功能。文章深入解析了其基于局部梯度优化的核心思想,提供了从环境搭建、参数详解到代码集成的完整实践指南,并分享了实战排错经验,旨在彻底解决无人机在复杂动态场景中的碰撞问题。

weixin_29031161的博客 1631

Allegro PCB设计小技巧:如何用Shift键快速平移复制走线和过孔?

本文深入解析了Allegro PCB设计中利用Shift键进行平移复制的核心技巧。通过对比传统复制粘贴流程,详细演示了如何高效、精准地批量复制走线和过孔,尤其适用于创建等间距电源总线、过孔阵列等场景,能显著提升PCB设计效率与准确性。

pz89012345的博客 554

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

让AI镜头“活”起来:22个核心运镜提示词,告别视频画面呆板

介绍了22种影视级镜头语言的AI提示词用法。内容涵盖推轨/后拉镜头等距离变化技巧、横摇/俯仰等方向控制手法、环绕/升降等空间位移方式,以及POV视角等特殊构图。

weixin_43489601的博客 6282

【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

TMS_WebGMaps_1.9.8.0_Full_Source

TMS_WebGMaps_1.9.8.0_Full_Source 全源码 delphi cbuilder

NTscan扫描工具

ntscan是一个扫描器,可以扫描没有密码和弱密码的共享,先到配置文件中填写猜测的用户名和密码,扫描过程中右侧出现的是扫描日志。

PAT甲级 1021 Deepest Root (25分)

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

weixin_43684054的博客 307

最新的sdio 4.0 spec 协议标准 带书签 高清完整PDF版

Part-E1-SDIO-Specification-Ver4.00-Final-120220.pdf:最新的sdio 4.0 spec 协议标准 带书签 高清完整PDF版,包括SDIO 接口的外设,SDIO 接口官方协议等。

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

jdk-8u144-linux-x64.tar.gz.

该资源为搭建Linux系统Java环境用,资源的后缀为tar.gz格式的压缩包

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

余额充值