这道题的关键在于如何求两个最远的结点,二叉树比较容易一直DFS就能找到,但是普通树就比较麻烦。要先找到一端,再去找另外一端,两端的并集就是答案。因为结点都是对称的,所以两端都是答案。还要注意去重,12 13这种就会重复。

#include<bits/stdc++.h>
using namespace std;
const int maxn = ;
vector<int>pre[maxn];
int n;
bool vis[maxn];
vector<int>ans;
void DFS(int s)
{
int i;
vis[s] = true;
for (i =;i<pre[s].size(); i++)
{
if (vis[pre[s][i]] == false)
{
DFS(pre[s][i]);
}
}
return;
}
vector<int>temp;
int maxh = ;
void find(int s,int height)//寻找以s为根距离s最远的结点,height为s距离该节点的距离.
{
if (vis[s] == true)
return;
vis[s] = true;
if (height > maxh)
{
temp.clear();
temp.push_back(s);
maxh = height;
}
else if (height == maxh)
{
temp.push_back(s);
}
int i;
for (i = ; i < pre[s].size(); i++)
{
find(pre[s][i], height + );
}
}
int main()
{
scanf("%d", &n);
int i, j;
for (i = ; i <= n-; i++)
{
int st, ed;
scanf("%d %d", &st,&ed);
pre[st].push_back(ed);
pre[ed].push_back(st);
}
memset(vis, false, sizeof(vis));
int sum = ;
for (i = ; i <= n; i++)
{
if (vis[i] == false)
{
sum++;
DFS(i);
}
}
if (sum >=)
{
printf("Error: %d components\n", sum);
}
else if (n == )
{
printf("%d\n", );
}
else
{
memset(vis, false, sizeof(vis));
find(, );//此时temp里面已经装了若干边缘结点
ans = temp;
memset(vis, false, sizeof(vis));
maxh = ;
temp.clear();
find(ans[], );
for (i = ; i < temp.size(); i++)
{
ans.push_back(temp[i]);
}
sort(ans.begin(), ans.end());
printf("%d\n", ans[]);
for (i = ; i <ans.size(); i++)
{
if(ans[i]!=ans[i-])
printf("%d\n",ans[i]);
}
}
}

1021 Deepest Root的更多相关文章

  1. PAT 1021 Deepest Root[并查集、dfs][难]

    1021 Deepest Root (25)(25 分) A graph which is connected and acyclic can be considered a tree. The he ...

  2. PAT甲级1021. Deepest Root

    PAT甲级1021. Deepest Root 题意: 连接和非循环的图可以被认为是一棵树.树的高度取决于所选的根.现在你应该找到导致最高树的根.这样的根称为最深根. 输入规格: 每个输入文件包含一个 ...

  3. PAT 甲级 1021 Deepest Root (并查集,树的遍历)

    1021. Deepest Root (25) 时间限制 1500 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue A graph ...

  4. [PAT] 1021 Deepest Root (25)(25 分)

    1021 Deepest Root (25)(25 分)A graph which is connected and acyclic can be considered a tree. The hei ...

  5. PAT 甲级 1021 Deepest Root (25 分)(bfs求树高,又可能存在part数part>2的情况)

    1021 Deepest Root (25 分)   A graph which is connected and acyclic can be considered a tree. The heig ...

  6. 1021. Deepest Root (25)——DFS+并查集

    http://pat.zju.edu.cn/contests/pat-a-practise/1021 无环连通图也可以视为一棵树,选定图中任意一点作为根,如果这时候整个树的深度最大,则称其为 deep ...

  7. 1021.Deepest Root (并查集+DFS树的深度)

    A graph which is connected and acyclic can be considered a tree. The height of the tree depends on t ...

  8. 1021. Deepest Root (25) -并查集判树 -BFS求深度

    题目如下: A graph which is connected and acyclic can be considered a tree. The height of the tree depend ...

  9. 1021. Deepest Root (25)

    A graph which is connected and acyclic can be considered a tree. The height of the tree depends on t ...

  10. PAT 甲级 1021 Deepest Root

    https://pintia.cn/problem-sets/994805342720868352/problems/994805482919673856 A graph which is conne ...

随机推荐

  1. 标准库 os、sys、logging、configparser、time、requests

    os : 与操作系统交互的模块 os.getcwd() 获取当前工作目录,即当前python脚本工作的目录路径 os.chdir("dirname") 改变当前脚本工作目录:相当于 ...

  2. Ubuntu上pip安装uwsgi失败的原因之一(未联网)

    ubuntu@ubuntu:~$ sudo pip install uwsgi 报错:The directory '/home/ubuntu/.cache/pip/http' or its paren ...

  3. 卓越研发之路 MOT技术管理者课堂

    引言:从2018年11月起,在北京.大连.上海.南京.杭州.武汉.成都.西安.深圳.广州等地巡回举办的技术沙龙.活动旨在交流软件研发及互联网技术的实战经验,分享优秀的案例实践,通过平台结识更多友人,挖 ...

  4. NPOI 自定义单元格背景颜色 XSSFWorkbook - Excel

    x 网上找到了,HSSFWorkbook自定义颜色的例子(讲的还挺细致的),但是XSSFWorkbook确没找到...研究了一下,坑掉了一地... NPOI.XSSF.UserModel.XSSFWo ...

  5. [No000015D]【李笑来 笔记整理】个人商业模式升级

    提前知道地图对你到达目标是至关重要的. 比较女性与男性的成本:女性的成本更高(周期性激素分泌波动),所以不要再这个时候和女朋友较劲,她也是身不由己,当你知道这件事情之后,你就会试着去理解她. 下面这张 ...

  6. plsql developer 安装

    PLSQL Developer 安装分以下几步: 一.下载Oracle客户端 PLSQL Developer是通过oracle client连上Oracle server的. http://www.o ...

  7. wpf(windos窗体)

    在windos窗体中可以放置各种控件,以及为控件定义事件等等,而窗体的显示可以通过show方法以及showdialog方法.他们的区别是 show:运行程序的时候弹出新窗体,而该新窗体会一闪而过,最小 ...

  8. Page11:状态反馈、输出反馈的概念及性能比较,极点配置的基本概念、意义及其算法[Linear System Theory]

    内容包含离散时间线性时不变系统的稳定判据 状态反馈.输出反馈的基本概念及其性能比较 极点配置的基本概念.意义及其算法

  9. __dict__和dir()的区别:未完

    1.  dir()是一个函数,返回的是list.__dict__是一个字典,键为属性名,值为属性值: 2.  dir()用来寻找一个对象的所有属性,包括__dict__中的属性,所以说__dict__ ...

  10. mac mysql提示mysql.sock'

    Warning: World-writable config file '/Applications/XAMPP/xamppfiles/etc/my.cnf' is ignored ERROR 200 ...