当前位置: 首页 > news >正文

南阳网站推广网站代理合作

南阳网站推广,网站代理合作,公司名称变更网站要重新备案吗,北京通州做网站题目 题目链接#xff1a; https://www.nowcoder.com/practice/a77b4f3d84bf4a7891519ffee9376df3 思路 核心就是树的最大直径(globalMax)一定是以某一个node为root最长的两个path-to-leaf. 就是普通dfs的同时算路径长度。时间: O(n), DFS一次 空间: O(n)参考答案Java impo…题目 题目链接 https://www.nowcoder.com/practice/a77b4f3d84bf4a7891519ffee9376df3 思路 核心就是树的最大直径(globalMax)一定是以某一个node为root最长的两个path-to-leaf. 就是普通dfs的同时算路径长度。时间: O(n), DFS一次 空间: O(n)参考答案Java import java.util.*;/** public class Interval {* int start;* int end;* public Interval(int start, int end) {* this.start start;* this.end end;* }* }*/public class Solution {/*** 代码中的类名、方法名、参数名已经指定请勿修改直接返回方法规定的值即可** 树的直径* param n int整型 树的节点个数* param Tree_edge Interval类一维数组 树的边* param Edge_value int整型一维数组 边的权值* return int整型*/public int solve (int n, Interval[] Tree_edge, int[] Edge_value) {//核心就是树的最大直径(globalMax)一定是以某一个node为root最长的两个path-to-leaf.//就是普通dfs的同时算路径长度。////时间: O(n), DFS一次//空间: O(n)// node_id - { {nei1, cost1}, {nei2, cost2}, ... }MapInteger, Listint[] graph new HashMap();int[] globalMax {0};// construct graphfor (int i 0; i n ; i) {graph.put(i, new ArrayListint[]());}for (int i 0; i n - 1 ; i) {// treat tree edge as bi-directionalint from Tree_edge[i].start;int to Tree_edge[i].end;graph.get(from).add(new int[] {to, Edge_value[i]});graph.get(to).add(new int[] {from, Edge_value[i]});}// 因为edge是bi-directional, 随便从哪个node开始dfs都一样。这里用了node-0.// -1 作为parentdfs(graph, globalMax, 0, -1);return globalMax[0];}// returns the max cost path-to-leaf from this root.public int dfs(MapInteger, Listint[] graph, int[] ans, int root,int parent) {int maxCost 0;int maxCost2 0;for (int[] nexts : graph.get(root)) {// NOTE: BaseCase (i.e. leaf) has only 1 nei, which is the parent// thus leaf will return maxCost 0 directly.if (nexts[0] parent) continue;// recursively finds the max cost path to any leafint cost dfs(graph, ans, nexts[0], root) nexts[1];// keep 2 largest costif (cost maxCost) {maxCost2 maxCost;maxCost cost;} else if(cost maxCost2){maxCost2 cost;}}// check if the 2 largest path is the global maxint curcost maxCost maxCost2;if (curcost ans[0]) {ans[0] curcost;}return maxCost;} }参考答案Go package mainimport . nc_tools/** type Interval struct {* Start int* End int* }*//*** 代码中的类名、方法名、参数名已经指定请勿修改直接返回方法规定的值即可** 树的直径* param n int整型 树的节点个数* param Tree_edge Interval类一维数组 树的边* param Edge_value int整型一维数组 边的权值* return int整型*/ func solve(n int, Tree_edge []*Interval, Edge_value []int) int {//核心就是树的最大直径(globalMax)一定是以某一个node为root最长的两个path-to-leaf.//就是普通dfs的同时算路径长度。////时间: O(n), DFS一次//空间: O(n)// node_id - { {nei1, cost1}, {nei2, cost2}, ... }graph : map[int][]*Node{} //map表示图// construct graphfor i : 0; i n; i {graph[i] []*Node{}}for i : 0; i n-1; i {// treat tree edge as bi-directionalfrom : Tree_edge[i].Startto : Tree_edge[i].Endgraph[from] append(graph[from], Node{to, Edge_value[i]})graph[to] append(graph[to], Node{from, Edge_value[i]})}// 因为edge是bi-directional, 随便从哪个node开始dfs都一样。这里用了node-0.// -1 作为parentans : []int{0}dfs(graph, ans, 0, -1)return ans[0] }func dfs(graph map[int][]*Node, ans *[]int, root, parent int) int {maxCost : 0maxCost2 : 0for _, nexts : range graph[root] {// NOTE: BaseCase (i.e. leaf) has only 1 nei, which is the parent// thus leaf will return maxCost 0 directly.if nexts.to parent {continue}// recursively finds the max cost path to any leafcost : dfs(graph, ans, nexts.to, root) nexts.cost// keep 2 largest costif cost maxCost {maxCost2 maxCostmaxCost cost} else if cost maxCost2 {maxCost2 cost}}// check if the 2 largest path is the global maxcursot : maxCost maxCost2if cursot (*ans)[0] {(*ans)[0] cursot}return maxCost }type Node struct {to intcost int } 参考答案PHP ?php/*class Interval{var $start 0;var $end 0;function __construct($a, $b){$this-start $a;$this-end $b;} }*//*** 代码中的类名、方法名、参数名已经指定请勿修改直接返回方法规定的值即可** 树的直径* param n int整型 树的节点个数* param Tree_edge Interval类一维数组 树的边* param Edge_value int整型一维数组 边的权值* return int整型*/ function solve( $n , $Tree_edge , $Edge_value ) {//核心就是树的最大直径(globalMax)一定是以某一个node为root最长的两个path-to-leaf.//就是普通dfs的同时算路径长度。////时间: O(n), DFS一次//空间: O(n)// node_id - { {nei1, cost1}, {nei2, cost2}, ... }$graph [];for($i0;$i$n;$i){$graph[$i] [];}// construct graphfor($i0;$i$n-1;$i){$from $Tree_edge[$i]-start;$to $Tree_edge[$i]-end;$graph[$from][count($graph[$from])] [$to,$Edge_value[$i]];$graph[$to][count($graph[$to])] [$from,$Edge_value[$i]];}$ans [0];// 因为edge是bi-directional, 随便从哪个node开始dfs都一样。这里用了node-0.// -1 作为parentdfs($graph,$ans,0,-1);return $ans[0]; }// returns the max cost path-to-leaf from this root. function dfs($graph,$ans,$root,$parent){$max1 0;$max2 0;foreach ($graph[$root] as $nexts) {// NOTE: BaseCase (i.e. leaf) has only 1 nei, which is the parent// thus leaf will return maxCost 0 directly.if($nexts[0] $parent) continue; //不走回头路// recursively finds the max cost path to any leaf$cost dfs($graph,$ans,$nexts[0],$root)$nexts[1];// keep 2 largest costif($cost $max1){$max2$max1;$max1 $cost;}else if($cost $max2){$max2 $cost;}}// check if the 2 largest path is the global max$curcost $max1$max2;if($curcost $ans[0]){$ans[0] $curcost;}return $max1; }
http://www.pierceye.com/news/471199/

