解题思路:本题是一道图的题目,但是无向图,给定的输入是图的各个边,题目中给出一个关键信息(Each node has labels in the set {0, 1, ..., edges.length})可知图中没有环,如果是有环的图这个题是无解的。最好的证明方法就是反正法,如果有环的话标号最大的节点必然小于边的数量,例如[0, 1], [1, 2], [0, 2], 三个节点且三条边,但编号最大的节点小于边的数量。

题目的解法依然是在图上做dfs寻找答案,因为是无向图,对于一个节点来说,可以从任何一个边进入,而从另外的边出去(除去只有一个边的节点,可以称为叶子节点),对于不同的入边,返回的答案是不一样的,对于已经计算过的从当前节点出去的边的最大长度也需要保存起来。

class Solution {
public:
int recursive(vector<vector<int>>& graph, vector<map<int, int>>& memo, vector<bool>& visited, int cur){
int len = 0;
for(int i = 0; i < graph[cur].size(); ++i){
int next = graph[cur][i];
if(!visited[next]){
visited[next] = true;
if(memo[cur][next] == 0){
memo[cur][next] = recursive(graph, memo, visited, next) + 1;
}
len = max(len, memo[cur][next]);
}
}
return len;
} int treeDiameter(vector<vector<int>>& edges) {
const int nodeCnt = edges.size() + 1;
vector<vector<int>> graph(nodeCnt, vector<int>());
vector<map<int, int>> memo(nodeCnt, map<int, int>());
for(auto edge : edges){
graph[edge[0]].push_back(edge[1]);
graph[edge[1]].push_back(edge[0]);
memo[edge[0]][edge[1]] = 0;
memo[edge[1]][edge[0]] = 0;
}
int ans = 0;
for(int i = 0; i < graph.size(); ++i){
if(graph[i].size() == 1){
vector<bool> visited(nodeCnt, false);
visited[i] = true;
ans = max(ans, recursive(graph, memo, visited, i));
}
}
return ans;
}
};

  

1245. Tree Diameter的更多相关文章

  1. Data Structure Binary Tree: Diameter of a Binary Tree

    http://www.geeksforgeeks.org/diameter-of-a-binary-tree/ #include <iostream> #include <vecto ...

  2. 直径问题 Diameter Problems

    2019-11-03 21:37:59 一.Diameter of Binary Tree 问题描述: 问题求解: 解法一.第一反应是树上动归,每个节点保存一下左右的最大深度,最后以每个节点作为中枢计 ...

  3. HourRank 19

    https://www.hackerrank.com/contests/hourrank-19/challenges 第一题略. 第二题是nim博弈,问删掉一个区间的石子,使得先手败的方案有几种,明显 ...

  4. 【CF1146】Forethought Future Cup - Elimination Round

    Forethought Future Cup - Elimination Round 窝也不知道这是个啥比赛QwQ A. Love "A" 给你一个串,你可以删去若干个元素,使得最 ...

  5. Codeforces Forethought Future Cup Elimination Round 选做

    1146C Tree Diameter 题意 交互题.有一棵 \(n(n\le 100)\) 个点的树,你可以进行不超过 \(9\) 次询问,每次询问两个点集中两个不在同一点集的点的最大距离.求树的直 ...

  6. 543. Diameter of Binary Tree

    https://leetcode.com/problems/diameter-of-binary-tree/#/description Given a binary tree, you need to ...

  7. LeetCode 543. Diameter of Binary Tree (二叉树的直径)

    Given a binary tree, you need to compute the length of the diameter of the tree. The diameter of a b ...

  8. [LeetCode] Diameter of Binary Tree 二叉树的直径

    Given a binary tree, you need to compute the length of the diameter of the tree. The diameter of a b ...

  9. [Swift]LeetCode543. 二叉树的直径 | Diameter of Binary Tree

    Given a binary tree, you need to compute the length of the diameter of the tree. The diameter of a b ...

随机推荐

  1. appium-Android的驱动程序

    Appium Android Driver是Android设备的测试自动化工具.Appium Android驱动程序自动化原生的,混合的和移动的Web应用程序,在模拟器,仿真器和真实设备上进行测试.A ...

  2. JMeter 阶梯式加压测试插件 Stepping Thread Group

    在日常性能测试过程中,有时需要对被测对象不断的增加压力,直至达到某个值后,并持续运行一段时间.这里将借助jmeter插件模拟这种情况. 本文介绍在jmeter中,使用插件Stepping Thread ...

  3. 51NOD 1005

    1005 大数加法  基准时间限制:1 秒 空间限制:131072 KB 分值: 0 难度:基础题  收藏  关注 给出2个大整数A,B,计算A+B的结果.   Input 第1行:大数A 第2行:大 ...

  4. 机器学习算法--Elastic Net

    1) alpha : float, optional Constant that multiplies the penalty terms. Defaults to 1.0. See the note ...

  5. 【SQL】链接服务器

    最近做项目,需要对两个数据库进行同步操作,所以采用在Server SQL中建立链接服务器方式实现. 链接服务器,可以直接访问/操作其他服务器上的数据库表. 1.连接SQL Server链接服务器 EX ...

  6. 78、tensorflow滑动平均模型,用来更新迭代的衰减系数

    ''' Created on 2017年4月21日 @author: weizhen ''' #4.滑动平均模型 import tensorflow as tf #定义一个变量用于计算滑动平均,这个变 ...

  7. 关于sql中日期操作

    select * from account where  DAYOFWEEK('2019-11-30') =7 limit 10 DAYOFWEEK对应结果: 周日:1 周一:2 周二:3 周三:4 ...

  8. 文件上传&&验证文件格式

    $(function(){ $(".layui-progress").hide(); $("[data-upload-file]").each(function ...

  9. 2019牛客多校第⑨场J Symmetrical Painting(思维,离散化)

    原题:https://ac.nowcoder.com/acm/contest/889/J 题意: 二维平面上有n个矩形,每个矩形左下角是(i−1,Li)(i−1,Li), 右上角是(i,Ri)(i,R ...

  10. nginx中root与alias关键字的区别

    前言 近段时间秋招上岸了,于是每天疯狂补各种分布式基础,每天都在痛苦与快乐中度过. 在学习 nginx 的时候,遇到配置上的问题:root 与 alias 的区别,卡了大概三个小时,记录下来警醒自己不 ...