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



 
解决com.kingbase8.util.KSQLException: This _connection has been closed. 一个消息管理系统,系统采用kingbase8数据库,数据库采用单体模式,后台应用也采用springboot单体模式。系统正式上线后,出现几个JDBC响应的异常信息: com.kingbase8.util.KSQLException: An I/O error occurred while sending to the backend. java.net.SocketTimeoutException: Read timed out. 阅读详情

相关推荐

1.2 重叠因子:双重指数移动平均(Double Exponential Moving Average, DEMA)概念与Python实战

双重指数移动平均(Double Exponential Moving Average, DEMA)是一种改进的移动平均线指标,由Patrick Mulloy在1994年开发。相比传统的指数移动平均线(EMA),DEMA通过减少滞后性来提供更快的市场反应。它通过两次计算EMA并进行特殊组合来实现这一目标,可以在保持平滑性的同时减少滞后。

Kewin的博客 2169

【PAT(甲级)】1021 Deepest Root

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

weixin_55202895的博客 406

Carsim2019与Simulink (Matlab2018b)联合仿真成功(超详细)

Carsim与Simulink联合仿真成功

强强的博客 3万+

pat 甲级 1021 Deepest Root

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

weixin_50671259的博客 1022

一步步教你使用 NVIDIA 推出的全新GPU加速求解器 cuOpt:解锁超高效率的求解体验

未完成

Linshaodan520的博客 869

黑猫带你学eMMC协议第2篇:eMMC部分相关名词释义

1 前言 1.1 声明 本文依据eMMC JEDEC5.1、网络资料及个人工作经验整理而成,如有错误请留言。 本文结合eMMC JEDEC5.1协议手册查看效果更佳。 1.2 本文背景 本文关键词不包括emmc jedec5.1协议第3章节:《Terms and definitions》内容。关于emmc协议层的关键词,直接查阅spec相关章节即可,清晰明了,此处没必要再列出。 本文为本人工作学习中常听到或常用到关乎于emmc/flash的一些关键词合集,没有固定分类,不需要强行记忆,供查询使用。 1.

黑猫奥利奥的博客 7442

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

组基轨迹建模 GBTM的介绍与实现(Stata 或 R)

组基轨迹建模(Group-Based Trajectory Modeling,GBTM)(旧名称:Semiparametric mixture model)历史:由DANIELS.NAGIN提出,发表文献《Analyzing Developmental Trajectories:A Semiparametric,Group-Based Approach》GBTM能够将一群人的轨迹分类并生成数个具有代表性的运动轨迹模型,然后对每个轨迹模型进行分析,以了解人们的运动特征、生理水平和风险等级等。

三分钟热度 1万+

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

1021 Deepest Root

老顽固也可爱 371

YOLOv8改进 | Conv篇:基于FasterBlock与C2f结构的二次创新,参数量下降70万【附保姆级代码】

通过引入FasterBlock二次创新C2f结构,我们成功地对YOLOv8模型进行了优化,显著降低了其参数量,并提升了推理速度,而精度保持不变甚至有所提升。该模型在多个实际应用场景中展现出了优异的性能,尤其是在嵌入式设备、移动端和自动驾驶等领域,具有广泛的应用潜力。未来的研究将进一步优化模型结构,探索更多的应用场景,为目标检测技术的发展做出贡献。

一键难忘的博客 3215

OpenClaw进行安全测试(附使用场景案例)

test-master 是一个全面的测试专家,覆盖功能测试、性能测试、安全测试三大领域。这里我们重点来看下使用test-master如何进行安全测试。使用 test-master 时,可以针对以下高频领域进行专项安全扫描:✅ OWASP Top 10 测试✅ SQL 注入测试✅ XSS 测试✅ CSRF 测试✅ 认证 / 授权测试。

十二测试录 331

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

【Unity实战】实现最完美最简单的UI适配方案,背景压缩或者拉伸变形解决方案,手机刘海屏适配,适配不同分辨率,不同尺寸,不同设备。

这期来讲一个简单的UI设计方案,很多同学可能搞不懂锚点、轴心这些概念,导致做好的UI在别人的设备上,乱跑或者是重叠,或者是参加游戏老发时间比较紧来不及做适配,都可以来试试这个方法,通过填充边框来保持游戏画面的原始比例,所以它的缺点也很明显,在一些移动设备上会浪费比较大的显示面积。 unity不同设备UI变形? UI在不同设备上如何适配?

向宇的博客,专注php/web全栈 unity游戏开发,欢迎大家评论纠错 3652

【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

NTscan扫描工具

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

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

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

PAT甲级 1021 Deepest Root (25分)

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

weixin_43684054的博客 307

Camera2Demo

在Camera2 的基础上进行封装,使得Camera2一堆初始化逻辑与Activity分离,降低耦合度,完成预览拍照并保存的操作

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

余额充值