相关文章:

  • 商务网站建设的组成包括自动链接 wordpress
  • 网站如何关闭东莞网站开发推荐
  • 自己开网站能赚钱吗网站界面设计描述
  • 二手交易网站建设方案ppt网站备案的作用
  • 北京行业网站建设临沂谁会做网站
  • 网站备案 游戏修改wordpress字体
  • 福建微网站建设价格宝山专业网站建设
  • 做采集网站难不关键词做网站名字
  • 怎么做律师事务所的网站用凡科做网站好吗
  • 免费做网站公司ydwzjs政务网站的建设
  • 企业网站设计总结西安做网站哪里便宜
  • wordpress 电影下载站济南最新消息
  • 怎样做企业的网站公司部门解散
  • 三亚中国检科院生物安全中心门户网站建设什么是响应式网站
  • 为什么要建设公司网站怎么制作图片视频和配音乐
  • 建设项目环境影响登记表备案系统网站论坛门户网站开发
  • 铁岭网站建设建设云企业服务平台
  • 响应式网站制作方法泰安明航网络科技有限公司
  • 建设网站需要几级安全等保深圳网站开发招聘
  • 无锡网站建设制作公司甘肃省建设工程网站
  • 广州微信网站建设哪家好公司网站排名优化手段
  • 深圳市路桥建设集团有限公司招标采购网站crntos wordpress
  • 广告网站制作报价深圳建筑设计平台网站
  • 网站ns记录南宁企业建站模板
  • 网站服务建设目前做哪些网站能致富
  • 专业网站定制公司深圳网页制作服务
  • 白云网站(建设信科网络)网页工具在哪里
  • 食品网站策划网站建设送企业邮箱吗
  • 天津自贸区建设局网站手机网站导航设计
  • 企业网站建设制作大连网站建设吗