//1021. Deepest Root (25)
//第二个case是测试N =0 的情况
#include <iostream>
#include <algorithm>
#include <queue>
#include <vector>
using namespace std;
vector<int> Road[10005];
int VisitedNum = 0;
bool visit[10005] = {false};
int N;
queue<int> q;
int currlen = 0;
int maxlen = 0;
vector<int> result;
void dfs(int curr)
{
int i;
if (currlen >= maxlen)
{
if (currlen == maxlen)
{
if (find(result.begin(), result.end(), curr)==result.end())
{
result.push_back(curr);
}
}
else
{
maxlen = currlen;
result.pop_back();
if (find(result.begin(), result.end(), curr)==result.end())
{
result.push_back(curr);
}
}
}
for (i = 1;i<=N;i++)
{
if (i!=curr && /*Road[i][curr] == true*/ find(Road[curr].begin(), Road[curr].end(), i)!=Road[curr].end() && visit[i] == false)
{
visit[curr] = true;
VisitedNum++;
currlen++;
dfs(i);
visit[curr] = false;
VisitedNum--;
currlen--;
}
}
}
int main()
{
scanf("%d", &N);
int i;
int n1, n2;
for (i = 1;i<N;i++)
{
scanf("%d %d", &n1, &n2);
Road[n1].push_back(n2);
Road[n2].push_back(n1);
}
if (N == 1)
{
printf("1\n");
return 0;
}
//BFS
int componentsnum = 0;
int j;
for ( j = 1;j<=N;j++)
{
if (visit[j] == true)
{
continue;
}
q.push(j);
while(!q.empty())
{
int topnum = q.front();
q.pop();
for( i = 0 ;i<Road[topnum].size();i++)
{
if (visit[Road[topnum][i]] == false)
{
q.push(Road[topnum][i]);
visit[Road[topnum][i]] = true;
}
}
}
if (q.empty())
{
componentsnum++;
}
}
if (componentsnum != 1)
{
printf("Error: %d components", componentsnum);
return 0;
}
fill(visit, visit+N+1, false);
VisitedNum = 1;
visit[1]=true;
dfs(1);
if (result.size()> 1)
{
sort(result.begin(), result.end());
for (i = 0;i<result.size();i++)
{
printf("%d\n", result[i]);
}
return 0;
}
fill(visit, visit+N+1, false);
VisitedNum = 1;
visit[result[0]] = true;
currlen = 0;
//maxlen = 0;
dfs(result[0]);
sort(result.begin(), result.end());
for (i = 0;i<result.size();i++)
{
printf("%d\n", result[i]);
}
return 0;
}
相关推荐
从“盲控”到“预见”:聊聊永磁同步电机负载观测器(PMSM Load Observer)如何让控制更丝滑
本文深入探讨了永磁同步电机负载观测器(PMSM Load Observer)如何通过前馈补偿和转动惯量辨识技术实现智能抗扰控制。从传统‘盲控’到预见性调节,负载观测器显著减少转速波动和能耗,提升系统响应速度。文章详细解析了观测器算法架构、工程调参技巧及前沿发展趋势,为电机控制领域提供实用解决方案。
【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...
ISP一键下载电路分析+74HC1G66GW(信号图)
之前要不用的最小系统的开发板要不就是用的SWD调试口,没有注意过ISP一键下载电路是个怎么回事,因为需要就简单的看了一下野火的ISP一键下载电路。 默认情况一下,一般我们的程序都是从用户闪存也就是内部的FLASH启动的, 对于F103RCT6来说ROM的大小是512kb,启动地址位0x8000000,这个在IDE keil里可以看的到,如下图。 首先简单理解一下ISP是什么: 可以把他理解为芯片厂商出场时固化在ROM当中的一段程序,它的功能就是把你写的程序一点点的搬运到FLASH当中,它引出了两个引脚
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...
华为P812E光猫补全shell配置PON口模式
终于看到华为企业级的带2.5电口P812E横空出世了,相信很多发烧友也已经将此收入昂中。相信体验的同时,遇到的第一件事情就是配置PON口模式的问题,本文将带发烧基友解锁模式。
手把手教你学Simulink--基于Simulink的高增益DC-DC变换器仿真建模
特点高电压增益:可以在不使用变压器的情况下实现较大的电压升压比。适用于低电压源:如太阳能电池板、燃料电池等提供的低压直流电源。效率问题:设计时需特别注意提高效率,减少能量损失。挑战电路复杂性:通常涉及多个电感和电容,以及复杂的开关模式。元件选择:合适的元件参数对于达到理想性能至关重要。控制策略:确保高效稳定运行的同时,适应负载变化。首先明确要模拟的任务内容。实现特定的电压增益比(例如从24V升至380V)。根据预期的输入输出电压确定合适的电路拓扑和元件值。
1021. Deepest Root (25)-PAT
1021. Deepest Root (25) 时间限制 1500 ms 内存限制 32000 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue A graph which is connected and acyclic can b
金砖大赛-第二届网络安全防护治理实战技能大赛全国总决赛WP
格式进行md4加密,然后将小写的md4密文进行作为flag信息进行提交,提交格式为flag{MD4小写},例如调用频率异常高的user_id是user_xxx,使用的非公开模型名称是model-name,具体调用次数为10,那么按照格式,对user_xxx:model-name:10进行md4加密后,提交范例为:flag{dd00cb9e393f4a9ac639294b3cfe4d6a}挖矿木马是通过远程脚本进行部署的,请登录到失陷的服务器上,找到并分析挖矿木马是从那个远程IP下载的。
PAT甲级 1021 Deepest Root (25分)
1021 Deepest Root (25分) 题目链接:PAT A 1021 题目大意:给出一个数n代表节点的个数(节点编号1-n),给出各个边的连接方式,问哪个节点为根节点时,这棵树最高,如果有多个这样的结点,则按升序的方式输出。如果给出的图不是一棵树,那么输出它有几个部分组成。 思路分析:注意题中输入第一行是节点数目,接下来的n-1行是边。由于连通,节点数为n,边为n-1的图一定是一棵树...
【Claude】Auto Mode 触发安全分类器拦截的三种错误变体及对应处理 bug报错已解决
深度解析Claude Auto Mode安全分类器拦截的三种变体:内容拒绝、操作中断、累积触发,含完整排查流程与绕过策略。
IC 笔试真题之吾见——华为实习(1)
前言 今天(2022 04 07)刚建立了一个专栏《IC真题之吾见》,准备记录一些IC真题的见解,为以后找工作做一点准备。 刷题绝不是目的,以题代练,以题代学。 题目来自师兄申请 华为实习“设计”岗位 的笔试真题。 见解大部分来自自己学习的理解,也有部分超纲知识是网络检索得到,希望可以多多在评论区留言交流,共同进步! 有问题的地方也希望可以直接批评指正! 后面也会同步更新此专栏! 1、下列有关时序分析的描述正确的是() A. recovery时间,是指异步控制信号在有效时钟沿到达之后必须保持
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...
手把手教你从入门到精通C# 操作MySql数据库
本文是《C#操作MySql数据库从入门到精通》专栏的导读,帮助大家知道学习本专栏能够获取什么!
浙大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
计算机控制技术及应用
《计算机控制技术及应用》通过总结作者的科研成果和吸收国内外的先进理论、方法和技术,以计算机控制系统的网络化、开放化、智能化和集成化发展趋势为主线索,主要介绍计算机控制系统的基本概念和计算机控制系统的典型形式,计算机控制系统的数学描述与基本分析方法,以及数字PID控制器、最少拍控制系统、达林算法、状态变量设计法等设计方法及其各种改进算法。重点突出计算机控制系统输入输出通道接口的设计方法和典型控制系统的设计案例与实现方法等。
VB摄像头监控
该VB模块可对摄像头捕获的画面进行录像等,可在创建的窗口进行控制
[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 ...
蓝牙模块HM-10手册
蓝牙模块HM-10手册,该手册对蓝牙模块HM-10进行了详细的介绍和使用方法以及一些AT指令。
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...
jdk-8u144-linux-x64.tar.gz.
该资源为搭建Linux系统Java环境用,资源的后缀为tar.gz格式的压缩包
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...
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
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
PAT A 1021 Deepest Root (25 分)
1021 Deepest Root (25 分) 题意(意会版) N个点,两两连了N-1条线,变成了一张图。这张图里要是没有成环的部分而且全部点都连在一起,就可以被看成一个树。 给你N个点和N-1条线,问你这个图: ①是不是树(是不是没有成环/全图连通)(这两个是一个概念。N个点N-1条线,有环必分块,分块必有环。); ②是树的话,给出最长的那些路的两端的标号; ③不是树的话,给出分成了几块。 逻...
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...
PAT 1021 Deepest Root (25)
先dfs一次找到最长的一端,作为根进行第二次dfs,找到所有最长的树。 要考虑只有1个点的情况 #include #include #include #include using namespace std; const int N=10010; int cnt=1,start[N],n,rt[N],dist[N],a[N]; struct edge{ int v,next; }e[2*N
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...
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 r...
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
【PAT甲级题解记录】1021 Deepest Root (25 分)
问题描述 求一个N个点N-1条边的图是否能构成二叉树,能的话输出能形成最大深度的根结点,不能的话输出联通分支数。 解题思路 一开始还以为要分别看回路和联通分支,但这道题给的数据N点N-1条边保证了这两种情况只会同时出现,那就简单很多了,直接暴力就可以过题。
406



被折叠的 条评论
为什么被折叠?



