1021. Deepest Root (25)

时间限制
1500 ms
内存限制
65536 kB
代码长度限制
16000 B
判题程序
Standard
作者
CHEN, Yue

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 (<=10000) 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

思路

二次dfs
1.第一次dfs确定图是否连通,如果连通找到最深的那个点first(多个就取最先被找到的),没有输出题目要求的错误信息。
2.第二次dfs重置所有状态,然后从first开始dfs,找到的所有的最深的点即是题目要求的节点,依次插入一个set容器中(每次插入会自动排序)。

3.输出set中的所有元素就行。

代码
#include<iostream>
#include<vector>
#include<set>
using namespace std;
vector<vector<int>> graph;
vector<int> highestNodes;
vector<bool> visits(10005,false);
int maxheight = 1;
set<int> results; void dfs(int root,int height)
{
visits[root] = true;
if(height >= maxheight)
{
if(height > maxheight)
{
highestNodes.clear();
maxheight = height;
}
highestNodes.push_back(root);
}
for(int i = 0;i < graph[root].size();i++)
{
if(!visits[graph[root][i]])
dfs(graph[root][i],height + 1);
}
} inline void resetVisits(const int n)
{
for(int i = 0;i <= n;i++)
visits[i] = false;
} int main()
{
int N;
while(cin >> N)
{
//input
graph.resize(N + 1);
for(int i = 1;i < N;i++)
{
int a,b;
cin >> a >> b;
graph[b].push_back(a);
graph[a].push_back(b);
} int cnt = 0;
//handle
int first = -1; //最高的节点之一
for(int i = 1;i <= N;i++)
{
if(!visits[i])
{
cnt++;
dfs(i,1);
if( i == 1)
{
for(int j = 0;j < highestNodes.size();j++)
{
results.insert(highestNodes[j]);
if(j == 0)
first = highestNodes[j];
}
}
}
} if(cnt > 1)
cout << "Error: "<< cnt <<" components" << endl;
else
{
highestNodes.clear();
maxheight = 1;
resetVisits(N);
dfs(first,1);
for(int i = 0;i < highestNodes.size();i++ )
results.insert(highestNodes[i]);
for(auto it = results.begin();it != results.end();it++)
cout << *it << endl;
}
}
}

  

 

PAT1021:Deepest Root的更多相关文章

  1. PAT-1021 Deepest Root (25 分) 并查集判断成环和联通+求树的深度

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

  2. PAT1021. Deepest Root (25)

    之前不知道怎么判断是不是树,参考了 http://blog.csdn.net/eli850934234/article/details/8926263 但是最后有一个测试点有超时,在bfs里我用了数组 ...

  3. PAT甲级1021. Deepest Root

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

  4. 1021.Deepest Root (并查集+DFS树的深度)

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

  5. 1021. Deepest Root (25) -并查集判树 -BFS求深度

    题目如下: A graph which is connected and acyclic can be considered a tree. The height of the tree depend ...

  6. A1021. Deepest Root

    A graph which is connected and acyclic can be considered a tree. The height of the tree depends on 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. 1021. Deepest Root (25)

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

  9. PAT 甲级 1021 Deepest Root

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

随机推荐

  1. SpriteBuilder中粒子发射器的reset on visibility toggle选项解释

    如果选中该选择框,表示粒子发射器将删除所有已存在的粒子当它们的可见状态被代码改变的时候. 如果该选择框没有选中,则发射器将保持产生粒子但不渲染它们(意思是有但你看不到)当它们的可视状态为NO的时候. ...

  2. 用U盘安装Ubuntu15.04

    用UltraISO刻录Ubuntu15.04到U盘安装,出现:Failed to load idlinux.c32错误,解决办法如下: source url: http://www.ubuntukyl ...

  3. 我所犯的JavaScript引用错误

    近期在w3cschool学习JavaScript和php--学完后,开始帮一哥们友情写网站.但是在使用ajax和Jquery的时候发现,我自己写的脚本不能运行.捣鼓了半天,没有发现任何语句错误.调试器 ...

  4. 【Java编程】随机数的不重复选择

    随机数的不重复选择就是从n个数中随机选取m(m<n)个数.在本文中,我们用Java来实现.因此我们先介绍Java的相关知识. 在Java中,Java.util.Set接口和Java.util.L ...

  5. DEVICE_ATTR

    说道sysfs接口,就不得不提到函数宏 DEVICE_ATTR,原型是 #define DEVICE_ATTR(_name, _mode, _show, _store) \ struct device ...

  6. Android Widget小组件开发(一)——Android实现时钟Widget组件的步骤开发,这些知识也是必不可少的!

    Android Widget小组件开发(一)--Android实现时钟Widget组件的步骤开发,这些知识也是必不可少的! PS:学习自某网站(不打广告) 这个小组件相信大家都很熟悉吧,以前的墨迹天气 ...

  7. Gradle 1.12用户指南翻译——第三十五章. Sonar 插件

    本文由CSDN博客万一博主翻译,其他章节的翻译请参见: http://blog.csdn.net/column/details/gradle-translation.html 翻译项目请关注Githu ...

  8. Hadoop RPC

    hadoop rpc机制 && 将avro引入hadoop rpc机制初探 1 RPC RPC(Remote Procedure Call)--远程过程调用,它是一种通过网络从远程计算 ...

  9. Android Studio 错误 Duplicate files copied in APK META-INF/LICENSE.txt解决方案

    My logcat: log Execution failed for task ':Prog:packageDebug'. Duplicate files copied in APK META-IN ...

  10. 存储引擎-Buffered tree

    Buffered-tree 也称为COLA,即cache-oblivious,可以不需要知道具体内存大小和一个块的大小,使用一套逻辑进行处理,因此内存大小可知,内存可能被临时占用去做其它事情. Buf ...