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. 通过CSS显示垂直文本

    原文链接: CSS Vertical Text 原文日期: 2014年03月18日 翻译日期: 2014年3月22日 翻译人员: 铁锚 示例地址: http://davidwalsh.name/dem ...

  2. 超过1个G免费资源,16套质量超高风格多样的移动UIKIT

    编者按:前两天发了一篇价值4000元的收费可商用Web 模版,今天来一波同样高质量的的App UI KIT,包括音乐/餐厅/运动等等类型的App,无论是下载来学习还是商用(对的可商用!)都不容错过,@ ...

  3. Android开发中的安全

    根据Android四大框架来解说安全机制 代码安全 java不同于C/C++,java是解释性语言,存在代码被反编译的隐患: 默认混淆器为proguard,最新版本为4.7: proguard还可用来 ...

  4. Java-Cookie源码

    public class Cookie implements Cloneable { private static final String LSTRING_FILE = "javax.se ...

  5. ExtJS:文件上传实例

    ExtJS:文件上传实例 var ext_dateFormat = 'Y-m-d H:i:s'; var dateFormat = 'yyyy-MM-dd HH:mm:ss'; var date = ...

  6. LeetCode之“数学”:Plus One

    题目链接 题目要求: Given a non-negative number represented as an array of digits, plus one to the number. Th ...

  7. windows下mysql免安装配置

    我下载的是mysql-5.5.20-win32.zip版本 1.解压 2.配置环境变量(让系统知道你的bin在哪个位置)path里面设置到安装目录的bin目录 3.复制一个my-huge.ini 另存 ...

  8. C语言实现printf的基本格式输出%d,%c,%p,%s

    关于printf的实现,想必看过我之前发表的文章的伙伴们已经了解了不少基本的知识.好了,接下来不多说了,直接上源码,看看一种简单的实现方式: #include <stdio.h> #def ...

  9. myBatis源码之Executor、BaseExecutor和CachingExecutor

    接下来是mybatis的执行过程,mybatis提供了一个接口Executor,Executor接口主要提供了update.query方法及事物相关的方法接口 /** * @author Clinto ...

  10. Glog 和 Log4cxx 的对比

    转自:http://monkeycn.iteye.com/blog/1021703 #1 Log4cxx有比较完整的配置文档方式,xml和java配置档:GLog只能通过启动程序的时候的输入参数来配置 ...