参考代码:
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语言程序设计教程(第三版)课后习题7.4 (C++代码)(和输入10个数排序没有区别,直接sort即可)浏览:2098 |
DNA (C++代码)浏览:650 |
C二级辅导-分段函数 (C语言代码)浏览:566 |
C语言程序设计教程(第三版)课后习题8.5 (C语言代码)浏览:584 |
C语言训练-立方和不等式 (C语言代码)浏览:751 |
C语言程序设计教程(第三版)课后习题6.3 (C语言代码)浏览:517 |
【回文数(二)】 (C语言代码)浏览:730 |
C语言程序设计教程(第三版)课后习题6.3 (C语言代码)浏览:946 |
WU-字符串比较 (C++代码)浏览:757 |
【亲和数】 (C语言代码)浏览:600 |