1021. Deepest Root (25)

时间限制
1500 ms

内存限制
32000 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,其实bfs更容易解该问题。
查找最长根的时候,首先从一点进行深搜,把其中距离最远的点记录下来(可访问到所有顶点,因为已经保证是树),然后从这些最远点中任选一点再进行深度搜索,这个时候的所有最远点都是最长根,关键是第一遍dfs保存的最远点也是最长根,这个地方最难理解了,首先,若第一遍dfs后的最远根不在从1开始的同一条支路上(他们的最近公共祖先是1),这是这些点必定都会是将来的最长根这很明显,若第一遍dfs后的最远根在从1开始的同一条支路上(他们的最近公共祖先不是1记为f),则第二遍dfs时的最长根所在的dfs遍历序列必过f,所以此时的情况又可将f点看做第一种情况中的1节点类似。综上第一次与第二次dfs的合集才是最终结果。
#include <iostream>
#include <cstdio>
#include <map>
#include <vector>
#include <set>using namespace std;const int maxx = 10001;map<int,vector<int> > tree;
set<int> res;
set<int> tres;
int pre[maxx],cnt,maxn = -1;
int visit[maxx];void dfs(int p,int step){if(step>maxn){res.clear();res.insert(p);maxn = step;}else if(step==maxn){res.insert(p);}vector<int>::iterator ite = tree[p].begin();for(;ite!=tree[p].end();++ite){if(visit[*ite]!=1){visit[*ite] = 1;dfs(*ite,step+1);visit[*ite] = 0;}}
}void init(int n){int i;for(i=1;i<=n;++i){pre[i]=i;visit[i]=0;}
}int root(int x){if(x!=pre[x]){pre[x] = root(pre[x]);}return pre[x];
}void merge(int x,int y){int fa = root(x);int fb = root(y);if(fa!=fb){pre[fa] = fb;--cnt;}
}int main(){int n,i,a,b;set<int>::iterator ite;scanf("%d",&n);cnt = n-1;init(n);for(i=1;i<n;++i){scanf("%d %d",&a,&b);tree[a].push_back(b);tree[b].push_back(a);merge(a,b);}if(cnt!=0){printf("Error: %d components\n",1+cnt);return 0;}visit[1] = 1;dfs(1,1);visit[1] = 0;//因为已经是树,所以不会再有环了,所以所有dfs后都恢复该点的可访问性很重要ite = res.begin();for(;ite!=res.end();++ite){tres.insert(*ite);}int point = (*res.begin());visit[point] = 1;dfs(point,1);visit[point] = 0;ite = res.begin();for(;ite!=res.end();++ite){tres.insert(*ite);}ite = tres.begin();for(;ite!=tres.end();++ite){printf("%d\n",*ite);}return 0;
}

浙大PAT 1021. Deepest Root (25)相关推荐

  1. 1021. Deepest Root (25)

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

  2. 【分析】1021 Deepest Root (25 分)【DFS解法】

    立志用最少的代码做最高效的表达 PAT甲级最优题解-->传送门 A graph which is connected and acyclic can be considered a tree. ...

  3. 【PAT甲级】1021 Deepest Root (25 分)(暴力,DFS)

    题意: 输入一个正整数N(N<=10000),然后输入N-1条边,求使得这棵树深度最大的根节点,递增序输出.如果不是一棵树,输出这张图有几个部分. trick: 时间比较充裕数据可能也不是很极限 ...

  4. 1021 Deepest Root (25 分) 【难度: 中 / 知识点: 树的直径 连通块】

    https://pintia.cn/problem-sets/994805342720868352/problems/994805482919673856 方法一: 数组模拟邻接表 第一步: 爆搜df ...

  5. PAT甲级-1021 Deepest Root(25分)

    题目:1021 Deepest Root(25分) 分析:找出以某个节点为根时,最深的根,这题可能会超时要用vector来表示二维数组 疑问:代码一是第二次写的超时了,代码二是第一次写的AC了,找不出 ...

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

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

  7. 【PAT - 甲级1021】Deepest Root (25分)(并查集,暴力枚举)

    题干: A graph which is connected and acyclic can be considered a tree. The height of the tree depends ...

  8. PTA Deepest Root (25分)

    释放无限光明的是人心,制造无边黑暗的也是人心,光明和黑暗交织着,厮杀着,这就是我们为之眷恋又万般无奈的人世间. A graph which is connected and acyclic can b ...

  9. 1021 Deepest Root

    要解决两大问题: 1. 数包含几个连通分量 2. 如何找到最深结点 注意:connected components的意思是连通分量 问题1我用并查集解决 问题2转化为如何得到每个结点的深度 值得注意之 ...

最新文章

  1. spring源码之—Assert.notNull
  2. SystemView软件中“Butterworth Lowpass IIR“的完美设置
  3. __CLASS__ get_class() get_called_class()区别
  4. 2015年国际智慧教育展览会盛大开幕
  5. ysoserial java 反序列化 Groovy1
  6. Dubbo--zookeeper面试中问题解答
  7. 1分钟了解CDN内容分发技术
  8. springcloud的config
  9. Python+Selenium WebDriver API:浏览器及元素的常用函数及变量整理总结
  10. 分数的大小比较优秀教案_人教版小学数学五年级下册异分母分数加、减法公开课优质课课件教案视频...
  11. 重新启动系统中的network服务器,linux系统调优-Network
  12. 架构设计:服务自动化部署和管理流程
  13. C语言入门之C语言开发环境搭建
  14. 2022年湖南省初级审计师考试模拟题及答案
  15. 树莓派4B静态IP与屏幕分辨率设置
  16. linux 设置mail
  17. UnicodeDecodeError: ‘gb2312‘ codec can‘t decode byte 0xe9 in position 5632: illegal multibyte sequen
  18. 硕士论文要不要附matlab程序,论文必须要有附录吗_毕业论文附录一定要写吗_毕业论文中附录是不是必须要写的...
  19. 初学者制作自己的网站详细流程(可以上传自己做的网页)
  20. 卷积和反卷积(deconv)

热门文章

  1. StringBuilder 拼接去掉最后一个逗号
  2. 从零开始:小程序开发环境搭建详解
  3. ncorrect Usage. flag provided but not defined: -rpc
  4. Java修饰符都有什么
  5. 阿里云新增三大高性能计算解决方案,助力生命科学行业快速发展
  6. 网络分级设计模型:核心层、汇聚层和接入层
  7. Win11 OneDrive登录遇到问题0x8004de40
  8. 如何通俗的理解函数的极限_函数的极限问题怎么解释更通俗易懂?初高中数学辅导...
  9. 三年的php简历_【完整模板】PHP工程师简历-简洁橙色-1-3年经验-Word简历模板
  10. Qualcomm 音频学习(Bring up)