描述
ACMCLUB王国有n+1座城市,标号分别是0到n,并且所有城市构成树状结构。0是国王所在的城市,相当于树状结构中的根。其他城市的成熟度定义为从0到该城市所包含的城市数(不包括0和该城市自己)。我们把最大的那个城市的成熟度作为整个王国的成熟度,现在我们想知道王国成熟度是多少。
输入
输入第一行为整数n(n<=10000),接下来有n行,每行输入一个数字,第i行表示节点i的父节点。数据保证构成一棵树。
输出
对于每组输入,输出一行,表示王国的成熟度。
样例输入1
5
3
3
0
2
0
样例输出1
2
import java.util.*; public class Main { static class City { int f; //父节点 int depth; boolean flag = false; //是否遍历过 City(int a, int b){ f = a; depth = b; } } static City[] city; public static int locDepth(int i) { if (city[i].f == 0) city[i].depth = 0; else city[i].depth = locDepth(city[i].f) + 1; city[i].flag = true; return city[i].depth; } public static void main(String args[]){ Scanner input = new Scanner(System.in); while (input.hasNext()) { int n = input.nextInt(); city = new City[n+1]; for (int i = 1; i <= n; i++) city[i] = new City(input.nextInt(), 0); for (int i = 1; i <= n; i++) if (!city[i].flag) locDepth(i); int max = 0; for (int i = 1; i <= n; i++) max = Math.max(max, city[i].depth); System.out.println(max); } } }
0.0分
0 人评分
C语言训练-阶乘和数* (C语言代码)浏览:980 |
震宇大神的杀毒软件 (C++代码)浏览:1110 |
C语言程序设计教程(第三版)课后习题6.11 (C语言代码)for循环浏览:1101 |
C语言程序设计教程(第三版)课后习题4.9 (C语言代码)浏览:1514 |
C语言程序设计教程(第三版)课后习题7.1 (C语言代码)浏览:512 |
三角形 (C++代码)记忆化搜索浏览:1220 |
C语言程序设计教程(第三版)课后习题9.2 (C语言代码)浏览:555 |
C语言程序设计教程(第三版)课后习题9.3 (C语言代码)浏览:668 |
大家好,我是验题君浏览:575 |
C语言程序设计教程(第三版)课后习题1.5 (C语言代码)浏览:398 |