浙大PAT 1021. Deepest Root (25)
1021. Deepest Root (25)
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
#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)相关推荐
- 1021. Deepest Root (25)
题目链接:http://www.patest.cn/contests/pat-a-practise/1021 题目: 1021. Deepest Root (25) 时间限制 1500 ms 内存限制 ...
- 【分析】1021 Deepest Root (25 分)【DFS解法】
立志用最少的代码做最高效的表达 PAT甲级最优题解-->传送门 A graph which is connected and acyclic can be considered a tree. ...
- 【PAT甲级】1021 Deepest Root (25 分)(暴力,DFS)
题意: 输入一个正整数N(N<=10000),然后输入N-1条边,求使得这棵树深度最大的根节点,递增序输出.如果不是一棵树,输出这张图有几个部分. trick: 时间比较充裕数据可能也不是很极限 ...
- 1021 Deepest Root (25 分) 【难度: 中 / 知识点: 树的直径 连通块】
https://pintia.cn/problem-sets/994805342720868352/problems/994805482919673856 方法一: 数组模拟邻接表 第一步: 爆搜df ...
- PAT甲级-1021 Deepest Root(25分)
题目:1021 Deepest Root(25分) 分析:找出以某个节点为根时,最深的根,这题可能会超时要用vector来表示二维数组 疑问:代码一是第二次写的超时了,代码二是第一次写的AC了,找不出 ...
- PAT甲级1021 Deepest Root :[C++题解]树的最大深度、并查集、dfs求树的深度
文章目录 题目分析 题目链接 题目分析 分析: 考察知识点:并查集.dfs.树的深度 给定n个结点,n-1条边,只要能保证只有1个连通分量,就是一棵树.否则的话就不是树,它是不连通的. 用并查集来看是 ...
- 【PAT - 甲级1021】Deepest Root (25分)(并查集,暴力枚举)
题干: A graph which is connected and acyclic can be considered a tree. The height of the tree depends ...
- PTA Deepest Root (25分)
释放无限光明的是人心,制造无边黑暗的也是人心,光明和黑暗交织着,厮杀着,这就是我们为之眷恋又万般无奈的人世间. A graph which is connected and acyclic can b ...
- 1021 Deepest Root
要解决两大问题: 1. 数包含几个连通分量 2. 如何找到最深结点 注意:connected components的意思是连通分量 问题1我用并查集解决 问题2转化为如何得到每个结点的深度 值得注意之 ...
最新文章
- spring源码之—Assert.notNull
- SystemView软件中“Butterworth Lowpass IIR“的完美设置
- __CLASS__ get_class() get_called_class()区别
- 2015年国际智慧教育展览会盛大开幕
- ysoserial java 反序列化 Groovy1
- Dubbo--zookeeper面试中问题解答
- 1分钟了解CDN内容分发技术
- springcloud的config
- Python+Selenium WebDriver API:浏览器及元素的常用函数及变量整理总结
- 分数的大小比较优秀教案_人教版小学数学五年级下册异分母分数加、减法公开课优质课课件教案视频...
- 重新启动系统中的network服务器,linux系统调优-Network
- 架构设计:服务自动化部署和管理流程
- C语言入门之C语言开发环境搭建
- 2022年湖南省初级审计师考试模拟题及答案
- 树莓派4B静态IP与屏幕分辨率设置
- linux 设置mail
- UnicodeDecodeError: ‘gb2312‘ codec can‘t decode byte 0xe9 in position 5632: illegal multibyte sequen
- 硕士论文要不要附matlab程序,论文必须要有附录吗_毕业论文附录一定要写吗_毕业论文中附录是不是必须要写的...
- 初学者制作自己的网站详细流程(可以上传自己做的网页)
- 卷积和反卷积(deconv)
热门文章
- StringBuilder 拼接去掉最后一个逗号
- 从零开始:小程序开发环境搭建详解
- ncorrect Usage. flag provided but not defined: -rpc
- Java修饰符都有什么
- 阿里云新增三大高性能计算解决方案,助力生命科学行业快速发展
- 网络分级设计模型:核心层、汇聚层和接入层
- Win11 OneDrive登录遇到问题0x8004de40
- 如何通俗的理解函数的极限_函数的极限问题怎么解释更通俗易懂?初高中数学辅导...
- 三年的php简历_【完整模板】PHP工程师简历-简洁橙色-1-3年经验-Word简历模板
- Qualcomm 音频学习(Bring up)