题解 | #树上上升序列#

树上上升序列

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); } }

全部评论

相关推荐

10-07 20:48
门头沟学院 Java
听说改名就会有offer:可能是实习上着班想到后面还要回学校给导师做牛马,看着身边都是21-25的年纪,突然emo了了
点赞 评论 收藏
分享
点赞 评论 收藏
分享
点赞 收藏 评论
分享
牛客网
牛客企业服务