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 连通.边 ...
随机推荐
- js实现前端动态筛选表格内容
代码参考: http://www.sharejs.com/codes/javascript/4289 http://www.jb51.net/article/103420.htm https://ww ...
- System.Web.UI.WebControls.FileUpload.cs
ylbtech-System.Web.UI.WebControls.FileUpload.cs 1. 程序集 System.Web, Version=4.0.0.0, Culture=neutral, ...
- PAT甲级——A1133 Splitting A Linked List【25】
Given a singly linked list, you are supposed to rearrange its elements so that all the negative valu ...
- 3列滚动抽奖 jquery.slotmachine
效果图: 需引入js文件: <script src="js/jquery-3.2.0.js"></script> <script src=" ...
- idea存在包但是运行项目一直报java.lang.NoClassDefFoundError的问题
以前做spark时候遇到过,今天高flink又遇到查了半天,这里记录下 1..idea->file->project structure->libraries->(左侧对话框这 ...
- C++单纯的指针传参的问题
C++指针传参也是单纯的复制一份地址,如下代码: #define _CRT_SECURE_NO_WARNINGS #include<iostream> using namespace st ...
- SSM三大框架的运行流程、原理、核心技术详解
一.Spring部分1.Spring的运行流程第一步:加载配置文件ApplicationContext ac = new ClassPathXmlApplicationContext("be ...
- umount:将文件设备卸载
[root@centos57 ~]# umount /dev/hda1 用设备文件名来卸载 [root@centos57 ~]# umount /aixi 用挂 ...
- 0821NOIP模拟测试赛后总结
60分rank20.挂.完. 赛时状态 不是很好.老眼混花看错无数题目信息. 倒不是很困.尽管昨天晚上为了某个该死的s-h-s-j活动报告忙到了今天,但我不得不说车上的睡眠还是挺好的. 照例通读三道题 ...
- css正則匹配、模糊匹配
//所有class包含font-red的p标签 p[class~="font-red"] {color: red;} [abc^="def"] 选择 abc 属 ...