题目来源:树中的最长路

解题思路:枚举每一个点作为转折点t,求出以t为根节点的子树中的‘最长路’以及与‘最长路’不重合的‘次长路’,用这两条路的长度之和去更新答案,最终的答案就是这棵树的最长路长度。只要以类似后序遍历的方式依次访问每个结点,从下往上依次计算每个结点的first值和second值,就能够用O(N)的时间复杂度来解决这个问题。

具体算法(java版,可以直接AC)

 import java.util.*;

 public class Main {

     public class Node {
public Node parent;//父节点
public List<Node> children;//子节点
public int first; //最长路
public int second;//次长路
public int val; public Node(int val, Node parent) {
this.val = val;
this.first = 0;
this.second = 0;
this.parent = parent;
this.children = new ArrayList<Node>();
} //更新节点的first和second
public void update() {
if (this.children.size() == 0) {//叶节点
this.first = this.second = 0;
} else if (this.children.size() == 1) {//只有一个子节点
this.first = this.children.get(0).first + 1;
this.second = 0;
} else {//大于等于2个子节点
int[] array = new int[this.children.size()];
for (int i = 0; i < this.children.size(); i++) {
array[i] = this.children.get(i).first;
}
Arrays.sort(array);
this.first = array[array.length - 1] + 1;
this.second = array[array.length - 2] + 1;
}
} //更新所有节点的first和second(在第一次建立树时调用)
public void updateAll() {
for (Node child : this.children) {
child.updateAll();
}
this.update();
}
} public int n;
public int index;
public int max;
public Node[] nodeMap; public Main(Scanner scanner, int n) {
this.n = n;
this.nodeMap = new Node[this.n + 1];
for (int i = 0; i < n - 1; i++) {
this.create(scanner.nextInt(), scanner.nextInt());
}
this.index = 1;
this.nodeMap[this.index].updateAll();//更新所有的节点
this.max = this.nodeMap[this.index].first
+ this.nodeMap[this.index].second;
this.index++;
} //创建树
private void create(int from, int to) {
Node parent = this.nodeMap[from];
Node child = this.nodeMap[to];
if (parent == null) {
parent = new Node(from, null);
this.nodeMap[from] = parent;
}
if (child == null) {
child = new Node(to, parent);
this.nodeMap[to] = child;
}
child.parent = parent;
parent.children.add(child);
} //将下标为i的节点设置为根节点
private void setRoot(int i) {
Node cur = this.nodeMap[i];
Node parent = cur.parent;
if (parent != null) {//如果存在父节点
parent.children.remove(cur);//从父节点中删除子节点
this.setRoot(parent.val);//递归计算父节点
cur.children.add(parent);//将父节点变成子节点
parent.parent = cur;
}
cur.update();//更新当前节点
} public void solve() {
while (this.index <= this.n) {
this.setRoot(this.index);
this.nodeMap[this.index].parent = null;//根节点的parent设置为null,否则出现死循环
int sum = this.nodeMap[this.index].first
+ this.nodeMap[this.index].second;
this.index++;
this.max = this.max > sum ? this.max : sum;//更新max
}
} public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int N = scanner.nextInt();
Main main = new Main(scanner, N);
main.solve();
System.out.println(main.max);
} }

