参考代码:
import java.util.*; public class Main { public static Map<Integer, List<Integer>> nodeToChildren; public static void main(String[] args) { Scanner sc = new Scanner(System.in); int N = sc.nextInt(); nodeToChildren = new HashMap<>(); for (int i = 2; i <= N; i++) { int parent = sc.nextInt(); if (!nodeToChildren.containsKey(parent)) { nodeToChildren.put(parent, new ArrayList<>()); } nodeToChildren.get(parent).add(i); } System.out.println(getMaxHeight(1)); } public static int getMaxHeight(int node) { if (!nodeToChildren.containsKey(node)) { return 0; } int max = 0; List<Integer> children = nodeToChildren.get(node); for (int child : children) { max = Math.max(max, getMaxHeight(child)); } return max + children.size(); } }
0.0分
2 人评分
C语言程序设计教程(第三版)课后习题10.1 (C语言代码)浏览:1435 |
震宇大神的杀毒软件 (C++代码)浏览:1108 |
简单的a+b (C语言代码)浏览:703 |
C语言程序设计教程(第三版)课后习题1.5 (C语言代码)浏览:510 |
找出最长的字符串来 (C语言代码)浏览:1760 |
C语言程序设计教程(第三版)课后习题10.2 (C语言代码)浏览:535 |
2003年秋浙江省计算机等级考试二级C 编程题(1) (C语言代码)浏览:809 |
C语言程序设计教程(第三版)课后习题1.5 (C++代码)浏览:403 |
1415题求解浏览:605 |
2004年秋浙江省计算机等级考试二级C 编程题(1) (C语言代码)浏览:580 |