1021 Deepest Root
这道题的关键在于如何求两个最远的结点,二叉树比较容易一直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的更多相关文章
- PAT 1021 Deepest Root[并查集、dfs][难]
1021 Deepest Root (25)(25 分) A graph which is connected and acyclic can be considered a tree. The he ...
- PAT甲级1021. Deepest Root
PAT甲级1021. Deepest Root 题意: 连接和非循环的图可以被认为是一棵树.树的高度取决于所选的根.现在你应该找到导致最高树的根.这样的根称为最深根. 输入规格: 每个输入文件包含一个 ...
- PAT 甲级 1021 Deepest Root (并查集,树的遍历)
1021. Deepest Root (25) 时间限制 1500 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue A graph ...
- [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 ...
- 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 ...
- 1021. Deepest Root (25)——DFS+并查集
http://pat.zju.edu.cn/contests/pat-a-practise/1021 无环连通图也可以视为一棵树,选定图中任意一点作为根,如果这时候整个树的深度最大,则称其为 deep ...
- 1021.Deepest Root (并查集+DFS树的深度)
A graph which is connected and acyclic can be considered a tree. The height of the tree depends on t ...
- 1021. Deepest Root (25) -并查集判树 -BFS求深度
题目如下: A graph which is connected and acyclic can be considered a tree. The height of the tree depend ...
- 1021. Deepest Root (25)
A graph which is connected and acyclic can be considered a tree. The height of the tree depends on t ...
- PAT 甲级 1021 Deepest Root
https://pintia.cn/problem-sets/994805342720868352/problems/994805482919673856 A graph which is conne ...
随机推荐
- Nordic NRF51822 从零开始系列(外部设备片—MPU6050DMP驱动的移植)
一.硬件准备 (1)开发板和软件参看 Nordic NRF51822 从零开始系列(一)开发环境的搭建 (2)mpu6050模块 二.前置知识 ...
- 转:关于ROWNUM的使用
转载自:原文:https://blog.csdn.net/songsenkeji/article/details/4432942 ROWNUM的概念ROWNUM是一个虚假的列.它将被分配为 1,2,3 ...
- 分布式文件系统HDFS,大数据存储实战(一)
本文进行了以下工作: OS中建立了两个文件,文件中保存了几组单词. 把这两个文件导入了hadoop自己的文件系统. 介绍删除已导入hadoop的文件和目录的方法,以便万一发生错误时使用. 使用列表命令 ...
- BZOJ 1001 - 狼抓兔子 - [Dinic最大流][对偶图最短路]
题目链接:https://www.lydsy.com/JudgeOnline/problem.php?id=1001 Description现在小朋友们最喜欢的"喜羊羊与灰太狼", ...
- [No0000179]改善C#程序的建议2:C#中dynamic的正确用法
dynamic是FrameWork4.0的新特性.dynamic的出现让C#具有了弱语言类型的特性.编译器在编译的时候不再对类型进行检查,编译期默认dynamic对象支持你想要的任何特性.比如,即使你 ...
- 最详细最全的redis配置文件解释
转载自:http://www.cnblogs.com/zhang-ke/p/5981108.html #redis.conf# Redis configuration file example.# . ...
- http协议-https协议-相对协议
http协议.https协议.相对协议 http://www.baidu.com (使用http协议访问百度) https://wwww.badu.com (使用https协议访问百度) //www. ...
- Flash builder 、flash cs6、 as 3.0研究
1.Flash/Actionscript3 载入资源文件方法考 http://zengrong.net/post/1107.htm 2.使用Flash Professional CS5和Flash B ...
- 最全的MonkeyRunner自动化测试从入门到精通(1)
一.环境变量的配置 1.JDK环境变量的配置 步骤一:在官网上面下载jdk,JDK官网网址: http://www.oracle.com/technetwork/java/javase/downloa ...
- oracle执行计划相关
执行计划相关 根据Operation缩进来判断,缩进最多的最先执行:(缩进相同时,最上面的最先执行) 同一级如果某个动作没有子ID就最先执行 同一级的动作执行时遵循最上最右先执行的原则 TABLE A ...