PAT甲级 - 1021 Deepest Root (25分)

从赛题到实战:一份面向高职“区块链技术应用”赛项的食品溯源系统全栈部署与运维指南 本文为高职院校学生备战“区块链技术应用”技能大赛提供了一份详实的食品溯源系统全栈部署与运维指南。内容涵盖从理解赛题业务逻辑、绘制技术蓝图,到搭建4节点FISCO BCOS联盟链、部署管理平台与智能合约,再到核心运维技能与前后端联调测试的完整流程,旨在帮助参赛者将赛题转化为可运行的实战项目,全面提升区块链全栈开发与运维能力。 阅读详情

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 (≤10​4​​) 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

自己写出来的一道题目,因为题目中限制了边的个数为n-1,不可能形成一个大环,思路还是找连通块个数,连通块个数>1,说明存在环,直接输出连通块个数即可;连通块个数 == 1,说明是tree,从每个顶点出发,记录max_depth即可。

#include<cstdio>
#include<vector>
#include<algorithm>
using namespace std;
const int maxn = 10010;
vector<int> Adj[maxn];
vector<int> ans;
int n;
int visited[maxn] = { false };

void DFS(int v) {
	visited[v] = true;
	for (int i = 0; i < Adj[v].size(); i++) {
		if (visited[Adj[v][i]] == false)
			DFS(Adj[v][i]);
	}
}
int max_depth;
int cur_depth = 0;
void init() {
	cur_depth = 0;
	for (int i = 1; i <= n; i++) {
		visited[i] = false;
	}
}


void Find_deepest(int v, int depth) {
	visited[v] = true;
	if (depth > cur_depth)
	{
		cur_depth = depth;
	}
	for (int i = 0; i < Adj[v].size(); i++) {
		if (visited[Adj[v][i]] == false)
			Find_deepest(Adj[v][i], depth + 1);
	}
}
int main() {
	scanf("%d", &n);
	int v1, v2;
	for (int i = 0; i < n - 1; i++) {
		scanf("%d%d", &v1, &v2);
		Adj[v1].push_back(v2);
		Adj[v2].push_back(v1);
	}
	int block = 0;
	for (int i = 1; i <= n; i++) {
		if (visited[i] == false) {
			DFS(i);
			block++;
		}
	}
	if (block > 1) {
		printf("Error: %d components", block);
	}
	else {
		for (int i = 1; i <= n; i++)
		{
			init();
			Find_deepest(i, 0);
			if (cur_depth > max_depth) {
				max_depth = cur_depth;
				ans.clear();
				ans.push_back(i);
			}
			else if (cur_depth == max_depth) {
				ans.push_back(i);
			}
		}
		sort(ans.begin(), ans.end());
		for (int i = 0; i < ans.size(); i++) {
			printf("%d\n", ans[i]);
		}
	}
}

 

【24届计算机校招】面经汇总 这是笔者为了能够记录一下接下来一段时间会看的一些博客视频等汇总。一是为了汇总后常回来看看,而是为了后面的人在这个阶段准备的时候有个参考。现在已经6月底了,大部分高校的夏令营都出来了,而我至今还是夏0营,前几天还为川大会收双非而感到高兴,但遗憾的是川大并没要我。而我另一位双非的朋友已经拿到offer,在决定去南开还是华师了。没有对比就没伤害,我开始怀疑自己的决策是否正确——这段时间的夏令营我都有关注,但我仅报了少部分的几个酒吧舞,而没有选择海投,我觉得只要我认准了一个东西,我拼尽全力就一定能够获取到。 阅读详情

相关推荐

F460 CLK配置功能模块说明

F460CLK配置功能模块说明 目录 F460CLK配置功能模块说明 CLK配置: 1. DDL_ICG——初始化配置 2. DDL_UTILITY——基础功能函数 3.DDL_PRINT-待细化 4.DDL_ADC 5.DDL_AES——AES 加解密算法处理器 6.DDL_CAN 7. DDL_CLK 8. DDL_CMP——电压比较器 9. DDL_CRC 10. DDL_DCU——数据计算单元 11. DDL_DMAC——DMA 控制器 ...