hihoCoder 1050 树中的最长路 最详细的解题报告的更多相关文章

  1. hihocoder 1050 树中的最长路(动态规划,dfs搜索)

    hihocoder 1050 树中的最长路(动态规划,dfs搜索) Description 上回说到,小Ho得到了一棵二叉树玩具,这个玩具是由小球和木棍连接起来的,而在拆拼它的过程中,小Ho发现他不仅 ...

  2. hihocoder#1050 : 树中的最长路(树中最长路算法 两次BFS找根节点求最长+BFS标记路径长度+bfs不容易超时,用dfs做TLE了)

    #1050 : 树中的最长路 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 上回说到,小Ho得到了一棵二叉树玩具,这个玩具是由小球和木棍连接起来的,而在拆拼它的过程中, ...

  3. 题解报告:hihoCoder #1050 : 树中的最长路

    描述 上回说到,小Ho得到了一棵二叉树玩具,这个玩具是由小球和木棍连接起来的,而在拆拼它的过程中,小Ho发现他不仅仅可以拼凑成一棵二叉树!还可以拼凑成一棵多叉树——好吧,其实就是更为平常的树而已. 但 ...

  4. hihoCoder #1050 : 树中的最长路

    题意: 求出树上最长路径的长度,并返回. 思路: 刚看到数据<=10^5,假如是单分支的树,那么有5万层,就不能递归,那就用桟实现, 那就要将长度信息保存在另开的数组中,很麻烦!!这题专门给递归 ...

  5. hiho #1050 : 树中的最长路 树的直径

    #1050 : 树中的最长路 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 上回说到,小Ho得到了一棵二叉树玩具,这个玩具是由小球和木棍连接起来的,而在拆拼它的过程中, ...

  6. [HIHO] 1050 树中的最长路

    #1050 : 树中的最长路 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 上回说到,小Ho得到了一棵二叉树玩具,这个玩具是由小球和木棍连接起来的,而在拆拼它的过程中, ...

  7. hihoCoder week11 树中的最长路

    题目链接: https://hihocoder.com/contest/hiho11/problem/1 求树中节点对 距离最远的长度 #include <bits/stdc++.h> u ...

  8. HihoCoder第十一周:树中的最长路

    #1050 : 树中的最长路 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 上回说到,小Ho得到了一棵二叉树玩具,这个玩具是由小球和木棍连接起来的,而在拆拼它的过程中, ...

  9. hihoCoder#1050(树中最长路)

    时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 上回说到,小Ho得到了一棵二叉树玩具,这个玩具是由小球和木棍连接起来的,而在拆拼它的过程中,小Ho发现他不仅仅可以拼凑成一 ...

随机推荐

  1. @loj - 3046@「ZJOI2019」语言

    目录 @description@ @solution@ @accepted code@ @details@ @description@ 九条可怜是一个喜欢规律的女孩子.按照规律,第二题应该是一道和数据 ...

  2. 【大厂面试08期】谈一谈你对HashMap的理解?

    摘要 HashMap的原理也是大厂面试中经常会涉及的问题,同时也是工作中常用到的Java容器,本文主要通过对以下问题进行分析讲解,来帮助大家理解HashMap的原理. 1.HashMap添加一个键值对 ...

  3. MFC exe文件生成的图标更改方法

    MFC exe文件生成的图标更改方法 https://blog.csdn.net/txwtech/article/details/92980545

  4. 套接字TCP控制台服务器程序代码示范

    套接字TCP控制台服务器程序代码示范  https://blog.csdn.net/txwtech/article/details/90344081

  5. python的坑--你知道吗?

    python的坑--你知道吗? 1.列表的坑 坑的地方是:因为列表用pop之后,后面的索引都会自动减一 # 列表的坑之一 list1 = ['python','java','php','c','c++ ...

  6. vue通过属性绑定为元素设置class样式

    第一种方式,直接传递一个数组,注意使用v-bind绑定             <h1 :class="['red','size'">这是一个h1文本</h1&g ...

  7. android handle详解3 ThreadHandler

    在android handle详解2的基础上,我们来学习ThreadHandler ThreadHandler的本质就是对android handle详解2的实现 HandlerThread其实还是一 ...

  8. Ubuntu k80深度学习环境搭建

    英伟达驱动安装 英伟达驱动下载:https://www.nvidia.cn/Download/driverResults.aspx/135493/cn/ 由于是驱动的冲突,那么自然是要杀掉和显卡结合不 ...

  9. Spring 容器的初始化

    读完这篇文章你将会收获到 了解到 Spring 容器初始化流程 ThreadLocal 在 Spring 中的最佳实践 面试中回答 Spring 容器初始化流程 引言 我们先从一个简单常见的代码入手分 ...

  10. Yolo车辆检测+LaneNet车道检测

    Yolo车辆检测+LaneNet车道检测 源代码:https://github.com/Dalaska/Driving-Scene-Understanding/blob/master/README.m ...