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 ...
随机推荐
- 使用 git log、git diff 命令时出现 ESC[33 和 ESC[m 乱码的解决办法
经过搜索之后了解到,出现该问题的原因是 git 使用的默认分页程序是 less,而默认的直接运行 less 的话,会无法正确解析转义字符.但是如果以 -r 命令来运行 less 的话,就可以解决了.故 ...
- 高效办公必不可少的5个Excel技巧
1.输入“001.002…”的编号 想要快速给表格添加上“001.002…”这样的编号,你可以这样做: 选择所有单元格——右键点击[设置单元格格式]——点击[文本]——点击[确定]即可. 2.单元格内 ...
- easyui datagrid 去掉外边框及行与行之间的横线标题字体
这是以前写的一个项目中写的东西,为了让datagrid样式好看,所有做的这个处理: 今天同事又问到于是记录下来 $('#id').datagrid({ width: '99%', height: 15 ...
- 口语详解|为什么“how to say”是错的?
你有没有说过一些印象深刻的中式英语呢?为什么有的英语会被称之为中式英语想必你大概知道,但是如何把中式英语使用正确你知道吗?今天,跟着小编来看看吧.By the way,今天的主角是"how ...
- Chrome浏览器如何调试移动端网页信息
Chrome浏览器如何调试移动端网页信息 2017年08月12日 12:42:20 阅读数:835 最近在弄项目,用WebView加载一个页面,想追踪页面中一个按钮的点击事件.这个可能就需要调试这个页 ...
- linux命令瞎记录find xargs
1.创建多个文件 touch test{0..100}.txt 2.重定向 “>>” 追加重定向,追加内容,到文件的尾部 “>” 重定向,清除原文件里面所有内容,然后把内容追加到文件 ...
- ios-静态库,动态库,framework浅析(一)
一,所谓的“库” * 所谓的“库” 库(Library)说白了就是一段编译好的二进制代码,加上头文件就可以供别人使用.什么时候我们会用到库呢? 一种情 ...
- innodb表锁情况
MySQL InnoDB默认行级锁.行级锁都是基于索引的 行级锁变为表级锁情况如下: 1.如果一条SQL语句用不到索引是不会使用行级锁的,会使用表级锁把整张表锁住. 2.表字段进行变更. 3.进行整表 ...
- sqlserver binary varbinary image 的区别
sqlserver binary varbinary image 的区别 binary 固定长度的二进制数据,其最大长度为 8,000 个字节. varbinary 可变长度的二进制数 ...
- tensorflow入门笔记(一) tf.app.flags.FLAGS
tf.app.flags.DEFINE_xxx()就是添加命令行的optional argument(可选参数),而tf.app.flags.FLAGS可以从对应的命令行参数取出参数.举例如下: FL ...