题目:

A graph which is connected and acyclic can be considered a tree. The hight 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 componentswhere 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 #include<iostream>
 2 #include<cstring>
 3 #include<vector>
 4 #include<algorithm>
 5 using namespace std;
 6 struct Node{
 7     vector<int> points;
 8 }nodes[10001];
 9
10 bool visited[10001];
11 int n;
12 int height=0,components=0;
13 vector<int> farthestNode;
14
15 void dfs(int root,int level){
16     visited[root]=true;
17     if(level>height){        //若高度更高,则原本的集合清空,将最高的更新
18         height=level;
19         farthestNode.clear();
20         farthestNode.push_back(root);
21     }else if(level==height){    //同等高度,将该结点添加进去
22         farthestNode.push_back(root);
23     }
24     for(int i=0;i<nodes[root].points.size();i++){
25         if(visited[nodes[root].points[i]]==false){    //对子结点进行深搜
26             dfs(nodes[root].points[i],level+1);
27         }
28     }
29 }
30
31 int main(){
32     cin>>n;
33     if(n==1){
34         cout<<"1";
35         return 0;
36     }
37     for(int i=1;i<n;i++){
38         int a,b;
39         cin>>a>>b;
40         nodes[a].points.push_back(b);
41         nodes[b].points.push_back(a);
42     }
43     memset(visited,0,sizeof(visited));
44     for(int i=1;i<=n;i++){
45         if(visited[i]==false){
46             visited[i]=true;
47             dfs(i,0);
48             components++;
49         }
50     }
51     if(components>1){
52         cout<<"Error: "<<components<<" components";
53     }else{
54         int k=farthestNode[0];    //距离最远的点
55         farthestNode.clear();
56         height=0;
57         memset(visited,0,sizeof(visited));
58         dfs(k,0);        //从最远点再次进行深搜,得到的点全为deepest root
59         farthestNode.push_back(k);    //k也为deepest root
60         sort(farthestNode.begin(),farthestNode.end());
61         for(int i=0;i<farthestNode.size();i++){
62             cout<<farthestNode[i]<<endl;
63         }
64     }
65     return 0;
66 }

 

转载于:https://www.cnblogs.com/orangecyh/p/10364620.html

PTA-1021—— Deepest Root(最后两组数据错误)相关推荐

  1. PAT甲级1021 Deepest Root :[C++题解]树的最大深度、并查集、dfs求树的深度

    文章目录 题目分析 题目链接 题目分析 分析: 考察知识点:并查集.dfs.树的深度 给定n个结点,n-1条边,只要能保证只有1个连通分量,就是一棵树.否则的话就不是树,它是不连通的. 用并查集来看是 ...

  2. 1021. Deepest Root (25)

    题目链接:http://www.patest.cn/contests/pat-a-practise/1021 题目: 1021. Deepest Root (25) 时间限制 1500 ms 内存限制 ...

  3. R语言plotly可视化:plotly可视化分裂的分组小提琴图、每个小提琴图内部分为两组数据、每个分组占小提琴图的一半(Split violin plot in R with plotly)

    R语言plotly可视化:plotly可视化分裂的分组小提琴图.每个小提琴图内部分为两组数据.每个分组占小提琴图的一半(Split violin plot in R with plotly) 目录

  4. excel 两组数据交点_初识Python 数据可视化

    ✨  冒号说:发着小光小热的小点点 ✨    "一图胜千言."          ----Arthur Brisbane 听说这个最近很火!在这个信息爆炸的时代,科技虽然使得数据获 ...

  5. 检验两组数据是否显著差异_SPSS非参数两个相关样本检验

    01 原理与方法 两个相关样本检验的方法主要有:Wilcoxon检验.Sign(符号)检验.McNemar检验和Marginal Homogeneity(边际同质性)检验等. Sign(符号)检验 配 ...

  6. python代码大全p-基于python实现计算两组数据P值

    我们在做A/B试验评估的时候需要借助p_value,这篇文章记录如何利用python计算两组数据的显著性. 一.代码 # TTest.py # -*- coding: utf-8 -*- ''' # ...

  7. excel 两组数据交点_30秒即可完成Excel数据对比,超高效率,快学起来不要犹豫!...

    在工作中,我们很经常需要比对两组甚至以上的Excel数据是否一致,如果数据少的话我们还能够慢慢看,数据要是对的话,用肉眼去看的话恐怕眼睛就可以不要了. 今天小编为大家带来五个能帮我们快速对比Excel ...

  8. datagrid出现相同两组数据_stata 数据操作基础知识:以一篇论文数据操作为例

    stata 数据操作基础知识:以一篇论文数据操作为例 上节回顾及问题 统计学学习大图景 数据描述 分位数回归 存在的问题: 1.学了就要多使用,哪怕生搬硬套也要多用 2.时间序列的方法,大家可以操作, ...

  9. datagrid出现相同两组数据_数据分析之统计学

    统计学知识 思维导图 第一节 统计学基本原理 数据分析相关概念 一.描述统计 测量尺度 1.定类(nominal) 功能:分类的作用,比如性别 2.定序(ordinal) 功能:分类.排序的作用,比如 ...

最新文章

  1. Android Studio如何用真机调试
  2. 多重线性回归 多元线性回归_了解多元线性回归
  3. 团队管理(3)---有效降低企业员工离职率
  4. linux关闭网卡休眠_CentOS_Linux常用实用指令整理三:高级指令
  5. MFC 序列化的理解及困惑点
  6. [JNI]开发之旅(4)项目架构介绍
  7. 苹果激活锁怎么解除?手把手教你关闭激活锁
  8. iOS录音及播放全解
  9. 《华为工作法》学习笔记
  10. Wunderlist 云端任务管理(Todo list)工具
  11. edz文件怎么导入EPLAN Electric P8
  12. 预测科技未来发展趋势的10个定律
  13. 如何区分网线是几类的_5类、6类网线双绞线如何区分又怎么样使用?
  14. 赴日软件工程师,据说很火
  15. Windows如何安装mysql
  16. 【网络】为什么百度查到的ip和ipconfig查到的不一样;详解公网Ip和私网ip;详解网络分类ABC;
  17. 格雷通路 算法 java,Java算法与数据结构教程
  18. mysql 多表 结构相同 查询
  19. REORG TABLE命令优化数据库性能
  20. JetBrains IDEA 2019.3 plugins|插件 搜索不到任何东西

热门文章

  1. 谷歌开源 Kotlin 版本 gRPC
  2. 使用Upida/Jeneva.Net验证传入的JSON
  3. 微软发布 PowerToys 首个预览版,重启的 Windows 工具集
  4. SQL Server:查找周开始和结束日期时间
  5. 使用EntityFramework Core和Enums作为字符串的ASP.NET Core Razor页面——第三部分
  6. 微博粉丝精灵_腾讯与精灵宝可梦公司宣布合作开发新游戏
  7. 偏微分方程的正问题和逆问题(inverse problem)
  8. 小程序分享如何自定义封面?
  9. 金蝶kis仓库管理系统演示_金蝶KIS专业版生产管理的系统亮点功能
  10. Execution default of goal org.springframework.boot:spring-boot-maven-plugin