childboy的博客 2460

【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

阿里巴巴达摩院人工智能训练师(高级)

阿里巴巴达摩院人工智能训练师(高级)

huang0612123的博客 1万+

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

实测有效防止colab自动断开连接

实测有效防止colab自动断开连接

Thebest_jack的博客 1万+

飞凌OK3588-C Linux R4系统下MIPI YUV摄像头CSI尺寸错误排查与极性适配方案

本文详细介绍了在飞凌OK3588-C平台的Linux R4系统下调试MIPI YUV摄像头时遇到的'csi size err'错误排查与极性适配方案。通过分析信号极性、驱动参数调整及FPGA协同调试,提供了从硬件检测到系统级调试的完整解决方案,帮助开发者快速解决类似问题。

bean的博客 259

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

IM 系统设计:仿微信架构,Netty + Protobuf + WebSocket 如何支撑百万级长连接?

相比 JSON,Protobuf 的体积通常只有前者的 1/5 到 1/10,且解析速度快一个数量级。// 消息类型:1-登录,2-心跳,3-单聊,4-群聊,5-ACK// 发送方ID// 接收方ID// 消息内容// 时间戳// 消息唯一ID,用于去重和ACK。

BUG猿的博客 1380

PAT甲级 1021 Deepest Root (25分)

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

weixin_43684054的博客 308

3D 韩系风格漫剧人设 | GPT-Image2 人设生成【附完整提示词】

宁姚同款清冷剑系美人,极致小巧鹅蛋脸,下颌线紧致纤细流畅,下巴小巧精致收窄,小脸衬得五官格外舒展,韩式水光肌,面部精细水光高光,鼻梁高光、鼻尖高光、面中苹果肌高光、唇珠高光,红棕色长卷发偏分大波浪,垂在一侧肩前,发量蓬松。发量丰厚蓬松有体积感,不贴头皮,几缕碎发自然垂落脸侧修饰脸型,长度过胸及背,发丝柔顺丝滑卷度自然,发尾自然垂落,身穿香槟色缎面挂脖长裙,挂脖设计无袖露肩露背,上半身围裹式荡领胸口垂褶,腰部堆褶收腰,修身直筒包臀裙长度到脚踝,缎面光泽感强垂坠感好,极简剪裁。纯白色背景,3D漫剧写实厚涂风。

欢迎来到鸾姝淡月博客,专为程序员和编程爱好者打造的技术博客。我们分享编程语言趋势、开发工具、编程哲学、职业发展等,助你掌握新技术,优化技能,激发编程热情。 146

YOLO26 旋转框检测 OBB 技术:角度预测与解码优化

YOLO26旋转框检测技术通过CSL角度编码有效解决了OBB检测中的周期性问题。该技术采用5参数表示法(cx,cy,w,h,θ),在检测头中分别处理分类、回归和角度预测。核心创新点包括:1)使用高斯或三角窗口的CSL编码对角度进行平滑表示;2)通过加权平均解码角度,避免边界不连续问题。该方法在遥感、文本检测等需要精确方向信息的场景中表现优异,相比传统HBB框可提升定位精度30%以上,同时保持了YOLO系列的高效特性。

一键难忘的博客 527

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

智能挂锁软件需求规格说明书(SRS)

智能挂锁系统SmartPadlockV1.0是一款融合蓝牙通信、指纹识别和远程控制技术的便携式安防设备。系统由设备端、移动App和云平台三部分组成,支持蓝牙配对(10米内)、指纹识别、OTP一次性密码等多种开锁方式,响应时间<1.5秒。产品采用SM2/SM4加密技术保障安全,日均20次使用下续航达180天,兼容iOS13+/Android8.0+系统。功能包括设备绑定、远程授权、开锁记录查询和OTA固件升级,符合个人信息保护法规要求,适用于仓储、快递柜等多场景应用。

