publicstaticvoidmain(String[] args){ Scanner sc = new Scanner(System.in); int n = sc.nextInt(); int[] parent = newint[n];
for (int i = 0; i < n; i++) { parent[i] = -1; }
for (int i = 0; i < n - 1; i++) { int a = sc.nextInt(); int b = sc.nextInt(); parent[b] = a; }
int h = 0; for (int i = 0; i < n; i++) { int max = 1; int j = i; while (parent[j] != -1) { j = parent[j]; max++; } h = max > h ? max : h; } System.out.println(h); } }
n = int(sys.stdin.readline().strip()) dic_s = {} for i in range(int(n)-1): j_1,j_2 = sys.stdin.readline().strip().split() dic_s[j_2] = j_1 print max([get(i) for i in set(dic_s.keys()) - set(dic_s.values())]) if n>1else1
javascript
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
var n=readInt(),rec={},dps={},rs=1 while(n-->1) { var a=readInt(),b=readInt() rec[a]=rec[a]||[] rec[a].push(b) } for(var k in rec) rs=Math.max(rs,maxDps(k)) print(rs) functionmaxDps(n){ if(!rec[n]) return1 if(dps[n]) return dps[n] var r=1 for(var x of rec[n]) r=Math.max(r,maxDps(x)+1) dps[n]=r return dps[n] }