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 连通.边 ...
随机推荐
- VS2008编译出现问题:error C2485: “__restrict”: 无法识别的扩展属性 解决办法
错误:Error3 error C2485: '__restrict' : unrecognized extended attribute f:\program files\microsoft vis ...
- quartz的job中注入spring对象!
一般情况下,quartz的job中使用autowired注解注入的对象为空,这时候我们就要使用spring-quartz提供的AdaptableJobFactory类. 自定义一个类: public ...
- 《DSP using MATLAB》Problem 8.36
上代码: function [wpLP, wsLP, alpha] = lp2lpfre(wplp, wslp) % Band-edge frequency conversion from lowpa ...
- 第三周课堂笔记4thand5th
循环打印 #计算字典中的键值对的个数 print(len(a)) #获取字典中键的列表 print(a.keys()) #获取字典中值的列表 print(a.values()) #获取字典中键值对的个 ...
- UBOOT命令简写说明
所以命令都可以简写,只要命令前面的一部分不会跟其它命令相同,就可以不用写全整个命令. save 命令 CRANE2410 # sa Saving Environment to Flash... Un- ...
- maven/gradle版本统一示例
<dependencyManagement> <dependencies> <dependency> <groupId>org.springframew ...
- ThinkPHP模型基础类提供的连贯操作方法
ThinkPHP模型基础类提供的连贯操作方法(也有些框架称之为链式操作),可以有效的提高数据存取的代码清晰度和开发效率,并且支持所有的CURD操作. 直线电机哪家好 使用也比较简单, 假如我们现在要查 ...
- python相关软件安装流程图解————————python安装——————python-3.7.1-amd64
首先查看自己的系统版本 是32位的还是64位的 https://www.python.org/downloads/windows/ —————————python下载安装 开始———————————— ...
- Joomla - 模块系统(新建模块、模块类别、自定义模块)
Joomla - 模块系统,模块配合模板的布局设置.菜单分配.权限分配能创建出一个内容丰富且易于管理的高度自定义前端页面架构 一.新建模块 进入后台,点击顶栏菜单 扩展管理 -> 模块管理 ,进 ...
- php链表笔记:单链表反转
<?php /** * Created by PhpStorm. * User: huizhou * Date: 2018/12/1 * Time: 11:41 */ /** * 1.链表的反转 ...