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. centos7 创建桌面快捷方式(chrome,eclipse)

    在将eclipse-SDK-3.7.2-Linux-gtk.tar.gz解压到某个目录下之后,命令行进行如下编辑 vi /usr/share/applications/eclipse.desktop ...

  2. [VS2008] Debug版本程序发布后 由于应用程序的配置不正确,应用程序未能启动,重新安装应用程序可能会纠正这个问题

    转自VC错误:http://www.vcerror.com/?p=59 问题描述: [VS2008] 版本程序发布后,运行程序弹出错误框: 由于应用程序的配置不正确,应用程序未能启动,重新安装应用程序 ...

  3. Delphi代码创建形式规范 1.0

                Delphi代码创建形式规范 1.0 本规范的目的:给自己的代码一个统一而标准的外观,增强 可读性,可理解性,可维护性 本规范的原则:名称反映含义,形式反映结构 1.单元风格 ...

  4. PAT甲级——A1108 Finding Average【20】

    The basic task is simple: given N real numbers, you are supposed to calculate their average. But wha ...

  5. springboot-配置多数据源(AOP实现)(HikariCP + MybatisPlus + mysql + SqlServer)

    场景: springboot项目,默认使用HikariCP连接池 + MybatisPlus持久层框架 + mysql数据库等一套流程,现需求需去第三方sqlserver数据库拉取数据,直连数据库,不 ...

  6. python字典的常用操作,数据类型划分

    一.数据类型划分之一 可分为:可变数据类型,不可变数据类型 不可变数据类型:元祖,布尔值(Bool),数值 int ,字符串 str               可哈希 可变数据类型:  list,d ...

  7. vue表格之:summary-method="getSummaries"与show-summary(列求和)

    //表格列求和 <el-table :summary-method="getSummaries" show-summary></el-table> getS ...

  8. spring自定义bean工厂模式解耦

    在resources下创建bean.properties accountService=cn.flypig666.service.impl.AccountServiceImpl accountDao= ...

  9. 关于spring java.lang.IllegalArgumentException: Name for argument type [java.lang.String] 的错误

    况描述: web工程在windows环境eclipse下编译部署没有问题,系统升级时需要运维从Git取相应的源码并编译部署到线上机器,部署启动正常没有错误,当访问业务的action时报错,如下. 错误 ...

  10. Sky Code

    Sky Code 给出n个数,求选出4个数组合,使其gcd为1,,\(n<=10000\),每个数\(<=10000\). 解 理解1:容斥原理 注意到Mobius反演式子不好写出,于是我 ...