题目来源:树中的最长路

解题思路:枚举每一个点作为转折点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. VMware历史版本下载【1.0~3.0】

    前提:此为走HTTP协议的FTP伺服器,而且有直到Vmware3.0[之后就没了]的版本 link:http://linux.mathematik.tu-darmstadt.de/pub/linux/ ...

  2. C#数据结构与算法系列(十):逆波兰计算器——逆波兰表达式(后缀表达式)

    1.介绍 后缀表达式又称逆波兰表达式,与前缀表达式相似,只是运算符位于操作数之后 2.举例说明 (3+4)*5-6对应的后缀表达式就是3 4 +5 * 6 - 3.示例 输入一个逆波兰表达式(后缀表达 ...

  3. int与Integer的区别(基本类型与复杂类型的对比)转

    基本类型,或者叫做内置类型,是JAVA中不同于类的特殊类型. Java中的简单类型从概念上分为四种:实数.整数.字符.布尔值.但是有一点需要说明的是,Java里面只有八种原始类型,其列表如下: 实数: ...

  4. js Date format(日期格式化:yyyy-MM-dd HH:mm:ss.S)

    今天在做日期显示的时候,那个显示格式困扰了很久,各种组件都尝试了,总是不如意,最后自己网上找了一个,然后稍微修改一下,感觉这个Util挺常用的,这里mark一下 Date.prototype.form ...

  5. Perl入门(四)Perl的正则表达式

    正则表达式是Perl语言的特色,基本的语法不是很难,但是编写一个符合需求.高效的正则表达式,还是有一些挑战的. Perl的三种匹配模式 1.查找 语法:m/正则表达式内容/; 作用:查找匹配内容中是否 ...

  6. StringEscapeUtils防止xss攻击详解

    StringUtils和StringEscapeUtils这两个实用类. 1.转义防止xss攻击 1.转义可以分为下面的几种情况 第一用户输入特殊字符的时候,在提及的时候不做任何处理保持到数据库,当用 ...

  7. 7、struct2的命名空间

    采用命名空间可以区分不同action下面相同的函数名称 我们来看下面的一个程序的代码 我们来看下面的代码: 添加物料的action处理类: package com.weiyuan.test; publ ...

  8. RabbitMQ:一、入门

    消息中间件 使用消息中间件的作用 解耦 削峰 异步 顺序保证 冗余(存储) RabbitMQ的特点 可靠性 灵活的路由 扩展性 高可用 多语言客户端 插件机制 多协议(主要还是AMQP) 相关概念 P ...

  9. 十位大牛做出的web前端开发规范总结

    Web前端作为开发团队中不可或缺的一部分,需要按照相关规定进行合理编写(一部分不良习惯可能给自己和他人造成不必要的麻烦).不同公司不同团队具有不同的规范和文档.下面是根据不同企业和团队的要求进行全面详 ...

  10. springsecurity简单学习

    一.初识SpringSecurity 在springboot项目中加入spring security. 1.在pom.xml中加入依赖 <dependency> <groupId&g ...