PAT甲级——A1021 Deepest Root
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 (≤) 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
#include <iostream>
#include <vector>
#include<set>
using namespace std;
vector<vector<int>>G;
int N, maxH = ;
bool visit[];
set<int>res;
vector<int>temp; void DFS(int node, int H)
{
if (H > maxH)
{
temp.clear();
temp.push_back(node);//更新新的根节点
maxH = H;
}
else if (H == maxH)
temp.push_back(node);//相同的最优解
visit[node] = true;
for (int i = ; i < G[node].size(); ++i)
if (visit[G[node][i]] == false)
DFS(G[node][i], H + );
} int main()
{
int a, b, s1 = , cnt = ;
cin >> N;
G.resize(N+);
for (int i = ; i < N; ++i)
{
cin >> a >> b;
G[a].push_back(b);
G[b].push_back(a);
}
for (int i = ; i <= N; ++i)
{
if (visit[i] == false)//开始深度搜索遍历,如果是一个联通区域,则只会执行一次
{
DFS(i, );
if (i == )
{
if (temp.size() != )
s1 = temp[];
for (int j = ; j < temp.size(); ++j)
res.insert(temp[j]);
}
cnt++;//计算集合数
}
}
if (cnt != )
printf("Error: %d components\n", cnt);
else
{
temp.clear();
maxH = ;
fill(visit, visit + N + , false);
DFS(s1, );
for (int j = ; j < temp.size(); ++j)
res.insert(temp[j]);
for (auto r : res)
cout << r << endl;
}
return ;
}
PAT甲级——A1021 Deepest Root的更多相关文章
- PAT甲级1021. Deepest Root
PAT甲级1021. Deepest Root 题意: 连接和非循环的图可以被认为是一棵树.树的高度取决于所选的根.现在你应该找到导致最高树的根.这样的根称为最深根. 输入规格: 每个输入文件包含一个 ...
- PAT 甲级 1021 Deepest Root
https://pintia.cn/problem-sets/994805342720868352/problems/994805482919673856 A graph which is conne ...
- PAT 甲级 1021 Deepest Root (并查集,树的遍历)
1021. Deepest Root (25) 时间限制 1500 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue A graph ...
- PAT 甲级 1021 Deepest Root (25 分)(bfs求树高,又可能存在part数part>2的情况)
1021 Deepest Root (25 分) A graph which is connected and acyclic can be considered a tree. The heig ...
- PAT Advanced A1021 Deepest Root (25) [图的遍历,DFS,计算连通分量的个数,BFS,并查集]
题目 A graph which is connected and acyclic can be considered a tree. The height of the tree depends o ...
- PAT甲级:1066 Root of AVL Tree (25分)
PAT甲级:1066 Root of AVL Tree (25分) 题干 An AVL tree is a self-balancing binary search tree. In an AVL t ...
- PAT A1021 Deepest Root (25 分)——图的BFS,DFS
A graph which is connected and acyclic can be considered a tree. The hight of the tree depends on th ...
- A1021. Deepest Root
A graph which is connected and acyclic can be considered a tree. The height of the tree depends on t ...
- [PAT] A1021 Deepest Root
[题目大意] 给出n个结点和n-1条边,问它们能否形成一棵n个结点的树,如果能,从中选出结点作为树根,使整棵树的高度最大.输出所有满足要求的可以作为树根的结点. [思路] 方法一:模拟. 1 连通.边 ...
随机推荐
- System.Object.cs
ylbtech-System.Object.cs 1.程序集 mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c5619 ...
- JVM系列(三)— Java内存模型
我们已经了解了Java虚拟机的运行时数据区,垃圾收集相关知识,接下来学习虚拟机非常重要的部分 这就是Java内存模型与线程(第12章),这一篇,将主要讲讲内存模型 了解Java内存模型之前,先了解下计 ...
- Java性能优化的50个细节,我必须分享给你!
来源:blog.csdn.net/dongnan591172113/article/details/51790428 ;i<list.size();i++) ,len=list.size();i ...
- Cefsharp实现快捷键功能
原文:Cefsharp实现快捷键功能 1 . 实现IKeyboardHandler接口 public class KeyBoardHander : IKeyboardHandler { public ...
- pom.xml文件配置maven仓库地址
中央仓库就是Maven的一个默认的远程仓库,Maven的安装文件中自带了中央仓库的配置($M2_HOME/lib/maven-model-builder.jar) 在很多情况下,默认的中央仓库无法满足 ...
- Map和Reduce函数
- UMP系统架构 LVS
- 异常处理记录: Unable to compile class for JSP
出错信息截图: 经过搜索引擎的帮助, 发现这些引发异常的可能原因: 1. tomcat的版本必须大于等于JDK的版本 2. maven中的jar与tomcat中jar冲突 看看pom.xml, 果然j ...
- 关于操作系统中英文切换的.po和.mo介绍
一.文件简介 .po文件,.mo文件,.pot文件是由gettext程序生成或者使用的源代码和编译结果. 1..pot文件 是一种模板文件,其实质与.po文件一样,其中包含了从源代码中提取所有的 ...
- csps-s模拟测试62,63Graph,Permutation,Tree,Game题解
题面:https://www.cnblogs.com/Juve/articles/11631298.html permutation: 参考:https://www.cnblogs.com/clno1 ...