题解 | #树上上升序列#

树上上升序列

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-11 11:00
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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