Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.BufferedReader;
- import java.io.IOException;
- import java.io.InputStreamReader;
- import java.io.PrintWriter;
- import java.util.Arrays;
- import java.util.HashSet;
- import java.util.StringTokenizer;
- /**
- * Created by Aksenov239 on 16.06.2014.
- */
- public class AdamAndTree {
- public static void main(String[] args) {
- new AdamAndTree().run();
- }
- BufferedReader br;
- StringTokenizer in;
- PrintWriter out;
- public String nextToken() throws IOException {
- while (in == null || !in.hasMoreTokens()) {
- in = new StringTokenizer(br.readLine());
- }
- return in.nextToken();
- }
- public int nextInt() throws IOException {
- return Integer.parseInt(nextToken());
- }
- public void solve() throws IOException {
- int n = nextInt();
- int[] p = new int[n + 1];
- int[] dp = new int[n + 1];
- int[] max1 = new int[n + 1];
- int[] max2 = new int[n + 1];
- Arrays.fill(max1, -1);
- Arrays.fill(max2, -1);
- p[0] = -1;
- for (int i = 0; i < n; i++) {
- int x = nextInt() - 1;
- p[i + 1] = x;
- dp[i + 1] = 1;
- int now = i + 1;
- while (p[now] != -1) {
- if (dp[now] > max1[p[now]]) {
- max2[p[now]] = max1[p[now]];
- max1[p[now]] = dp[now];
- } else if (dp[now] > max2[p[now]]) {
- max2[p[now]] = dp[now];
- }
- if (dp[p[now]] < Math.max(max1[p[now]], max2[p[now]] + 1)) {
- dp[p[now]] = Math.max(max1[p[now]], max2[p[now]] + 1);
- } else {
- break;
- }
- now = p[now];
- }
- out.print(max1[0] + " ");
- }
- }
- public void run() {
- try {
- br = new BufferedReader(new InputStreamReader(System.in));
- out = new PrintWriter(System.out);
- solve();
- out.close();
- } catch (IOException e) {
- e.printStackTrace();
- System.exit(1);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement