1021 Deepest Root (25 point(s))

MIPI转LVDS芯片调试实战:从Kernel到UEFI的完整点亮流程 本文详细解析了MIPI转LVDS芯片调试的全流程,从Kernel驱动开发到UEFI早期初始化,涵盖I2C通信、电源时序控制等核心挑战。通过LT9211芯片实战案例,提供设备树配置、电源时序优化及显示异常排查等实用技巧,助力工程师高效完成屏幕调试工作。 阅读详情

1021 Deepest Root (25 point(s))

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

考察知识点:连通分支、DFS

注意点:

1. 并查集求连通分支的方法;

2. 图的存储方法;

3. DFS求树的深度以及各结点所在的层次

思路:

先给出结论,一棵有N个结点的树,一定有N-1条边(离散数学中有证明)。因此,首先用并查集来计算连通分支数,如果不连通输出,否则说明这一定是一棵树,没有环。

下一个任务是确定以哪一个结点为根,树有最大的深度。采用遍历每一个结点的方法,暴力求解,问题转化为已知根节点求树的深度。用数组visit[]、level[]分别记录第i个结点是否已经被访问以及所在的层次,使用DFS搜索。

参考大神链接:https://blog.youkuaiyun.com/richenyunqi/article/details/79551890

图相关的题目在近几年PAT-A中越来越多,需要重视!

#include<iostream>
#include<cstdlib>
#include<cstring>
#include<vector>
using namespace std;
const int MAX = 1e4+7;
int Tree[MAX];
int findRoot(int x){//寻找根结点 
	if(Tree[x]==-1) return x;
	else{
		int temp = findRoot(Tree[x]);
		Tree[x] = temp;
		return temp;
	}
}
bool visit[MAX];
int maxLevel[MAX];//第i个结点作为根节点时,树的最大深度
int level[MAX];//当前根节点下,第i个结点的层次(深度) 
vector<int> graph[MAX];
void dfs(int v,int curRoot){//寻找以curRoot为根节点,从v开始的各个结点的层次 
	visit[v]=true;
	for(int i=0;i<graph[v].size();i++){//遍历每个邻接结点 
		int k = graph[v][i];//当前邻接结点 
		if(!visit[k]){
			level[k]=level[v]+1;
			maxLevel[curRoot]=max(level[k],maxLevel[curRoot]);//跟新最大树深度 
			dfs(k,curRoot); 
		}
	} 
}
int main(void){
	int n,a,b;cin>>n;
	memset(Tree,-1,sizeof(Tree));
	for(int i=1;i<=n-1;i++){
		cin>>a>>b;
		int na = findRoot(a);
		int nb = findRoot(b);
		if(na!=nb) Tree[na]=nb;
		graph[a].push_back(b);
		graph[b].push_back(a);
	}
	int bCnt=0;//计算连通分支数 
	for(int i=1;i<=n;i++){
		if(Tree[i]==-1) bCnt++;
	}
	if(bCnt>=2) cout<<"Error: "<<bCnt<<" components"<<endl;
	else{
		for(int i=1;i<=n;i++){
			memset(level,0,sizeof(level));
			memset(visit,false,sizeof(visit));
			dfs(i,i);//深度优先搜索
		} 
		int deepest = 0;
		for(int i=1;i<=n;i++) deepest=max(deepest,maxLevel[i]);
		for(int i=1;i<=n;i++){
			if(maxLevel[i]==deepest) cout<<i<<endl;
		}
	}
	return 0;
}

 

【MATLAB代码】TDOA+TOA融合算法,高精度定位。通过多边定位解算4个锚点、平面目标的位置,附代码 二维平面下的TOA、TDOA及混合定位算法,通过4个锚节点对目标点进行定位。 阅读详情

相关推荐

Agent本地部署教程详细步骤

在开始之前,你需要明确你要部署的Agent是什么。如果你需要部署其他Agent,请参考其官方文档调整步骤。

weixin_39673931的博客 2494

1021 Deepest Root (25 point(s)) - C语言 PAT 甲级

