|
| 1 | +```java |
| 2 | +import java.io.BufferedReader; |
| 3 | +import java.io.IOException; |
| 4 | +import java.io.InputStreamReader; |
| 5 | +import java.util.*; |
| 6 | + |
| 7 | +public class Main { |
| 8 | + |
| 9 | + static int nodeCnt,ans; |
| 10 | + static int[] parents; |
| 11 | + static Node[] nodes; |
| 12 | + |
| 13 | + static class Node{ |
| 14 | + int num; |
| 15 | + int minute = 0; |
| 16 | + HashSet<Node> children = new HashSet<>(); |
| 17 | + |
| 18 | + public Node (int num) { |
| 19 | + this.num = num; |
| 20 | + } |
| 21 | + |
| 22 | + public int calculate() { |
| 23 | + |
| 24 | + if (children.size() == 0) { |
| 25 | + return 0; |
| 26 | + } |
| 27 | + PriorityQueue<Integer> pq = new PriorityQueue<>(); |
| 28 | + for (Node c: children) { |
| 29 | + pq.add(c.calculate()); |
| 30 | + } |
| 31 | + |
| 32 | + int min = 0; |
| 33 | + while (!pq.isEmpty()) { |
| 34 | + int num = pq.poll(); |
| 35 | + min = Math.max(min, num+pq.size()+1); |
| 36 | + } |
| 37 | + |
| 38 | + |
| 39 | + |
| 40 | + |
| 41 | + return min; |
| 42 | + } |
| 43 | + |
| 44 | + } |
| 45 | + |
| 46 | + |
| 47 | + |
| 48 | + |
| 49 | + public static void main(String[] args) throws NumberFormatException, IOException { |
| 50 | + init(); |
| 51 | + process(); |
| 52 | + print(); |
| 53 | + } |
| 54 | + |
| 55 | + |
| 56 | + |
| 57 | + public static void init() throws NumberFormatException, IOException { |
| 58 | + BufferedReader br= new BufferedReader(new InputStreamReader(System.in));; |
| 59 | + nodeCnt = Integer.parseInt(br.readLine()); |
| 60 | + parents = new int[nodeCnt]; |
| 61 | + nodes = new Node[nodeCnt]; |
| 62 | + ans = 0; |
| 63 | + |
| 64 | + StringTokenizer st = new StringTokenizer(br.readLine()); |
| 65 | + for (int i = 0; i < nodeCnt; i++) { |
| 66 | + nodes[i] = new Node(i); |
| 67 | + parents[i] = Integer.parseInt(st.nextToken()); |
| 68 | + |
| 69 | + } |
| 70 | + } |
| 71 | + |
| 72 | + |
| 73 | + public static void process() throws IOException { |
| 74 | + for (int i = 1; i< nodeCnt; i++) { |
| 75 | + int parent = parents[i]; |
| 76 | + nodes[parent].children.add(nodes[i]); |
| 77 | + } |
| 78 | + ans = nodes[0].calculate(); |
| 79 | + |
| 80 | + } |
| 81 | + |
| 82 | + |
| 83 | + |
| 84 | + |
| 85 | + public static void print() { |
| 86 | + System.out.println(ans); |
| 87 | + } |
| 88 | +} |
| 89 | +``` |
0 commit comments