题解 | #树上上升序列#

树上上升序列

http://www.nowcoder.com/questionTerminal/8eda05798f8c486a9f954432515ae436

#大佬解答下为何有部分算例不能通过

import java.util.LinkedList; import java.util.List; import java.util.Scanner;

import java.util.LinkedList; import java.util.List; import java.util.Scanner;

public class Main { static int res; static int n; static int[] weights; public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int n = scanner.nextInt(); weights = new int[n+1]; List[] graph = new LinkedList[n+1]; for (int i = 1; i <= n; i++) { graph[i] =new LinkedList(); weights[i] = scanner.nextInt(); } for (int i = 0; i < n-1; i++) { graph[scanner.nextInt()].add(scanner.nextInt()); } for (int i = 1; i <=n; i++) { dfs(i,graph,1); } System.out.println(res); } public static void dfs(int s, List[]graph,int step){ res = Math.max(res,step); for(int p:graph[s]){ if(weights[p]>weights[s]){ int newstp = step+1; dfs(p,graph, step+1); } }

全部评论

相关推荐

不愿透露姓名的神秘牛友
07-15 17:46
暑期就挂了,秋招还有机会吗
大聪明777:研发提前批,14号刚开的,官网上面的配图上有写。提前批没过的话,秋招还可以投,不过前面的笔试/面试记录会被保留,供秋招参考
26届校招投递进展
点赞 评论 收藏
分享
06-08 22:25
门头沟学院 Java
从零开始的转码生活:这hr不会打开手机不分青红皂白给所有人群发这句话,过一会再给所有人再发一遍,这肯定会有重复的,不管,再过一会再发一遍
点赞 评论 收藏
分享
05-27 14:57
西北大学 golang
强大的社畜在走神:27届真不用急,可以搞点项目、竞赛再沉淀沉淀,我大二的时候还在天天打游戏呢
投递华为等公司10个岗位
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务