设计思路: DFS 1. DFS 求连通分量,若大于 1,直接输出连通分量数 2. 若能构成数,再一次 DFS,寻找最大深度的根节点: * 从第一次 DFS 得到的最大深度节点集合中,任选一个开始,再次得到一个最大深度节点的集合,两个节点集合的并集即为所求

有三只土豆 329

常见的数值积分方法 (欧拉、中值、龙格-库塔,【常用于IMU中】)

1. 积分基本概念 设F(x)为函数f(x)的一个原函数,我们把函数f(x)的所有原函数F(x)+C(C为任意常数)叫做函数f(x)的不定积分(indefiniteintegral)。 非线性微分方程: 在有限的时间间隔Δt积分: 连续时间内积分: 工程上最常见的有三种:欧拉积分(Euler method)、中值积分(Midpoint method)和龙格-库塔法积分(Runge–Kutta methods)。他们的区别就是如何用数值方法模拟一个斜率。这里简单总结如下: ...

努力努力努力 2万+

[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

从网格划分到结果解读:一次完整的子弹侵彻LS-DYNA仿真避坑指南

本文详细解析了子弹侵彻LS-DYNA仿真中的关键技术与避坑指南,涵盖网格优化、对称边界条件设置、沙漏控制参数调整及结果可信度验证。通过HyperMesh网格划分和LS-DYNA高级参数配置,帮助工程师提升仿真精度与效率,确保计算结果物理可信。

weixin_29816689的博客 406

Qt Installer Framework4.6.1的最基础使用1(win10+离线安装包 )

因工作需要,win程序发布后,打包成一可执行exe文件,且兼容win7系统笔者记录了一些关键要点,供有需要的小伙伴使用。

aggs1990的专栏 904

1021 Deepest Root (25)(25 point(s))(cj)

1021 Deepest Root (25)(25 point(s))Questions 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 roo...

Cute_jinx的博客 275

光栅衍射有入射角时_偏振光栅的衍射角度及衍射效率测量

偏振光栅是一种基于入射光的偏振态实现选择性分光的衍射光学元件,衍射角度取决于光栅线数。通过控制入射光的偏振态,偏振光栅能够调控正一阶和负一阶之间的能量分布。当入射光束偏振态不同时,偏振光栅具有不同的衍射特性。理想的偏振光栅:当入射光为右旋圆偏振光时,只有+1级衍射光,衍射光为左旋圆偏振光。当入射光为左旋圆偏振光时,只有-1级衍射光,衍射光为右旋圆偏振光。当入射光为非圆偏振光时,偏振光栅同...

weixin_39552768的博客 6003

1021 Deepest Root (25 point(s))

题解 两遍dfs #include&lt;iostream&gt; #include&lt;vector&gt; #include&lt;cstdio&gt; #include&lt;set&gt; #include&lt;cstring&gt; using namespace std; const int MAXN = 1e4 + 10; vector&lt;int&gt; t; ...

徐伯莱 356

数字滤波器应用介绍

线性滤波器FIR ,会引起恒定延迟,通过补零的方式补偿非线性滤波器IIR,会引起频率相关的延迟,通过零相位延迟filtfilt补偿从信号中去除不需要的频谱时,通过滤波和补偿实现。如果需要去除的频率与采样率相差甚远,比如4kHz 与100Hz, 可通过 低通滤波->补偿->降采样–> 滤波 补偿 -->上采样还原的方法。*降采样前一定要先低通滤波为了防止混叠。

weixin_39107270的博客 274

手把手教你学Simulink——基于Simulink的固态变压器(SST)多级协同控制

摘要:本文详细介绍了基于Simulink的固态变压器(SST)多级协同控制方法。SST作为新一代智能电网核心设备,通过AC-DC整流、DC-DC隔离和DC-AC逆变三级架构实现电能高效转换。针对多级耦合难题,提出能量-功率协同控制策略:纵向解耦电压环与功率前馈,横向联动级间功率指令。在Simulink中搭建10kV/400V系统模型,实现直流母线电压波动<2%的动态性能。特别设计了负载突变时的三级协同机制,通过虚拟惯性算法和前馈控制实现快速响应。文章包含完整的建模步骤、控制推导和参数设置,为电力电子工

MHD0815的博客 192

PAT(甲级)1021 Deepest Root (25point(s))

题目 题目链接 思路 题目大意:给一张图,这个图有点特殊,没有环且边数等于节点数-1,即是一棵树;每个节点都可能是根节点,要求是输出那些作为根节点时树的深度最大的节点; 首先要判断是不是联通的,可以用并查集做,可以用图的DFS遍历做; 然后如果是联通的话,需要找到那些作为根节点时树的深度最大的节点;这里有一个方法,那就是DFS两次,第一次从任意一个节点出发,假设从1号节点出发,DFS时会遍历所有点,并得到每个点的深度,我们把那些深度最大的点记录下来,存放在temp中;第二次从temp 数组的任意一个元素出发

hjyp的博客 220

华为交换机系统软件升级和安全漏洞修复教程

在华为交换机国网可以查询到华为交换机官方电话:400-822-9999,通过下命令查询出需要升级的交换机软件版本和补丁版本号发给华为,获取新的升级系统软件和补丁以及升级教程。下面是我升级华为交换机总结的教程,作为参考。...

星如雨落 2964

1021 Deepest Root (25 point(s))

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

佳迪斯的博客 187

IEM模型,地表粗糙度模拟

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

VC中sqlite的使用

在VC中对sqlite的使用,实现数据库大批数据的快速插入,读取数据库中的数据在ListCtrl中显示

PAT (Advanced Level) Practice 1021 Deepest Root (25 point(s))

题目 PAT 甲 1021 链接 题目大意 给出一棵树,如果不满足树,则输出一共有多少个联通集,否则,输出根节点的集合,此处的根节点必须满足从该点遍历,有最大的深度。 解题思路 第一遍DFS既确定一共有几个联通子集,同时,保存从每个根节点进行遍历,每一层的节点编号。把深子层的节点放入到集合s 第二遍DFS,从第一次的最深的节点中,任选一个进行DFS,把得到的最深层的节点与第一次的s集合取交集,即可得到结果 #include<bits/stdc++.h> using namespace std

helloful的博客 177

Probability and Statistics - 4th edition

Probability and Statistics - 4th edition

1021 Deepest Root (25)(25 point(s))

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

weixin_30469895的博客 81

计算机控制技术及应用

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

PAT(A) 1021 Deepest Root (25 point(s))

任取一个结点作为root求出高度最大的结点,再从其中任取一个结点作为root求出高度最大的结点,两个集合求并即为the deepest root #include<bits/stdc++.h> using namespace std; int maxlevel = -1; vector<vector<int>> t; vector<int> vis, temp; set<int> root; void BFS(int index, int de

Anpotan的博客 105

1021 Deepest Root (25 分)

问题描述:一个连接图,将其转化为树,求以哪个点为根,使树的高度最高。 解题思路:以任意一点出发,得到最深的点都是最深的根,再以其中一个出发收集其他根(证明???),dfs与bfs都可以。 AC代码: dfs: #include&lt;iostream&gt; #include&lt;vector&gt; #include&lt;set&gt; #include&lt;cstring&gt...

lianwaiyuwusheng的博客 224

1021. Deepest Root (25)

#include #include #include #include using namespace std; const int maxn = 10010; int deepest = 0; vector city[maxn]; int deep_r[maxn], p = 0; bool visit[maxn] = {false}; bool visit1[maxn] = {fa

江湖小虾米的博客 316

[PAT]1021. Deepest Root (25)@Java实现

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

zjkC050818的博客 475

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

永无止境 2271

甲级PAT 1021 Deepest Root(dfs+二维vector)

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

Joyceyang_999的博客 257

暴力_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 h...

A_Bo的博客 600

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

dijiang0473的博客 177

【PAT】A1021. Deepest Root (25)

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

Duke 215

1021 Deepest Root (25 分)(C语言实现)(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 ...

JJJ's blog 370
上一篇: 1069 The Black Hole of Numbers (20 point(s))
下一篇: 1003 Emergency (25 point(s))(未完待续)
coderwait
博客等级 码龄9年 20粉丝 210原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值