moton2017的博客 2112

PAT甲级1021 Deepest Root (25 分)题解

\quad这个题暴力也可以过,我试过枚举每一个点开始进行dfs,记录从这个点开始到其他节点的最大深度和对应的节点,记录下来,相互比较得到答案,时间需要1000ms,题目给的限制是2000ms,完全够。 \quad正确解法是从树任一点a开始遍历,记录下距离a最远的顶点集合res1,再从res1中任选一个顶点开始第二次遍历,记录下距离该顶点最远的顶点集合res2,答案就是res1和res2中顶点去重...

qq_40438165的博客 1636

文档:亲测可用解决vnc灰屏,黑屏,鼠标大黑叉情况

主要解决vnc-viewer灰屏,黑屏,鼠标大黑叉等现象,详细见文档。

VB摄像头监控

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

【PAT甲级题解记录】1021 Deepest Root (25 分)

问题描述 求一个N个点N-1条边的图是否能构成二叉树,能的话输出能形成最大深度的根结点,不能的话输出联通分支数。 解题思路 一开始还以为要分别看回路和联通分支,但这道题给的数据N点N-1条边保证了这两种情况只会同时出现,那就简单很多了,直接暴力就可以过题。

retr0的博客 379

C# WinForm 基于DirectShow.dll编写的程序,可边预览边保存视频,音频和图片

基于微软的DirectShow.dll的二次开发1.自动识别连接的摄像头跟麦克风且可以对其进行选择(默认选中第一个)2.可录制wmv和avi两种格式的视频并且都带有音频,avi可以录制的分辨率和帧数进行选择,wmv默认进行了淸晰度优化如不喜欢可去除代码中有相关提示3.可在预览点开之后进行拍照,也可边录制视频边拍照4.添加了视频和音频的压缩功能(酌情使用)这次二次开发发现C#对DirectShow的相关信息太少,这次做完也不敢藏私共享出来,还望大家指正

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

laplacebh的博客 995

IEM模型,地表粗糙度模拟

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

Day46: [PAT甲级] 1021 Deepest Root (25分)

Day46: [PAT甲级] 1021 Deepest Root (25分) 题源: 来自PAT真题库: https://pintia.cn/problem-sets/994805342720868352/problems/994805482919673856 代码: dirty code凑合看吧 #include<iostream> #include<string> #...

qq2215459786的博客 161

1021 Deepest Root (25 分)-PAT甲级

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 calledthe deepest root. Input S.

神笔码农.的博客 746

PAT甲级1021 Deepest Root (25 分)

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

Dexter 191

PAT 甲级 1021 Deepest Root (25 分)

题意:找到能让树的深度最深的结点,若不唯一,升序输出。如果给出的图不连通,输出联通块的个数。 思路:并查集+求树的直径。 求出树最深是多少,然后每个结点dfs一次,如果等于那个最深的深度, 如果等于那个最深的深度就输出。 代码: include<iostream> #include<cstring> #include<cstdio> #include<al...

smilng的博客 283

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

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

valcanoshan的博客 192

PAT 甲级 1021 Deepest Root (25分)

题目 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. I

qq_41001689的博客 154

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

猪の成长日记 372

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

weixin_41514525的博客 152

PAT 1021 Deepest Root (25 分) -甲级

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

qq_39071488的博客 189

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

【BFS层次遍历+last变量】一遍AC~ 思路和之前的一样,使用BFS+last变量进行层次遍历就可以求出深度。遍历以每个结点做根的情况就可以顺利地得到一个深度啦!其他的就是基本操作了,不多b。 参考之前写过的一道题的思路:PAT1004 #include <bits/stdc++.h> using namespace std; int N; vector<int> v[...

weixin_42278063的博客 164
上一篇: PAT甲级 -1013 Battle Over Cities (25分) 求连通块的数量
下一篇: PAT甲级 - 1018 Public Bike Management (30分)
DedicateToAI
博客等级 码龄9年 6粉丝 90原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值