树的直径
文章平均质量分 72
阿.荣.
勿弃
展开
-
牛客 NC99 树的直径
描述 给定一棵树,求出这棵树的直径,即树上最远两点的距离。 包含n个结点,n-1条边的连通图称为树。 示例1的树如下图所示。其中4到5之间的路径最长,是树的直径,距离为5+2+4=11 示例1 输入: 6,[[0,1],[1,5],[1,2],[2,3],[2,4]],[3,4,2,1,5] 返回值: 11 思路: 定义 简单路径:路径上的所有点至多只访问了一次。 树的直径:树的最长的简单路径。 算法 (1)两次BFS/DFS:先任选一个起点,BFS/DFS找到最长路的终点,再从终点进行第二次BFS原创 2021-09-07 17:28:09 · 567 阅读 · 1 评论 -
poj 1985 Cow Marathon (树的直径 模板题)
Cow Marathon Time Limit: 2000MS Memory Limit: 30000K Total Submissions: 5064 Accepted: 2464 Case Time Limit: 1000MS Description After hearing about the epidemi原创 2016-08-02 23:27:49 · 498 阅读 · 0 评论 -
poj 2631 Roads in the North (树的直径模板题)
Roads in the North Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 2667 Accepted: 1313 Description Building and maintaining roads among communities in th原创 2016-08-02 23:33:03 · 211 阅读 · 0 评论 -
poj 1383 Labyrinth (迷宫 最长路)
Labyrinth Time Limit: 2000MS Memory Limit: 32768K Total Submissions: 4301 Accepted: 1623 Description The northern part of the Pyramid contains a very large and comp原创 2016-08-02 23:39:49 · 291 阅读 · 0 评论