需要自己创建树的数据结构
有向无环图
dfs
import java.util.Scanner; import java.util.*; import java.io.*; public class Main { private static int max = 0; private static ArrayList<Integer>[] graph; public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); int[] arr=new int[n+1]; for(int i=1;i<=n;i++){ arr[i] = sc.nextInt(); } graph = new ArrayList[n+1]; for(int i=1;i<=n;i++){ graph[i] = new ArrayList<>(); } for(int i=1;i<n;i++){ int a = sc.nextInt(); int b = sc.nextInt(); graph[a].add(b); graph[b].add(a); } for(int i=1;i<=n;i++){ dfs(arr,i,0); } System.out.println(max); } public static void dfs(int[] arr, int cur, int len){ len++; max = Math.max(max,len); for(int num:graph[cur]){ if(arr[num] > arr[cur]) dfs(arr,num,len ); } } }