1245. Tree Diameter
解题思路:本题是一道图的题目,但是无向图,给定的输入是图的各个边,题目中给出一个关键信息(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的更多相关文章
- Data Structure Binary Tree: Diameter of a Binary Tree
http://www.geeksforgeeks.org/diameter-of-a-binary-tree/ #include <iostream> #include <vecto ...
- 直径问题 Diameter Problems
2019-11-03 21:37:59 一.Diameter of Binary Tree 问题描述: 问题求解: 解法一.第一反应是树上动归,每个节点保存一下左右的最大深度,最后以每个节点作为中枢计 ...
- HourRank 19
https://www.hackerrank.com/contests/hourrank-19/challenges 第一题略. 第二题是nim博弈,问删掉一个区间的石子,使得先手败的方案有几种,明显 ...
- 【CF1146】Forethought Future Cup - Elimination Round
Forethought Future Cup - Elimination Round 窝也不知道这是个啥比赛QwQ A. Love "A" 给你一个串,你可以删去若干个元素,使得最 ...
- Codeforces Forethought Future Cup Elimination Round 选做
1146C Tree Diameter 题意 交互题.有一棵 \(n(n\le 100)\) 个点的树,你可以进行不超过 \(9\) 次询问,每次询问两个点集中两个不在同一点集的点的最大距离.求树的直 ...
- 543. Diameter of Binary Tree
https://leetcode.com/problems/diameter-of-binary-tree/#/description Given a binary tree, you need to ...
- 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 ...
- [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 ...
- [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 ...
随机推荐
- Losing session data in ASP.NET
Losing session data in ASP.NET By default Response.Redirect terminates thread execution and there mi ...
- Windows IO System
Windows IO System是由一些executive components组成,这些component可以认为是ntoskrnl.exe中相对独立的一些module. 整个IO System是 ...
- Rust <8>:lifetime 高级语法与 trait 关联绑定
一.生命周期关联:如下声明表示,'s >= 'c struct Parser<'c, 's: 'c> { context: &'c Context<'s>, } ...
- shell编程2:数组的运用
Shell 数组 定义数组 在Shell中,用括号来表示数组,数组元素用"空格"符号分割开.定义数组的一般形式为: name=(name1 name2 name3) 复制代码 还可 ...
- 把我坑惨的一个MySQL双引号!
来源:For DBA www.fordba.com/mysql-double-quotation-marks-accident.html 一.前言 最近经常碰到开发误删除误更新数据,这不,他们又给我找 ...
- SpringCloud-技术专区-Zuul-使用指南
Zuul作为微服务系统的网关组件,用于构建边界服务,致力于动态路由.过滤.监控.弹性伸缩和安全. Zuul功能 认证 压力测试 金丝雀测试 动态路由 负载削减 安全 静态响应处理 主动/主动交换管理 ...
- 那些年踩过的eleUl上传图片的坑?
html : <el-upload :headers="header" // 请求头参数(一般包含token,认证参数authorization) :data="u ...
- 多线程实现奇偶统计v1 - 暴力版
#include <stdio.h> #include <stdlib.h> #include <time.h> #include "pthread.h& ...
- LLppdd never give up!
LLppdd never give up! Time Limit: 1 s Memory Limit: 256 MB 题目背景 LLppdd是个被毒害的小朋友,他的初中生涯充满了坎坷. 直到初三的某一 ...
- wrong mechandise category
Issue: cannot open masterdata for this two UPCs since the error below is displayed, but I checked an ...