沿着每一个儿子往下走,当前辈分的最深深度=当前层的兄弟数目+父辈的最深深度
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;
/**
* 统计结点的子节点个数,然后往下遍历即可
* 为了往下遍历,因此需要记录某一个结点有哪些儿子
* 使用单链表存储
*/
public class Main {
static int N=100000+5;
static int[] head=new int[N];
static int[] next=new int[N];
static int[] v=new int[N];
static int idx=0;
static int res=0;
public static void main(String[] args) throws IOException {
init();
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
String[] line = reader.readLine().split(" ");
int n=Integer.parseInt(line[0]);
for (int i = 0+2; i < n-1+2; i++) {
line=reader.readLine().split(" ");
int father=Integer.parseInt(line[0]);
add(father,i);
}
dfs(1,0);
System.out.println(res);
}
private static void dfs(int father,int cnt) {
//已经是根节点,如果是案例的话最根节点5也算在内的
if (head[father]==-1) {
res=Math.max(res, cnt);
}
int sonCnt=0;
for (int i = head[father]; i != -1; i=next[i]) {
sonCnt++;
}
for (int i = head[father]; i != -1; i=next[i]) {
int j=v[i];
// System.out.println(j+" "+sonCnt);
dfs(j, cnt+sonCnt);
}
}
private static void init() {
Arrays.fill(head, -1);
}
static void add(int a,int b){
v[idx]=b;next[idx]=head[a];head[a]=idx++;
}
}