解题思路:本题是一道图的题目,但是无向图,给定的输入是图的各个边,题目中给出一个关键信息(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. gene network analysis

      基因表达分析包括3个层次[68], 首先是单基因水平, 即比较对照组与实验组的每个基因是否存在表达差异, 这主要指差异基因表达分析; 其次是多基因水平, 如按照基因的共同功能.相互作用.共同表达等 ...

  2. nucleus plus代码学习

    int.S: ;************************************************************************ ;* ;* FUNCTION ;* ; ...

  3. jdbc 事物

    package transaction; import jdbc.utils.*; import java.sql.Connection; import java.sql.PreparedStatem ...

  4. Python基础教程(008)--第一个Python程序

    前言: 学会第一个Python程序 了解Python2和Python3的区别 内容 执行Python程序的三种方式 解释器--Python.Python3 交互式-- ipthon 集成开发环境--P ...

  5. delphi 监控文件系统

    elphi 监控文件系统 你是否想为你的Windows加上一双眼睛,察看使用者在机器上所做的各种操作(例如建立.删除文件:改变文件或目录名字)呢? 这里介绍一种利用Windows未公开函数实现这个功能 ...

  6. 2018icpc南京/gym101981 A Adrien and Austin 博弈

    题意: n个连续排列的石子,每次只许拿连续的(中间没有空格)的k个,问你谁必胜 题解: 简单博弈,特判总数为0,k=1两种情况,其他情况先拿必胜,方法是拿掉中间的,然后对方怎么拿你镜面拿就行. #in ...

  7. MCS-51系列单片机和MCS-52系列单片机有何异同

    MSC-51:1,片内4K字节程序存储器:2,片内128字节数据存储器:3,片内2个16位硬件定时器/计数器.MSC-52: 1,片内8K字节程序存储器:2,片内256字节数据存储器:3,片内3个16 ...

  8. Oracle架构实现原理、含五大进程解析(图文详解)

    目录 目录 前言 Oracle RDBMS架构图 内存结构 系统全局区SGA 高速缓存缓冲区数据库缓冲区 日志缓冲区 共享池 其他结构 进程结构 用户连接进程 用户进程User Process Ser ...

  9. mac查看python安装路径

    1.terminal : input: which Python 或者 which Python3 2.terminal: input : python  --->import sys  --- ...

  10. Windows环境下Oracle数据库的自动备份脚本自动删除30天前的备份

    @echo off echo ================================================ echo Windows环境下Oracle数据库的自动备份脚本 echo ...