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. spring揭秘 读书笔记 一 IoC初探

    本文是王福强所著<<spring揭秘>>一书的读书笔记 ioc的基本概念 一个例子 我们看下面这个类,getAndPersistNews方法干了四件事 1 通过newsList ...

  2. [面试算法题]有序列表删除节点-leetcode学习之旅(4)

    问题描述 Write a function to delete a node (except the tail) in a singly linked list, given only access ...

  3. 网站开发进阶(二十六)js刷新页面方法大全

    js刷新页面方法大全 在项目开发过程中,需要实现刷新页面.经过学习,发现下面这条语句就可以轻松实现. location.reload(); // 刷新页面 有关刷新页面的其它方法,具体学习内容如下,有 ...

  4. LeetCode之“树”:Binary Tree Preorder && Inorder && Postorder Traversal

    Binary Tree Preorder Traversal 题目链接 题目要求: Given a binary tree, return the preorder traversal of its ...

  5. 【Visual C++】游戏编程学习笔记之七:键盘输入消息

     本系列文章由@二货梦想家张程 所写,转载请注明出处. 作者:ZeeCoder  微博链接:http://weibo.com/zc463717263 我的邮箱:michealfloyd@126.c ...

  6. Android Studio使用Lint进行代码检查

    Android Studio目前已经更新到1.4版本,它作为Google官方推荐的IDE,功能非常强大,其中提供了一套静态代码分析工具,它可以帮助我们检查项目中存在的问题,让我们更有规范性的开发App ...

  7. PS图层混合算法之一(不透明度,正片叠底,颜色加深,颜色减淡)

    下列公式中,A代表了上面图层像素的色彩值(A=像素值/255),B代表下面图层像素的色彩值(B=像素值/255),C代表了混合像素的色彩值(真实的结果像素值应该为255*C).该公式也应用于层蒙板. ...

  8. android 开发从入门到精通

    Android-Tips This is an awesome list of tips for android. If you are a beginner, this list will be t ...

  9. 修改input属性placeholder的样式

    <!doctype html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  10. Next Permutation 下一个排列

    Implement next permutation, which rearranges numbers into the lexicographically next greater permuta ...