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 componentswhere 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的更多相关文章

  1. PAT甲级1021. Deepest Root

    PAT甲级1021. Deepest Root 题意: 连接和非循环的图可以被认为是一棵树.树的高度取决于所选的根.现在你应该找到导致最高树的根.这样的根称为最深根. 输入规格: 每个输入文件包含一个 ...

  2. PAT 甲级 1021 Deepest Root

    https://pintia.cn/problem-sets/994805342720868352/problems/994805482919673856 A graph which is conne ...

  3. PAT 甲级 1021 Deepest Root (并查集,树的遍历)

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

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

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

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

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

  8. A1021. Deepest Root

    A graph which is connected and acyclic can be considered a tree. The height of the tree depends on t ...

  9. [PAT] A1021 Deepest Root

    [题目大意] 给出n个结点和n-1条边,问它们能否形成一棵n个结点的树,如果能,从中选出结点作为树根,使整棵树的高度最大.输出所有满足要求的可以作为树根的结点. [思路] 方法一:模拟. 1 连通.边 ...

随机推荐

  1. linux下怎么安装Go开发环境?linux部署golang

    linux下怎么安装Go开发环境?linux部署golang  0.请自行安装SSH远程工具 1.  SSH远程登录你的linux服务器 2.   yum install mercurial安装 me ...

  2. CSS 常用的兼容性调试技巧

    1.实现所有浏览器主页居中 Firefox下主页居中代码:.box{margin:0px auto} IE5.5下主页居中代码:body{text-align:center;} 将以上两种代码,合在一 ...

  3. 如何上传文件到git

    具体有三大步骤: 一.创建新的仓库 二.本地仓库 三.git命令上传(需要下载git) 一.创建新的仓库   二.本地仓库 其实这个本地仓库就是文件的所在地,在哪都可以 三.git命令上传(需要下载g ...

  4. day 57 Django基础五之django模型层之关联管理器

    Django基础五之django模型层之关联管理器   class RelatedManager "关联管理器"是在一对多或者多对多的关联上下文中使用的管理器.它存在于下面两种情况 ...

  5. KVM热添加技术

    1.KVM热添加的种类 硬盘.网卡.内存.cpu 2.KVM热添加硬盘 1.在宿主机上创建硬盘 qemu-img create -f qcow2 /opt/web02_add01.qcow2 10G ...

  6. 搞笑:Java & PHP & C++ 之间的战斗!

    Java 拥有十八般武艺,PHP一招致命!PHP 果然是世界上最好的编程语言~ Java 连老将 C++ 也干不过,直接一招 KO,我也是醉了... 这里并不是贬低Java,只有资深老司机能看懂的图吧 ...

  7. Spring中AOP的实现

    Spring中整合了AOP的功能,虽然有不足,没有专门做AOP框架的那么完美,但是用一用感觉还是不错的 一些概念: AOP 面向切面编程 aspect 切面/切面类(我个人认为一个真正被解耦的程序,切 ...

  8. 多重背包 /// 单调队列DP oj1943

    题目大意: em.... 就是多重背包 挑战340页的东西 ...自己的笔记总结总是比较乱的 重点:原始的状态转移方程中 更新第 i 种物品时 重量%w[i] 的值不同 则它们之间是相互独立的: 1- ...

  9. spark2.0在IDE运行的问题

    spark2.0搭建到服务器跑很方便,但是本地跑和之前1.6还是有点区别,鼓捣了一点到半夜2点多总算能跑了.. 遇到的问题 1.idea千万要用file---setting-----plugins的s ...

  10. (转)线程池 ExecutorService 详细介绍以及注意点区别

    线程池 ExecutorService 相信java开发都用到,这里做个简单笔记 一 Java通过Executors提供四种线程池,分别为: newCachedThreadPool创建一个可缓存线程池 ...